I have a Python program that I'm running in Ubuntu on a local machine. At some point during the program I connect to two remote machines: one running Ubuntu and the other running Windows. I have the IP addresses, names, and passwords to connect to these machines. The local machine sends the remote machines a command, and the remote machines send a response. Right now I'm just trying to get the remote machines to echo 'hello' back to the local machine. I can remote into both of these machines on the command line from my local machine using:
sshpass -p password ssh -X name#ipaddress
where password, name, and ipaddress are correctly filled in for each machine. So I know that I can remote into both of the machines just fine. From there I can run any command as if I was on that machine locally.
However, I would like to do this from within my program while sending in a simple echo 'hello' instruction. I can successfully accomplish this with the remote Ubuntu server with the following:
from subprocess import Popen, PIPE
cmd = ["sshpass", "-p", password, "ssh", "-X", name+"#"+ipaddress, "echo 'hello'"]
ssh = Popen(cmd, stdout=PIPE, stderr=PIPE)
print ssh.stdout.readlines()
The password, name, and ipaddress are variables that are already defined by this point. This works on the Ubuntu remote server where it returns the following list:
['hello\n']
This is good, but when I try the same on the Windows server I get an empty list:
[]
I've tried various commands and I always get an empty list. Is there something else or different that I need to be doing when connecting to the Windows server? Thank you in advance.
I looked at the stderr and that includes the following list:
['X11 forwarding request failed on channel 0\r\n', 'exec request failed on channel 0\r\n']
When I don't include the -X tag then I only get the second error:
['exec request failed on channel 0\r\n']
The target machine most likely does not have a Linux like commands installed. Install something like Cygwin on the Windows machine.
Related
I can ssh into a remote machine.
I then try to connect to a jupyter notebook job that I started on one of the nodes of the remote machine:
ssh -L 8069:localhost:8069 me#remote.machine ssh -L 8069:localhost:8069 me#node14
This has always worked fine in the past.
When I execute this lately, nothing happens until I eventually get a time out message. If I cancel it and then try to simply ssh into the remote machine again, it again does nothing until I get the error message:
ssh: connect to host remote.machine port 22: Connection timed out
I am trying to figure out if this is a problem at my end or at the remote machine. If it's the latter I can't understand why I am able to ssh to the remote machine fine until I try the
ssh -L 8069:localhost:8069 me#remote.machine ssh -L 8069:localhost:8069 me#node14
connection.
You are trying to do a double ssh connection: one to remote.machine and then another one to node14.
The problem seems to be the ssh process in the node14 machine. So, you can connect to the first machine but no to the second one. Ask your administrator to enable the sshd process in node14
You can test this case by logging into remote.machine via:
ssh -L 8069:localhost:8069 me#remote.machine.
Once you get shell access you can try the connection to node14 via:
ssh -L 8069:localhost:8069 me#node14.
According to the description, this last try should fail with the timeout.
I'm trying to run a local Python script from my laptop (which works fine) on remote server (VPS).
The script can't read local files from my laptop from VPS
Output:
My script on pycharm.
import sys
import time
import paramiko
# Connect to remote host
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('myip', port=22, username='root', password='mypassword')
# Setup sftp connection and transmit this script
sftp = client.open_sftp()
sftp.put(r'/myscript.py', '/myscript.py')
sftp.close()
# till now everything's good. I check my VPS files i find my script uploaded there
# Run the transmitted script remotely without args and show its output.
# SSHClient.exec_command() returns the tuple (stdin,stdout,stderr)
stdout = client.exec_command('python3 /myscript.py')[1]
for line in stdout:
# Process each line in the remote output
print(line)
client.close()
sys.exit(0)
When I run script from VPS I got this issue
I can't run the script directly from VPS to check the issue because I use local files, check the screenshot:
When I remove local paths and run the script (both from pycharm and VPS), it works fine.
You cannot magically access local files from script run on a server.
I can't run the script directly from VPS to check the issue because i use local files, check img.
There's no difference between running the script in remote shell using your favourite SSH terminal client (I assume that's what you mean by "run the script directly from VPS") and running the script in remote shell using Paramiko. It still runs in the remote shell.
There's no easy way to make the client files accessible from the server. That would be a security nightmare.
Either your script has to upload the files to the server.
Or you need to run a (SFTP/FTP/whatever) server on your local machine to make your local file accessible to the world.
For an example how to run an SFTP server, see my guide:
Installing SFTP/SSH server on Windows using OpenSSH
I am trying to connect to a remote server from a jump server. It connected to the remote server perfectly, but when I try to run a python script on the remote server, it says no directory found. Please help
gateway_session = SSHSession('host',
'unman', password='password').open()
# from jump server, establish connection with a remote server
remote_session = gateway_session.get_remote_session('host',
'username',password='password')
print(gateway_session.get_cmd_output('python /Folder/test.py'))
Try first to check where you are:
print(gateway_session.get_cmd_output('pwd;ls -alrth; ls -alrth /'))
That way, you know if there is indeed a Folder at /
The OP adds in the comments:
all I needed was to was separate the commands with a semi colon and run them using the same get output command.
The OP adds:
I just sshpass to a jump server from their, I sshpass to the remote on the same command in a shell script.
I am trying to ran a matlab executable application from Python on a remote server.
I used following code:
os.system("\\Server-01\\D$\\matlab_t.exe 7.25 16") # 7.25 and 16 are input arguments of matlab_t.exe
The above code is running on my local machine. I noticed that it is using resources (CPU and memory) of my local machine, while I am trying to use resources on the remote server.
May I know how I can execute it using server resource?
Thanks.
That command will run on your computer, the path may be pointing to a remote server, but no one has told the remote server that it should execute code, only that they need to serve the matlab_t.exe file.
You have to use a mechanism to access the remote server. Normally ssh is used for this purpose, but the ssh daemon has to be running on the remote server and also you need to have access (ask you admin about that).
Then you can use python like this:
import paramiko
ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute_on_remote_server)
In python, the os.system command only executes the command on the local machine. What you want is a local command that will get the server to execute it by itself.
If the server is Windows based then you can use PsExec to do this, if the server is Linux based then using ssh with a python library (like the other answer demonstrates) would probably be the way to go.
Using PsExec, your command in os.system would be something like:
psexec.exe \\Server-01 -u <username> -p <password> D:\matlab_t.exe 7.25 16
If you server needed no authentication, you could remove the username and password flags.
I have a Windows machine where I start an IPython kernel (to do some stuff not possible on another machine).
I have a Linux machine from which I would like to connect to the IPython kernel running on the Windows machine.
I can SSH from the Linux machine to the Windows machine (using this solution: https://superuser.com/a/172299).
I have tried following: https://github.com/ipython/ipython/wiki/Cookbook:-Connecting-to-a-remote-kernel-via-ssh. Both the automatic and the manual solution gives the following:
"ERROR: Kernel did not respond"
Using the same solution, I can connect from my Linux machine to an IPython kernel running on a Linux server. Any solution to get this to work with Linux to Windows?
You don’t need SSH to connect to a remote ipython kernel, regardless of whether it’s a ipython kernel running on Windows or Linux or Mac. What you do need is to have the Ip of the remote kernel visible to the terminal from which you are trying to connect from. Here are the steps:
Find out the ip address of the server (the machine on which the ipython kernel is running i.e. where you want the computation to happen) and client (the machine from which you are trying to connect to):
1.1. If you are on Windows, open up the command prompt and do a ipconfig to find out the ip addresses. If the Windows server has a direct Internet connection/lan connection, you should see a couple of ips like 192.168.57.1 and 10.2.3.64 and 127.0.0.1.
1.2. If you are on linux, open up a terminal and type ifconfig or ip addr show. You should again see a couple of ips like 192.168.57.1 and 10.2.3.64 and 127.0.0.1.
1.3. Test that atleast one of your server ip addresses is visible from the client: Ping your server from your client, using the command ping. ping will work on either Windows or Linux terminals. If you are running the windows/Linux as a VM or is behind a firewall, it is very much possible that your client or server is not visible from the other side. You don’t have to ping the ip address 127.0.0.1. This is a loop back address, and is only visible from the same machine where you got this ip address from. For example if you ping 127.0.0.1 from the Windows machine, it will ping the same Windows machine. If your client and server instances are running on the same machine, then its fine to use this address. However, if your client or server is running on a VM or a different machine altogher, then 127.0.0.1 wont work.
Start the remote kernel:
2.1. Once you have figured out which ip address on the server is visible from the client, start a kernel on the machine using ipython kernel. The ipython kernel will startup and show that `To connect another client to this kernel, use:
--existing kernel-1234.json
2.2. Locate the kernel-1234.json file on your server by importing (https://stackoverflow.com/a/35094772/4752883)
In [1]: from jupyter_client import find_connection_file
In [2]: find_connection_file()
Out[2]: 'C:\\Users\\me\\AppData\\Roaming\\jupyter\\runtime\\kernel-1234.json'
This will work either for Linux or Windows.
Start the remote client:
3.1. Once you locate the file, copy it over to your server machine using scp in linux or pscp or winscp in windows SCP w/ ssh: copying a local file from windows to a remote server using scp
3.2. Make sure that you are the same directory as the kernel-1234.json file.
3.3. Open up the kernel-1234.json file using vim or your favorite text editor. You will notice a line saying "ip": "127.0.0.1". Change 127.0.0.1 the ip address from your server that is visible from the client, that you found in step 1.3 and save the json file.
3.4. Start up the remote kernel using jupyter console –existing=kernel-1234.json, while located in the same drive where kernel-1234.json is located.
If you have followed the steps above, you should now be able to connect to the remote ipython kernel regardless of whether the ipython kernel is running on Windows/Linux/Mac.
I tried the manual way on https://github.com/ipython/ipython/wiki/Cookbook%3a-Connecting-to-a-remote-kernel-via-ssh once again and it worked. In detail:
windows-machine$ ipython kernel -f kernel-1234.json
linux-machine$ scp windows-machine:path/to/kernel-1234.json .
linux-machine$ cat kernel-1234.json
{
"stdin_port": 55534,
"ip": "127.0.0.1",
"control_port": 58391,
"hb_port": 60540,
"signature_scheme": "hmac-sha256",
"key": "fa461cf7-f078-4c22-909f-cfa7d2a30596",
"shell_port": 60159,
"transport": "tcp",
"iopub_port": 59207
}
linux-machine$ ssh -f -N -L 55534:127.0.0.1:55534
linux-machine$ ssh -f -N -L 58391:127.0.0.1:58391
linux-machine$ ssh -f -N -L 60540:127.0.0.1:60540
linux-machine$ ssh -f -N -L 60159:127.0.0.1:60159
linux-machine$ ssh -f -N -L 59207:127.0.0.1:59207
linux-machine$ ipython console --existing kernel-1234.json