linux - 在脚本中执行 ssh 命令

标签 linux shell ssh


我尝试制作一个脚本来自动化我的实验。
我的目的是从一个 shell 脚本运行这样的命令,该脚本在服务器上运行另一个脚本:

ssh root@server.com 'sh ControlIperfServer.sh start'

即使文件 ControlIperfServer.sh 已经放在 server.com 的/root 目录中,但终端的输出是:

bash: sh ControlIperfServer.sh start: command not found

当我在终端中输入命令时,我能够运行该命令。仅当从脚本 执行时才无法执行。

我的意思是:
当我在终端中键入命令时。它有效。

[root@CentOSVanilla ~]# ssh root@server.com 'sh ControlIperfServer.sh start'
Running IPERF SERVER
[root@CentOSVanilla ~]# 

当我将命令放入脚本并执行脚本时。这是行不通的。

[root@CentOSVanilla ~]# ./Runtest.sh
bash: sh ControlIperfServer.sh start: command not found
[root@CentOSVanilla ~]#

我使用了 verbose -v 选项来跟踪

debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug1: Offering public key: /root/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_CTYPE = UTF-8
debug1: Sending command: 'sh ControlIperfServer.sh start;'
bash: sh ControlIperfServer.sh start;: command not found
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2352, received 2360 bytes, in 0.2 seconds
Bytes per second: sent 10829.0, received 10865.9
debug1: Exit status 127

非常感谢您的帮助。

我添加我的 Runtest.sh 的代码:

#!/bin/sh
# A script to run test automatically, need modify when using
# Usage:
#       ./RunTest.sh <number of test> [threads]
#       [threads]: when there is threads, use -P option of Iperf
#                  Otherwise, run multiple instances of Iperf
#       <number of test>: Defaut is 1
#                         Number of Threads/Processes
#                         Number of time capture traffic
RUN_IPERF_SERVER="sh ControlIperfServer.sh start"
STOP_IPERF_SERVER="sh ControlIperfServer.sh stop"
RUN_TCPDUMP="sh ControlTCPdump.sh start"
STOP_TCPDUMP="sh ControlTCPdump.sh stop"
RUN_CLIENT="iperf -c"
TARGET_SERVER="10.10.10.253"
REMOTE_SERVER="server.com"
DIRECTORY="CaptureTraffic"
TCP_DURATION="60"

if [ "$1" = "" ]; then
    MAX_TEST=1;
else
    MAX_TEST=$1;
fi

# Run server daemon on remote server
CMD="ssh -v root@$REMOTE_SERVER \"$RUN_IPERF_SERVER\""
echo Running $CMD
$CMD # Run iperf server remotely
sleep 5 # Waiting for server to run

CMD="ssh -v $REMOTE_SERVER 'mkdir $DIRECTORY && echo ok'"
DIROK="$($CMD)"
if [ "$DIROK" = ok ]; then
    mkdir $DIRECTORY
else
    echo Cannot create directory\! Please be careful\!
fi

for INCR in $(seq 1 $MAX_TEST)
do
    echo Running Test $INCR

    #Prepare TCP to capture traffic (run on host)
    echo Setting-up TCPdump for capture traffic
    FILENAME="$DIRECTORY/CapturedTraffic$INCR.dmp"
    ssh $REMOTE_SERVER \'$RUN_TCPDUMP $FILENAME\'
    sleep 5

    # Running the client (running locally)
    if [ "$2" = threads ]; then
        echo "Running Iperf Client as threads model"
        $RUN_CLIENT $TARGET_SERVER -t $TCP_DURATION -P $INCR
    else
        echo "Running Iperf Client as processes model"
        sh parallel.sh -j $INCR \"$RUN_CLIENT $TARGET_SERVER -t $TCP_DURATION\"
    fi

    #Stop TCPdump (run on host)
    ssh $REMOTE_SERVER '$STOP_TCPDUMP'    
    sleep 5 #Wait for TCPdump write file to harddrive

done

#Stop Iperf Deamon
ssh $REMOTE_SERVER '$STOP_IPERF_SERVER'
sleep 3

我已经将所有脚本文件放在名为“server.com”的主机上。

最佳答案

这个

CMD="ssh -v root@$REMOTE_SERVER \"$RUN_IPERF_SERVER\""

是个坏主意。 CMD 中嵌入的引号被视为文字引号;它们不仅仅用于保护 RUN_IPERF_SERVER 中的空白。将命令的名称放在变量中是可以的,但它的参数应该分开。如果您正在使用 bash(或其他一些支持数组的 shell),我推荐

CMD=ssh
CMD_OPTIONS=( -v root@REMOTE_SERVER "$RUN_IPERF_SERVER" )
$CMD "${CMD_OPTIONS[@]}"

如果你不能使用数组,你可以回退到使用eval:

CMD="ssh -v root@$REMOTE_SERVER \"$RUN_IPERF_SERVER\""
eval "$CMD"

但我会推荐这个

CMD=ssh
CMD_OPTIONS="-v root@REMOTE_SERVER"  # Note no whitespace to protect
$CMD $CMD_OPTIONS "$REMOTE_IPERF_SERVER"

关于linux - 在脚本中执行 ssh 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15529594/

相关文章:

android - 如何导出结构体成员

windows - 使用 git bash 的自定义控制台

github - 无法通过 ssh 连接到 github

linux - 在 ubuntu 32 位上构建 trilinos 时出现内部 cmake 错误

linux - 更改用户并在 1 行中运行 ssh 指令

linux - 如何在不离开当前 shell 的情况下在当前 shell 中间脚本中设置 sudo?

unix - 检查符号链接(symbolic link)

使用sftp到特定文件夹的linux新用户

java - clj-ssh/JSch 无法在 Windows 上加载库 'c'

c - 无法通过linux串口读取