近期在执行脚本时,遇到频繁登录,执行简单的查询脚本,因此做了一个简单初步的执行脚本

#!/bin/sh
function trapper(){
         trap 'exit 1' EXIT QUIT;
}
 
serverArr=(
        imc-master,172.29.129.110
         imc-slave,172.29.129.111
);
 
function printServers(){
        echo '===================================SERVERS===================================';
        for ((i=0; i< ${#serverArr[*]}; i++))
        do
                serverName=$(echo ${serverArr[$i]}|cut -d ',' -f 1);
                serverIP=$(echo ${serverArr[$i]}|cut -d ',' -f 2);
                echo -e "${i}\t${serverName} \t\t\t ${serverIP}";
        done
        echo '===================================++END++===================================';
}
 
function conn(){
        index=$1;
        for ((i=0; i<${#serverArr[*]}; i++))
        do
                if [ $index -eq $i ]
                then
                        serverIP=$(echo ${serverArr[$i]}|cut -d ',' -f 2);
                fi
        done;
        echo "Connecting to $serverIP";
        ssh usertest@$serverIP "/bin/bash < /home/usertest/command_imc.sh"
}
 
function main(){
        while true
        do
                trapper;
                clear;
                printServers;
                read -p '输入服务器编号' num;
                echo "sh /opt/iMC/deploy/procctl.sh -plist"
                conn $num;
        done;
}
main;

其中第35行是一个进程信息收集脚本放在要运行的主机上(就是110和111两台主机的本地路径)。

#!/bin/bash

格式化查询价结果的输出sh /opt/iMC/deploy/procctl.sh -plist(应用特定命令)
sh /opt/iMC/deploy/procctl.sh -plist |  awk '{split($0,a,"|");printf "%-10s%-20s%-10s%-10s%-6s%-15s%-20s%-12s%-8s\n", a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]}';
sleep 10 ;
exit;


执行结果展示:

shell登录+脚本执行_awk格式化

格式化命令使用的是awk,

awk '{split($0,a,"|");printf "%-10s%-20s%-10s%-10s%-6s%-15s%-20s%-12s%-8s\n", a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]}'