如何批量获取服务器的信息?

有时我们需要批量获取服务器的信息,难道只能一台一台登录上去找吗?

这里需要介绍一个神器 sshpass, sshpass 可以使用 ssh 登录服务器并且做一些操作,下面直接呈上脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

instances=`cat hosts`
username=test_username
password=test_password

for instance in ${instances[*]}; do
echo ${instance}
echo ${instance} >> result.txt

ping_result=`ping -w 2 -c 3 ${instance} | grep packet | awk -F" " '{print $6}'| awk -F"%" '{print $1}'| awk -F' ' '{print $1}'`
if [[ $ping_result -eq 0 ]]; then
os_info=$(sshpass -p ${password} ssh -o StrictHostKeyChecking=no -l ${username} ${instance} 'cat /etc/redhat-release')
echo ${os_info} >> result.txt
tomcat_info=$(sshpass -p ${password} ssh -o StrictHostKeyChecking=no -l ${username} ${instance} 'ps aux | grep tomcat')
tomcat_info=$(echo ${tomcat_info} | grep -Eo "file=.*conf")
tomcat_info=$(echo ${tomcat_info/file=/})
tomcat_info=$(echo ${tomcat_info/\/conf/})
echo ${tomcat_info}
# echo ${tomcat_info} >> result.txt
version_info=$(sshpass -p ${password} ssh -o StrictHostKeyChecking=no -l ${username} ${instance} "${tomcat_info}/bin/version.sh")
echo ${version_info} | grep -Eo "Server version:.*?Server"
echo ${version_info} | grep -Eo "Server version:.*?Server" >> result.txt
echo "------" >> result.txt
fi
done
hosts
1
2
1.2.3.4
2.3.4.5

在脚本中,我们获取了服务器信息,以及 Tomcat 的版本信息。


标题Title
作者末日没有进行曲
链接link
时间:2020-02-20
声明:本博客所有文章均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×