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

时间:2018-04-02 17:16:44

标签: bash

我目前正在编写以下脚本,该脚本登录到远程服务器并运行几个命令来验证服务器的性能,并根据这些命令的输出打印消息。但是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 个答案:

答案 0 :(得分:1)

感谢您检查脚本,我找到了解决问题的方法

导致问题的是sshpass命令,如果你想在HEREdoc中使用变量,你只需要在单引号中打开HERE,但如果在ssh之前计算变量那么你就没有了用单引号打开HERE

sshpass -f /root/scripts/passwordFile.txt / usr / bin / ssh -T student35 @ $ {i}&lt;&lt; &#39;该处&#39;

在我更改sshpass命令后,我的脚本工作

答案 1 :(得分:0)

我稍微修改了你的脚本。

正如@chepner所建议的,我没有使用Host_storing_func。 对于sshpaas的Heredocs有些棘手。你必须逃避heredoc的所有后退和$符号。

注意 - 在heredoc开始之前,它允许你缩进heredoc体。此外,尽可能避免反向滴答。改为使用$(命令)。

希望它有所帮助。

#!/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