[英]SSH to machine through a middle host
In my work with my professor I have to ssh into our server and from there I ssh into each node to run our programs. 在我与我的教授的工作中,我必须ssh到我们的服务器,然后从那里我进入每个节点来运行我们的程序。 I am trying to write a python program that will let me do everything that I need to do on the remote node from my local machine.
我正在尝试编写一个python程序,它将让我从本地计算机上执行远程节点上需要执行的所有操作。 The commands that I will be running on the nodes are:
我将在节点上运行的命令是:
Right now I make my input files on my local machine, scp them to the server, then I copy the files to each node and run our fluid_dynamics program on each node. 现在我在我的本地机器上创建我的输入文件,将它们scp到服务器,然后我将文件复制到每个节点并在每个节点上运行我们的fluid_dynamics程序。 I then do the reverse to get our output back to my local machine.
然后我反过来将输出恢复到我的本地机器。
I was looking at paramiko but I can not figure out how I can use it to get from my local machine to the nodes because I must go through the server. 我正在看paramiko,但我无法弄清楚如何使用它从我的本地机器到节点,因为我必须通过服务器。 local -ssh--> server -ssh--> nodes
local -ssh - > server -ssh - > nodes
Is there a way to do this in python or should I try something else such as: using: 有没有办法在python中执行此操作,或者我应该尝试其他方法,如:using:
os.system(ssh -t server ssh node 'command')
or making a bash scripts on the server for each of the different commands (compile.sh, move_inputs.sh, retrieve_outputs.sh) and then just connecting to the server and running the bash scripts. 或者在服务器上为每个不同的命令(compile.sh,move_inputs.sh,retrieve_outputs.sh)创建一个bash脚本,然后只连接到服务器并运行bash脚本。
Sorry if this doesn't make sense or if it is worded badly, any help is appreciated. 对不起,如果这没有意义或措辞不当,任何帮助表示赞赏。
Additional Info: The reason I am using python is because I want the program to be able to make the input files, send them to the nodes and retrieve the output files, and to finally generate graphs of our data. 附加信息:我使用python的原因是因为我希望程序能够生成输入文件,将它们发送到节点并检索输出文件,并最终生成我们数据的图形。 I already have some code to generate our input files and to make the graphs from the outputs.
我已经有了一些代码来生成输入文件并从输出中生成图形。
You don't need Python to do this. 你不需要Python来做到这一点。 Check the ProxyCommand configuration option for SSH .
检查SSH的ProxyCommand配置选项 。 Here is a tutorial that explains the details.
这是一个解释详细信息的教程 。
With a trick from my colleague, you can ssh/scp from local to nodes directly. 通过我的同事的技巧,您可以直接从本地到节点ssh / scp。
Edit your ~/.ssh/config: 编辑你的〜/ .ssh / config:
Host *
ControlMaster auto
ControlPath ~/.ssh/master-%r@%h:%p
Host node1 node2 or node*
ProxyCommand ssh server 'nc -w 5 %h 22'
Have fun! 玩得开心!
You can do it with Paramiko: 你可以用Paramiko做到这一点:
proxy_command = 'ssh -i %s %s@%s nc %s %s' % (proxy_key, proxy_user, proxy_host, host, 22)
proxy = paramiko.ProxyCommand(proxy_command)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=user, password=password, sock=proxy)
stdin, stdout, stderr = client.exec_command('echo HELLO')
print "Echo: %s" % (", ".join(stdout.readlines()))
client.close()
It works with SFTPClient
too: 它也适用于
SFTPClient
:
proxy_command = 'ssh -i %s %s@%s nc %s %s' % (proxy_key, proxy_user, proxy_host, host, 22)
proxy = paramiko.ProxyCommand(proxy_command)
transport = paramiko.Transport(proxy)
transport.connect(username=user, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
You can do this by creating a tunnel through your server to the node: 您可以通过在服务器上创建到节点的隧道来执行此操作:
import os, sys, shlex
import subprocess
import paramiko
cmd = "ssh -f -N -p " + str(serverport) + " -l " + serveruser + " -L " + str(tunnelport) + ":" + nodehost + ":" + str(nodeport) + " " + serverhost
args = shlex.split(cmd)
tun = subprocess.Popen(args)
stat = tun.poll()
Once the tunnel is set up you can ftp to the nodes: 设置隧道后,您可以ftp到节点:
transport = paramiko.Transport(("127.0.0.1", tunnelport))
transport.connect(username=nodeusername, password=nodepw)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(localfile, remotefile)
Or you can connect and execute a command using paramiko.SSHClient().connect("127.0.0.1", port=port, username=user, password=pw) and paramiko.SSHClient().exec_command(command). 或者,您可以使用paramiko.SSHClient()。connect(“127.0.0.1”,port = port,username = user,password = pw)和paramiko.SSHClient()。exec_command(command)连接并执行命令。
Then the tunnel process can be killed thus: 然后可以杀死隧道进程:
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.splitlines():
if cmd in line:
pid = int(line.split(None, 1)[0])
os.kill(pid, signal.SIGKILL)
Use plink root@10.112.10.1 -pw password ls -l
使用plink
root@10.112.10.1 -pw password ls -l
Download plink and copy it to your windows machine 下载plink并将其复制到您的Windows机器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.