Bash 需要关于shell脚本循环中ssh使用的帮助吗
我需要关于如何在shell脚本的while循环中通过ssh命令连接远程系统的帮助。我能够从shell脚本使用ssh连接一个远程系统。请查找下面给出的示例代码段Bash 需要关于shell脚本循环中ssh使用的帮助吗,bash,Bash,我需要关于如何在shell脚本的while循环中通过ssh命令连接远程系统的帮助。我能够从shell脚本使用ssh连接一个远程系统。请查找下面给出的示例代码段 ssh "root@148.147.179.100" ARG1=$rpmFileName 'bash -s' <<'ENDSSH' echo ">>Checksum ..." md5sum /root/$ARG1 ENDSSH 当尝试在循环中运行同一段代码时,得到错误语法错误:意外的文件结
ssh "root@148.147.179.100" 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
您可以尝试使用'-n'ssh开关 e、 g:
你能发布不起作用的代码,这样我们可以帮助你调试它吗?并通过缩进四个空格将其放入代码块中,以便更容易阅读。你在ENDSSH之前添加了空格吗?可以显示while循环吗?这里有一个猜测:如果您的循环类似于从文件读取IP地址的while循环,那么您就有一个冲突:read和ssh命令都希望使用stdin。但是正如@Adam所说,我们需要查看代码。@glennjackman-在这种情况下,您的循环只运行一次,因为ssh将吃掉其余的stdin,而不是抛出语法错误。
for i in {1..7}
do
ssh -n myhost$i mycommand
done