I'm running a Python script on my local computer using the Shortcuts app from my phone (this works perfectly well and returns data to my phone). I also want the script to display a web browser on the local computer. The code for this is simple:
import webbrowser
import sys
print("This print statement is shown on my phone")
webbrowser.get('C:/Program Files (x86)/Google/Chrome/Application/chrome.exe%s').open(str(sys.argv)) # works locally but not over ssh
print("This print statement is also shown on my phone")
But to make SSH display I'm confident that I would need the ssh -X or -Y argument which cannot be passed into the shortcuts app.
I can see two solutions which might work but I haven't been able to find
There is an equivalent argument to -X or -Y which can be passed in the main body of text for the ssh command
There is a way to pass arguments to the shortcut app
Any thoughts would be greatly appreciated!
not sure if i understand you correctly.
But, if you ask how to include parameters in your ssh that are attributed to the script you call. I made an example triggering a bash script through ssh, supplied with params.
# some variables
X="True"
Y="False"
Z="True"
# bash script
# echo supplied parameters
echo "$1"
echo "$2"
echo "$3"
# ssh command
sshpass -p 'pswd' ssh user#host "bash -s" < /home/user/sc.sh "$X $Y $Z"
Related
How can I run a command in a linux box through Paramiko as a sudo user and send the sudo password alomg with it, then get the output?
I tried the following code but since it did not return the output ( I tried testing ls -l /root) I am not sure if it’s working or not.
stdin, stdout, stderr = ssh.exec_command("sudo ls -l")
stdin.write('sudo_password\n')
stdin.flush()
Output = stdout.read.splitlines()
A second question is that My understanding is that we cannot ”sudo -s” in the first command and then run the next commands as root while we have used a non-root command to start the session with at first, right?
(Imagine there is no root password on the box)
What is a functional piece of code to send a command as sudo, then provide the password and get the output?
What is a functional code to do the same thing for multiple commands? Can we send the password once and then use the it for the rest of the commands?
sudo on the target system is configured to require a terminal emulation for password prompt.
It suggests you to use -S switch to allow password input from the stdin (what you code is attempting to do). So do that.
thanks to everyone taking their time and posting their answers.
there is a straightforward answer to this question and here it is:
command = 'echo "sudo-password" | sudo <the-command-to-run-as-sudo>'
stdin, stdout, stderr = SESSION.exec_command(command)
stdout = stdout.read().decode()
example command:
command = 'echo "123456" | sudo ls-l /root'
stdin, stdout, stderr = SESSION.exec_command(command)
stdout = stdout.read().decode()
I want a script to set up a better (but non-default) location for my .zshrc file:
% sudo echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv
but this fails:
zsh: permission denied: /etc/zshenv
The problem is related to the redirection being done first, but there is no obvious way to solve this. I can hear you saying 'use zsh -c' but that fails:
sudo zsh -c 'echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv'
results in $HOME being evaluated inside the shell launched with sudo, so my /etc/zshenv file contains
ZDOTDIR=/var/root/.config/zsh
Trust me: my home directory is not /var/root.
I tried --preserve-env but that did not work:
sudo --preserve-env zsh -c 'echo "ZDOTDIR=$HOME/.config/zsh" >> /etc/zshenv'
Obviously I can find a workaround for this, such as saving $HOME to a local variable in the script, but it seems like there must be a more direct and simple and straightforward way to do it.
I just realized I was thinking about the problem all wrong. I want to prevent $HOME from being evaluated. Of course escaping a single quote inside a single quote is a new kind of PITA, but here it is:
sudo zsh -c 'echo '"'"'ZDOTDIR=$HOME/.config/zsh'"'"' >> /etc/zshenv'
I want to install Anaconda through EasyBuild. EasyBuild is a software to manage software installation on clusters. Anaconda can be installed with sh Anaconda.sh.
However, after running I have to accept the License agreement and give the installation location on the command line by entering <Enter>, yes <Enter>, path/where/to/install/ <Enter>.
Because this has to be installed automatically I want to do the accepting of terms and giving the install location in one line. I tried to do it like this:
sh Anaconda.sh < <(echo) >/dev/null < <(echo yes) >/dev/null \
< <(echo /apps/software/Anaconda/1.8.0-Linux-x86_64/) > test.txt
From the test.txt I can read that the first echo works as <Enter>, but I can't figure out how to accept the License agreement, as it sees it now as not sending yes:
Do you approve the license terms? [yes|no]
[no] >>> The license agreement wasn't approved, aborting installation.
How can I send the yes correctly to the script input?
Edit: Sorry, I missed the part about having to enter more then one thing. You can take a look at writing expect scripts. thegeekstuff.com/2010/10/expect-examples. You may need to install it however.
You could try piping with the following command: yes yes | sh Anaconda.sh. Read the man pages for more information man yes.
Expect is a great way to go and probably the most error proof way. If you know all the questions I think you could do this by just writing a file with the answers in the correct order, one per line and piping it in.
That install script is huge so as long as you can verify you know all the questions you could give this a try.
In my simple tests it works.
I have a test script that looks like this:
#!/bin/sh
echo -n "Do you accept "
read ANS
echo $ANS
echo -n "Install path: "
read ANS
echo $ANS
and an answers file that looks like this:
Y
/usr
Running it like so works... perhaps it will work for your monster install file as well.
cat answers | ./test.sh
Do you accept Y
Install path: /usr
If that doesn't work then the script is likely flushing and you will have to use expect or pexpect.
Good luck!
Actually, I downloaded and looked at the anaconda install script. Looks like it takes command line arguments.
/bin/bash Anaconda-2.2.0-Linux-x86_64.sh -h
usage: Anaconda-2.2.0-Linux-x86_64.sh [options]
Installs Anaconda 2.2.0
-b run install in batch mode (without manual intervention),
it is expected the license terms are agreed upon
-f no error if install prefix already exists
-h print this help message and exit
-p PREFIX install prefix, defaults to /home/cody.stevens/anaconda
Use the -b and -p options...
so use it like so:
/bin/bash Anaconda-2.2.0-Linux-x86_64.sh -b -p /usr
Also of note.. that script explicitly says not to run with '.' or 'sh' but 'bash' so they must have some dependency on a feature of bash.
--
Cody
I am trying to get the output of a command in a variable and checking whether its matching with other variable.
$login1=`ssh ****************** date`;
This command when typed manually will expect a prompt " Password: " . When i run it from the script it is ruuning that command and printing that prompt waiting for user to enter, but i dont need that. I just need to get that output and compare
if($login1=~ /Password:/)
{
print " yes";
}
else
{
print "No ";
}
However the script is just stopping at Password prompt . Please suggest me on how to achieve this .
You might want to look at the -f flag for ssh:
-f Requests ssh to go to background just before command execution.
This is useful if ssh is going to ask for passwords or
passphrases, but the user wants it in the background. This
implies -n. The recommended way to start X11 programs at a
remote site is with something like ssh -f host xterm.
If you want to avoid passwords, set up a public/private key pair with no passphrase (dangerous, but much less dangerous than putting a password in a script) and copy the public key to the remote site. IIRC, it goes something like this:
localhost $ ssh-keygen -b 2048 -t ecdsa -N '' -f ./datekey
localhost $ scp ./datekey.pub remotehost:/tmp
localhost $ ssh remotehost
(login)
remotehost $ cat /tmp/datekey.pub >> ~/.ssh/authorized_keys
remotehost $ logout
localhost $ ssh -i ./datekey remotehost date
Make sure you store ./datekey somewhere no other user can access it at all -- not even read access.
If you're just trying to detect, you might simply need to feed it EOF to get it to move along:
$login1=`ssh ****************** date < /dev/null`;
I am trying to control a remote Python script via psexec, which reads commands from stdin, but I need to redirect psexec's input since psexec itself will be launched from another program. However, I have no luck making psexec accept redirected input. Is it supposed to work at all?
An example of what I'm trying to do, where input is a file containing input to the remote script:
psexec \\mymachine python c:\script.py < input
Here's one way I was able to kinda accomplish what you're after:
PsExec.exe -d \\\\192.168.1.1 cmd /k "echo list volume | diskpart"
This would pass the commands "list volume" to the diskpart command. Additionally you can also try using cmd like this for you example:
PsExec.exe -d \\\\192.168.1.1 cmd /k "python c:\script.py < input"