2012-04-10 81 views
0

我需要關於如何通過shell腳本while循環中的ssh命令連接遠程系統的幫助。我能夠使用shell腳本中的ssh連接一個遠程系統。請找到示例代碼段爲如下......在shell腳本循環中需要關於ssh用法的幫助

ssh "[email protected]" ARG1=$rpmFileName 'bash -s' <<'ENDSSH'   
    echo ">>Checksum ..." 
    md5sum /root/$ARG1 
ENDSSH 

當試圖在循環中運行的同一段代碼得到錯誤「語法錯誤:意外的文件結尾」,這是我無法解析。

但是,當將另一個腳本文件中的同一段代碼放在另一個腳本文件的while循環中時,該腳本正在工作。

任何人都可以幫助我解決一些問題。


請發現整個代碼如下...

#!/bin/sh 
rpmFileName="" 
file="serverIps.txt" 
dir="/home/rtulluri/downloads/EVAT-1123/AxisTar"; 
numberOfIps=0 
axisTarfileTarget='/var' 
#This function checks whether given ip is valid or not 
#returns 0 for valid ip, 1 for invalid ip 
function valid_ip() 
{ 
    local ip=$1 
    local stat=1 
    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] 
    then 
     OIFS=$IFS 
     IFS='.' 
     ip=($ip) 
     IFS=$OIFS 
     [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ 
      && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] 
     stat=$? 
    fi 
    #echo "stat = $stat" 
    return $stat 
} 
#Check whether given file exists or not 
if [ -s $file ] 
then 
echo "$file exists" 
else 
    echo "$file doesn't exist" 
    echo "exiting ........" 
    exit 
fi 
IFS=, 
echo "---------------" 
while read sysType serverIp uid pwd 
do 

    sysType="${sysType#"${sysType%%[![:space:]]*}"}" # remove leading whitespace characters 
    sysType="${sysType%"${sysType##*[![:space:]]}"}" # remove trailing whitespace characters 
    serverIp="${serverIp#"${serverIp%%[![:space:]]*}"}" # remove leading whitespace characters 
    serverIp="${serverIp%"${serverIp##*[![:space:]]}"}" # remove trailing whitespace characters 

    uid="${uid#"${uid%%[![:space:]]*}"}" # remove leading whitespace characters 
    uid="${uid%"${uid##*[![:space:]]}"}" # remove trailing whitespace characters 

    pwd="${pwd#"${pwd%%[![:space:]]*}"}" # remove leading whitespace characters 
    pwd="${pwd%"${pwd##*[![:space:]]}"}" # remove trailing whitespace characters 

    if [ -n "$serverIp" ] 
    then 
    valid_ip $serverIp 
    #Assign the return value to a variable 
    isValidIp=$? 
    else 
     isValidIp=1 
    fi 

    if [ $isValidIp -eq "0" ] 
    then 
     numberOfIps=$(($numberOfIps + 1)) 
     echo "$numberOfIps) $serverIp --> is valid"  

     if [ "$sysType" = "ebox" ] 
     then 
       echo "$serverIp is an eBox device.." 
      echo "About to pass $serverIp as argument to connct.sh" 
      #./connct.sh $serverIp 
      ssh "$address" ARG1=$rpmFileName 'bash -s' <<'ENDSSH' 
     echo ">>Checksum ..." 
      ENDSSH 
     fi 
    else 
     echo "$serverIp --> is invalid" 
    fi  
    echo "" 
done < $file 
+1

你能發佈沒有工作的代碼,所以我們可以幫你調試它? – 2012-04-10 13:17:49

+0

並將它放在一個代碼塊中(通過縮進四個空格),以便更易於閱讀。 – 2012-04-10 13:18:34

+1

你在'ENDSSH'之前加了空格嗎?你能顯示while循環嗎? – bmk 2012-04-10 13:21:00

回答

0

您可以嘗試使用 '-n' SSH開關

如:

for i in {1..7} 
do 
    ssh -n myhost$i mycommand 
done