在远程计算机上运行bash脚本的一部分

时间:2015-07-04 08:20:33

标签: bash ssh

我需要在本地运行一些命令,然后在远程机器上使用单个本地bash脚本运行一些命令。

为简单起见,我只想说明并执行以下操作并在本地桌面计算机上执行。

#!/bin/bash

#upload some files to a remote machine
cd /tmp
./upload-files.sh

#run commands on remote machine
ssh myuser:mypass@somewhere.com
cd /tmp/uploads    <--- these commands don't run in the ssh connection
./process-uploads.sh
exit

#run command locally again.
cd -
echo 'complete!'

关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:1)

您可以here-doc使用ssh命令:

#!/bin/bash

#upload some files to a remote machine
cd /tmp
./upload-files.sh

#run commands on remote machine
ssh -t -t myuser:mypass@somewhere.com<<EOF
cd /tmp/uploads
./process-uploads.sh
exit
EOF

#run command locally again.
cd -
echo 'complete!'

答案 1 :(得分:0)

如果您只想运行一个命令:

ssh myuser:mypass@somewhere.com 'cd /tmp/uploads; ./process-uploads.sh'