2013-03-20 253 views
0


我嘗試製作一個腳本來自動執行我的實驗。
我的目的是從一個shell腳本,在服務器上運行另一個腳本運行如下命令:
在腳本中執行ssh命令

ssh [email protected] 'sh ControlIperfServer.sh start' 

即使文件ControlIperfServer.sh已經放置在服務器的/ root目錄。 COM但是從端子的輸出是:

bash: sh ControlIperfServer.sh start: command not found 

雖然我能夠當我在終端鍵入以運行該命令。從腳本執行它時,它只能執行。我的意思是:
當我在終端鍵入命令。有用。

[[email protected] ~]# ssh [email protected] 'sh ControlIperfServer.sh start' 
Running IPERF SERVER 
[[email protected] ~]# 

當我將該命令放入腳本並執行腳本時。這是行不通的。

[[email protected] ~]# ./Runtest.sh 
bash: sh ControlIperfServer.sh start: command not found 
[[email protected] ~]# 

我使用的詳細-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 [email protected] 
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 [email protected] 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 [email protected]$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」主機上的所有腳本文件。

+2

Runtest.sh是什麼樣的?這聽起來像你可能在某處過分引用了命令名。當我做一個測試命令時,我看到'debug1:發送命令:echo foo'(注意缺少引號)。 – chepner 2013-03-20 16:52:02

+0

@chepner:你應該提供這個答案。這很可能是這種情況。 – Dolda2000 2013-03-20 17:06:56

+0

這是原因,但我不知道足夠的細節來指定真正的解決方案。 – chepner 2013-03-20 17:08:28

回答

4

CMD="ssh -v [email protected]$REMOTE_SERVER \"$RUN_IPERF_SERVER\"" 

是一個壞主意。嵌入在CMD中的引號被視爲文字引號;它們不僅僅用於保護RUN_IPERF_SERVER中的空格。把一個命令的名字放在一個變量中是可以的,但它的參數應該保持分開。如果您正在使用bash(或支持數組一些其他的殼),我建議

CMD=ssh 
CMD_OPTIONS=(-v [email protected]_SERVER "$RUN_IPERF_SERVER") 
$CMD "${CMD_OPTIONS[@]}" 

如果你不能使用數組,你可能回落到使用eval

CMD="ssh -v [email protected]$REMOTE_SERVER \"$RUN_IPERF_SERVER\"" 
eval "$CMD" 

,但我會推薦這

CMD=ssh 
CMD_OPTIONS="-v [email protected]_SERVER" # Note no whitespace to protect 
$CMD $CMD_OPTIONS "$REMOTE_IPERF_SERVER" 
+0

它像一個魅力。非常感謝@chepner! – 2013-03-21 08:27:44

+0

我還有一個問題。每次我通過ssh發送命令時,是否需要發送「退出」命令?謝謝你回答我! – 2013-03-21 09:05:45

+0

否;如果你發送一個明確的命令,一旦該命令完成,'ssh'就會終止。實際上,即使你不指定命令也是如此;一個交互式shell就會啓動,一旦該shell完成(即註銷),'ssh'就會終止。 – chepner 2013-03-21 12:30:09

0

看起來像是認爲'sh ControlIperfServer.sh start'是一個命令。一個文件。不是帶參數的文件。 嘗試像\'逃脫'來解決。

也使用./ControlIperfServer.sh或/root/ControlIperfServer.sh。 根的外殼不應該有「。」。在出於安全原因的道路上,所以不要指望它在那裏。

-2

給/ bin/sh試一下,而不是sh。如果您從腳本調用ssh,則可能不會加載別名。