登录到远程计算机并运行脚本

时间:2018-07-23 05:38:07

标签: python linux bash shell ssh

我有一台中央计算机和多台远程计算机。已配置从中央计算机到远程计算机的无密码连接。

我必须从中央虚拟机登录到远程虚拟机并在远程虚拟机中执行脚本

下面是示例,我正在尝试

中央机器:

list[0] = choice(pair).copy()

远程机器

#!/bin/sh
ssh root@remotemachine1
#run runscript.py in remotemachine1
ssh root@remotemachine2
#run runscript.py in remotemachine2

runscript.py

python runscript.py

有人帮助我如何在remote1计算机上登录并运行脚本,以及如何从remote1计算机退出并在remote2计算机上运行脚本。

1 个答案:

答案 0 :(得分:2)

您可以运行以下命令

ssh  $DEST_USER@$DEST_MACHINE "python -u path_to_your_script.py"

如果脚本不存在,请确保先使用scp上传脚本,然后执行上述命令。

device = ["device1","device2","device3"]
for i, x in enumerate(device):**
    print ("{}. {}".format(i+1, x))
select_device = raw_input("Choose Device: ")
print "You Selected Device {}".format(device[int(select_device) - 1])