At the earliest time to do operation, the automation tools are not very hot, and the need for bulk execution will generally rely on pssh, sshpass+ scripts such as the implementation of the bulk of the demand. In 2012, there was a record of the SSH tool---sshpass:ssh support password parameters. Recently the company bought Aliyun platform, will be in a computer room Aliyun project. So many of the current main opportunity to migrate Ali platform up, at present many business modules are doing resource usage investigation, as the ITO Department, need to cooperate with the business part to obtain the use of resources. And before the host was ansible installed, the commercialization of the BMC in the acquisition of data is not comfortable, here on the use of Sshpass + shell way to achieve the memory usage of multiple hosts to do a statistic.
First, have the same user and password
The installation of Sshpass is no longer detailed here, as recorded in previous posts. The first scenario is that all hosts have a common user with the same password. In this case, a for loop basically solves the problem.
1. Main script
The main script is the script to be executed, the principle is the script will be executed first SCP to the remote host, and then through SSH remote execution of the command, after the completion of the implementation of the deletion of the program just executed. Using the Stricthostkeychecking=no parameter is an interactive hint that occurs when an SSH connection is avoided yes/no
[Root@361way sshpass]# Cat main.sh
#!/bin/bash
For host in ' Cat hosts ';d o
#echo $host Mem:
Sshpass-p ' password ' scp-o stricthostkeychecking=no mem.sh amos@ $host:/tmp/
Sshpass-p ' password ' ssh-o stricthostkeychecking=no amos@ $host "sh/tmp/mem.sh"
Sshpass-p ' password ' ssh-o stricthostkeychecking=no amos@ $host "rm-rf/tmp/mem.sh"
Done
2. What needs to be done
Here, for example, to obtain a storage usage. We need to get used memory (no buffer, cache), total memory, percent used, (used by +4g system)/Total memory usage. The specific script is as follows:
#!/bin/bash # Usedmem (KB) Totalmem (KB) used1_percent (no eredundance) used1_percent (redundance)
Used= ' free-m|grep ' buffers/cache ' |awk ' {print $ (NF-1)} '
used_redu= ' free-m|grep ' buffers/cache ' |awk ' {print $ (NF-1) +4096} '
Total= ' cat/proc/meminfo |grep memtotal |awk ' {print $2/1024} '
used_value=$ (echo $used $total | awk ' {printf '%0.2f\n ', $1/$2} ')
usedredu_value=$ (echo $used _redu $total | awk ' {printf '%0.2f\n ', $1/$2} ')
dcnip= '/sbin/ifconfig|grep inet|grep 10|awk ' {print $} '
echo "$dcnip | $used | $total | $used _value | $usedredu _value "
3, need to execute the host list
[Root@361way sshpass]# Cat hosts
10.211.160.64
10.211.160.65
10.211.160.66
10.211.160.67
10.211.160.68
10.211.160.69
10.211.160.70
10.211.138.18
10.211.138.19
When used, perform sh main.sh > Result.txt.
Two, different user name and password
This can be referenced in the storage automation before the use of the method (while loop + SSH + expect implementation), you can refer to the checktime.tar.gz in Http://github.com/361way/shell (The script was uploaded GitHub soon, but it was used earlier), and this is implemented using a while loop. The code is as follows:
1. main.sh Main Script
#!/bin/sh
while read ServerInfo; Todo
Sp=${serverinfo:0:1}
if [$sp = = ' # ']; Then
Continue
Fi
Ip1= ' echo $serverinfo | Awk-f ', ' {print $} '
User1= ' echo $serverinfo | Awk-f ', ' {print $} '
Passwd1= ' echo $serverinfo | Awk-f ', ' {print $} '
Sshpass-p "$PASSWD 1" scp-o stricthostkeychecking=no mem.sh $USER 1@ $IP 1:/tmp/
Sshpass-p "$PASSWD 1" ssh-o stricthostkeychecking=no $USER 1@ $IP 1 "sh/tmp/mem.sh"
Sshpass-p "$PASSWD 1" ssh-o stricthostkeychecking=no $USER 1@ $IP 1 "rm-rf/tmp/mem.sh"
Done < Serverinfo.list
2. Serverinfo.list Host information file
[Root@361way auto]# cat serverinfo.list #IP, Username,userpassword
10.144.229.181,admin,admin@test
10.144.229.182,root,root123
Third, summary
There is no innovation here, can not be laurels, take the first thing to do when the operation of the game to play, the script to carry out the disadvantage is single-threaded running, but it is based on the list to run one after the other, to facilitate the results of an abnormal comparison. Now if you want to chase a lightweight DIY automation tool, we recommend that you can refer to the ansilbe, so that there is Python + Paramiko module + multithreading way to achieve. But if the intranet environment, the installation of some automation tools, especially in the case of a lot of dependencies, it is difficult to do, you can try these lightweight gadgets.