如何使用Bash脚本在远程Linux服务器上运行多个命令

3
我正在编写以下脚本,该脚本登录到远程服务器并运行几个命令以验证服务器的性能,并根据这些命令的输出打印一条消息。但是SSH不起作用,返回的是托管脚本的服务器的状态。请参考以下代码:

脚本

#!/bin/bash
#######################
#Function to add hosts to the array
#the following function takes the ip addresses provided while the       script is run and stores them in an array
#######################
Host_storing_func () {
  HOST_array=()
  for i in $@ ;do
    HOST_array+=(${i});
  done
  #echo ${HOST_array[*]}
}

#######################
#Calling above function
#######################
Host_storing_func "$@"

############################################################
#Collect Stats of Ping,memory,iowait time test function
############################################################
b=`expr ${#HOST_array[*]} - 1 `
for i in `seq 0 $b` ;do
  sshpass -f /root/scripts/passwordFile.txt /usr/bin/ssh  student35@${HOST_array[${i}]}   <<   HERE
    echo `hostname`
    iowaittm=`sar 2 2|awk '/^Average/{print $5};'`
    if [ $iowaittm > 10 ];then
     echo "IO ==> BAD"
    else
     echo "IO ==> GOOD"
    fi
    memoryy=`free -m |grep Swap|awk '{if($2 == 0) print 0;else print (($4 /  $2 ) * 100)}'`
    if [ ${memoryy} < '10' ] ;then
     echo "memory ==> good"
    elif [[ "${memory}" -ge 0 ]] && [[ "${memory}" -le 10 ]];then
     echo "No Swap"
    else
     echo "memory ==> bad"`enter code here`
    fi
    ping -w2 -c2 `hostname` | grep "packet loss"|awk -F, '{print $3}'|awk -F% '{print $1}'|sed 's/^ *//'|awk '{if ($1 == 0) print "Yes" ;else print "No"}'
    HERE
done

输出: oc5610517603.XXX.com 是源服务器的名称

[root@oc5610517603 scripts]# ./big_exercise.sh 9.XXX.XXX.XXX 9.XXX.XXX.XXX
Pseudo-terminal will not be allocated because stdin is not a terminal.
oc5610517603.XXX.com 
IO ==> GOOD
No Swap
ping: oc5610517603.ibm.com: Name or service not known
Pseudo-terminal will not be allocated because stdin is not a terminal.
oc5610517603.XXX.com
IO ==> GOOD
No Swap
ping: oc5610517603.XXX.com: Name or service not known

2
https://www.shellcheck.net/ - Biffen
Host_storing_func 似乎不是必要的;只需使用 HOST_array=("$@") 替代即可。 - chepner
嗨Biffen,我按照你建议的更改了代码,确实有帮助,但是我该如何解决主要问题呢?因为脚本仍在源服务器上执行而不是远程服务器。[root@oc5610517603 scripts]# ./big_exercise.sh 9.XXX.XXX.XXX 9.XXX.XXX.XXX 伪终端将不会被分配,因为标准输入不是终端。 oc5610517603.XXX.com IO ==> GOOD 无交换 ping: oc5610517603.ibm.com:名称或服务未知 伪终端将不会被分配,因为标准输入不是终端。 oc5610517603.XXX.com IO ==> GOOD 无交换 ping: oc5610517603.XXX.com:名称或服务未知 - mourya nadella
2个回答

1
感谢您查看脚本,我找到了一种解决问题的方法。
问题出在sshpass命令上,如果您想在HEREdoc中使用变量,则只需将开头HERE放在单引号中,但如果变量在ssh之前计算,则不必将开头HERE放在单引号中。
更改sshpass命令后,我的脚本运行成功了。

太好了,今天学到了新东西!很高兴你解决了问题。祝一切顺利。 - Rakesh Gupta

0

我稍微修改了你的脚本。

如@chepner所建议,我没有使用Host_storing_func。 对于sshpaas来说,heredocs有些棘手。您必须在heredoc中转义每个反引号和$符号。

请注意heredoc开始前的-,它允许您缩进heredoc正文。此外,尽可能避免使用反引号。改用$(command)。

希望能帮到您。

#!/bin/bash
#######################
#Function to add hosts to the array
#the following function takes the ip addresses provided while the       script is run and stores them in an array
#######################
array=( "$@" )
user="student35"

############################################################
#Collect Stats of Ping,memory,iowait time test function
############################################################

for host in ${array[@]}; do
    sshpass -f /root/scripts/passwordFile.txt /usr/bin/ssh -l ${user} ${host}  <<-HERE
    thishost=\$(hostname)
    echo "Current Host -> \$thishost";
    iowaittm=\`sar 2 2|awk '/^Average/{print \$5}'\`
    if [ \$iowaittm > 10 ]; then
        echo "IO ==> BAD"
    else
        echo "IO ==> GOOD"
    fi
    memory=\$(free -m | grep Swap | awk '{if(\$2 == 0) print 0;else print ((\$4 /  \$2 ) * 100)}')
    if [ \${memory} < '10' ] ;then
        echo "memory ==> good"
    elif [[ "\${memory}" -ge 0 ]] && [[ "\${memory}" -le 10 ]]; then
        echo "No Swap"
    else
        echo "memory ==> bad"\`enter code here\`
    fi
    ping -w2 -c2 \`hostname\` | grep "packet loss"|awk -F, '{print \$3}'|awk -F% '{print \$1}'|sed 's/^ *//'|awk '{if (\$1 == 0) print "Yes" ;else print "No"}'
    HERE
done

1
你的脚本对我很有效,但是我对我的脚本进行了轻微的更改,这样它就可以工作了。 - mourya nadella

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接