使用 ssh 在 bash 脚本中的远程服务器中连接和执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18350232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:19:24  来源:igfitidea点击:

Using ssh to connect and execute commands in a remote server within a bash script

bashsshsh

提问by canecse

I'm trying to figure out a way to ssh to a remote file, check whether a file exists, and if it exist ssh back to the first server, execute a command and get done. Here is a snippet of my code so far:

我正在尝试找出一种 ssh 到远程文件的方法,检查文件是否存在,如果存在则 ssh 返回到第一台服务器,执行命令并完成。到目前为止,这是我的代码片段:

for file_name in $(ls $FILE_DIR/)
do
    ssh [email protected]: home/some/directory
    'if [ -f $DIRECTORY/$file_name ]
    then
        ssh [email protected]: home/some/Directory
        'scp [email protected]: $DIRECTORY/$file_name $DIRECTORY/$file_name"_"$(date +%Y%m%d%H%M%S)


    fi''
        scp [email protected] $FILE_DIR/$file_name $DIRECTORY/$file_name

When I execute the script, the connection to the remote server opens up in the command line and the rest of the script doesn't get executed.

当我执行脚本时,到远程服务器的连接在命令行中打开,脚本的其余部分不会被执行。

Also, is what I'm trying to do valid? Can I ssh within an server that has been accesed through ssh?

另外,我正在尝试做的事情有效吗?我可以在已通过 ssh 访问的服务器中使用 ssh 吗?

回答by Malcolm Jones

So I have a couple suggestions:

所以我有几个建议:

  1. When running remote commands, sometimes its important to simulate a tty. In order to do that, Try ssh -t -t to force pseudo-tty allocation even if stdin is not a terminal.

  2. Have you thought about using salt-stack? Salt allows you to run remote commands, in parallel on multiple machines across a network. Salt is an amazing solution for running remote commands on other machines, and its extremely easy to setup.

  3. Try running your remote command with bash -x before running the script, that way, you should see all output needed to help you debug the situation. If thats not enough, make sure you redirect standard out/error to a file to see what went wrong eg:

    ssh [email protected] 'bash -x ./script_2004.5664 | tee -a ~/some.log > 2>&1'

  1. 运行远程命令时,有时模拟 tty 很重要。为了做到这一点,即使 stdin 不是终端,也可以尝试 ssh -t -t 强制伪 tty 分配。

  2. 你有没有想过使用salt-stack?Salt 允许您在网络上的多台机器上并行运行远程命令。Salt 是在其他机器上运行远程命令的绝佳解决方案,并且非常容易设置。

  3. 在运行脚本之前尝试使用 bash -x 运行远程命令,这样,您应该会看到帮助您调试情况所需的所有输出。如果这还不够,请确保将标准输出/错误重定向到文件以查看出了什么问题,例如:

    ssh [email protected] 'bash -x ./script_2004.5664 | tee -a ~/some.log > 2>&1'

回答by Aleks-Daniel Jakimenko-A.

First of all, do not use ls to iterate over files. Bash can do that on its own.

首先,不要使用 ls 来遍历文件。Bash 可以自己做到这一点。

for file_name in "$FILE_DIR"/*

Also use [[ ]] instead of [

也使用 [[ ]] 而不是 [

Here is the script that probably does what you want:

这是可能执行您想要的操作的脚本:

for filename in "$FILE_DIR/"*; do
    # Check if file exists on the remote server
    if [[ $(ssh [email protected] "[[ -f $DIRECTORY/$filename ]] && echo 1") ]]; then
        # Download that file
        scp [email protected]: "$DIRECTORY/$filename" "$DIRECTORY/${filename}_$(date +%Y%m%d%H%M%S)"
    fi
done