简体   繁体   English

Bash:如何远程运行脚本

[英]Bash: how to run a script remotely

I have a script (say run.py ) and I want to scp that to a remote machine (say 10.1.100.100 ), cd into a directory in that remote machine, and execute run.py in that directory. 我有一个脚本(比如run.py ),我想将它scp到远程机器(比如10.1.100.100 ),cd到该远程机器的目录中,然后在该目录中执行run.py

How do I wrap the above procedure in one single bash script? 如何在一个单独的bash脚本中包含上述过程? I don't know how to let bash execute commands remotely in another machine. 我不知道如何让bash在另一台机器上远程执行命令。

Hopefully I can see that stdout of run.py in my terminal. 希望我能在终端中看到run.py stdout。 But if I can only redirect it, that's fine as well. 但如果我只能重定向它,那也没关系。

chmod +x ./run.py
scp -pq  ./run.py 10.1.100.100:'/home/myremotedirectory/run.py'
ssh 10.1.100.100     'cd /somedirectory  &&  /home/myremotedirectory/run.py'

See if that helps 看看是否有帮助

You can do it like this: 你可以这样做:

ssh -l yourid 10.1.100.100 << DONE
cd /your/dir/
./run.py
DONE

Above has been edited, I don't remember what it was like originally, if I want to do it in one single connection, I will do it this way. 上面已经编辑过,我不记得它原来是什么样的,如果我想在一个连接中做到这一点,我会这样做。

ssh -l yourid 10.1.100.100 python < <(
echo "import os"
echo "os.chdir('/yourdir')"
echo "print(os.getcwd())"
cat yourscript.py
)

How to run a local script over SSH 如何通过SSH运行本地脚本

Synopsis: 概要:

Script execution over SSH without copying script file. 通过SSH执行脚本而不复制脚本文件。 You need a simple SSH connexion and a local script. 您需要一个简单的SSH连接和本地脚本。

Code: 码:

#!/bin/sh
print_usage() {
        echo -e "`basename $0` ssh_connexion local_script"
        echo -e "Remote executes local_script on ssh server"
        echo -e "For convinient use, use ssh public key for remote connexion"
        exit 0
}

[ $# -eq "2" ] && [ $1 != "-h" ] && [ $1 != "--help" ] || print_usage

INTERPRETER=$(head -n 1 $2 | sed -e 's/#!//')

cat $2 | grep -v "#" | ssh -t $1 $INTERPRETER

Examples: 例子:

 - ssh-remote-exec root@server1 myLocalScript.sh #for Bash
 - ssh-remote-exec root@server1 myLocalScript.py #for Python
 - ssh-remote-exec root@server1 myLocalScript.pl #for Perl
 - ssh-remote-exec root@server1 myLocalScript.rb #for Ruby

Step by step explanations 一步一步的解释

This script performs this operations: 1° catches first line #! 此脚本执行此操作:1°捕获第一行#! to get interpreter (ie: Perl, Python, Ruby, Bash interpreter), 2° starts remote interpeter over SSH, 3° send all the script body over SSH. 获得解释器(即:Perl,Python,Ruby,Bash解释器),2°通过SSH启动远程互操作,3°通过SSH发送所有脚本体。

Local Script: 本地脚本:

Local script must start with #!/path/to/interpreter 本地脚本必须以#!/ path / to / interpreter开头

- #!/bin/sh for Bash script
 - #!/usr/bin/perl for Perl script
 - #!/usr/bin/python for Python script
 - #!/usr/bin/ruby for Ruby script

This script is not based on local script extension but on #! 此脚本不是基于本地脚本扩展,而是基于#! information. 信息。

Remember, that this is not a rule, that you HAVE TO cd to the requested directory. 请记住,这不是一个规则,你必须cd到所请求的目录。

Once you get access to the remote machine, just type a relative path to this file, without using cd: 一旦您访问远程计算机,只需键入此文件的相对路径,而不使用cd:

/some_folder/./run.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM