需求:在实施和运维工作中有时候会因为一些特殊原因需要整理服务器台账信息,很多时候没有厂商提供的信息,只能自行整理,但一台台查看太浪费时间,所以整理一个脚本以便不时之需
脚本原理,
1、设置数组变量,并指定服务器ip列表
#需要检查的服务器列表 IP@@ssh端口@@用户名@@密码
serverInfo=(
192.168.195.129@@22@@root@@dragon
192.168.195.130@@22@@root@@dragon
)
2、利用expect自动交付远程到服务器执行命令生成临时文件
3、取回临时文件,解析各项参数
脚本执行结果
======================192.168.195.129服务器信息==============================
【硬件厂商】
[ 0.000000] DMI: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/20/2014
【系统内核】
3.10.0-1160.el7.x86_64
【系统版本】
CentOS Linux release 7.9.2009 (Core)
【CPU 信息】
c p u 型号: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
物理核心数:1
虚拟核心数:2
线 程 数:2
【内存信息】
total used free shared buff/cache available
Mem: 3.7G 1.1G 1.6G 12M 1.0G 2.3G
Swap: 2.0G 0B 2.0G
【存储信息】
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 6.0G 12G 36% /
/dev/sda1 1014M 185M 830M 19% /boot
【网络设置】
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.62.53 netmask 255.255.255.0 broadcast 192.168.62.255
inet6 fe80::2dc7:ef72:3800:2cbe prefixlen 64 scopeid 0x20<link>
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.195.129 netmask 255.255.255.0 broadcast 192.168.195.255
inet6 fe80::75a7:b9c9:bed7:a43d prefixlen 64 scopeid 0x20<link>
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
======================192.168.195.130服务器信息==============================
【硬件厂商】
[ 0.000000] DMI: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/20/2014
【系统内核】
3.10.0-1160.el7.x86_64
【系统版本】
CentOS Linux release 7.9.2009 (Core)
【CPU 信息】
c p u 型号: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
物理核心数:1
虚拟核心数:2
线 程 数:2
【内存信息】
total used free shared buff/cache available
Mem: 3.7G 822M 1.6G 84M 1.3G 2.6G
Swap: 2.0G 0B 2.0G
【存储信息】
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 5.2G 12G 31% /
/dev/sda1 1014M 185M 830M 19% /boot
【网络设置】
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.62.54 netmask 255.255.255.0 broadcast 192.168.62.255
inet6 fe80::3995:a7f4:ef41:ff98 prefixlen 64 scopeid 0x20<link>
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.195.130 netmask 255.255.255.0 broadcast 192.168.195.255
inet6 fe80::75a7:b9c9:bed7:a43d prefixlen 64 scopeid 0x20<link>
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
?shell脚本
理论上适用于linux x86,脚本运行需要安装expect(依赖tcl),请自行安装,或者下载完整脚本包
下载地址:批量获取linux服务器厂商/cpu/内存/磁盘/网卡等信息-Linux文档类资源-CSDN文库
#! /bin/bash
#====================参数区域 start====================
#需要检查的服务器列表 IP@@ssh端口@@用户名@@密码
serverInfo=(
192.168.195.129@@22@@root@@dragon
192.168.195.130@@22@@root@@dragon
)
#当前服务器root密码
mastServerPasswd='dragon'
#====================参数区域 end======================
echo '========================================开始校验自动人机交互所需rpm包=========================================================='
#校验expect
expectJy=`rpm -qa|grep expect`
if [ -z ${expectJy} ];then
echo 'expect未安装,即执行将安装'
rpm -ivh ./expect-5.45-14.el7_1.x86_64.rpm --nodeps >/etc/null
echo "完成"
else
echo '已存在expect,跳过安装,当前版本号:'`rpm -qa|grep expect`
fi
#校验tcl
tclJy=`rpm -qa|grep expect`
if [ -z ${tclJy} ];then
echo 'tcl未安装,即执行将安装'
rpm -ivh ./tcl-8.5.13-8.el7.x86_64.rpm --nodeps >/etc/null
echo "完成"
else
echo '已存在tcl,跳过安装,当前版本号:'`rpm -qa|grep tcl`
fi
echo -e '\n===============================================开始循环生成记录文件============================================================'
for(( i=0;i<${#serverInfo[@]};i++)); do
ServerIp=`echo ${serverInfo[i]}|awk -F@@ '{print $1}'`;
ServerPort=`echo ${serverInfo[i]}|awk -F@@ '{print $2}'`;
ServerUser=`echo ${serverInfo[i]}|awk -F@@ '{print $3}'`;
ServerPasswd=`echo ${serverInfo[i]}|awk -F@@ '{print $4}'`;
echo -e "远程到${ServerIp}生成记录文件"
(
/usr/bin/expect <<EOF
set timeout 30
spawn ssh -p${ServerPort} ${ServerUser}@${ServerIp}
expect {
"yes" { send "yes\n"
expect "*assword:" { send "${ServerPasswd}\n" }}
"*assword:" { send "${ServerPasswd}\n" }
timeout { send_error "连接到${ServerIp}服务器超时,请检查网络环境或延长超时时间\n"; exit 1; }
}
expect "]#"
send "uname -a >/tmp/${ServerIp}\r"
send "cat /etc/redhat-release >>/tmp/${ServerIp}\n"
send "grep 'DMI' /var/log/dmesg >>/tmp/${ServerIp}\n"
send "free -h >>/tmp/${ServerIp}\r"
send "df -h >>/tmp/${ServerIp}\r"
send "grep -i 'core id' /proc/cpuinfo >>/tmp/${ServerIp}\r"
send "grep -i 'processor' /proc/cpuinfo >>/tmp/${ServerIp}\r"
send "grep -i 'physical id' /proc/cpuinfo >>/tmp/${ServerIp}\r"
send "cat /proc/cpuinfo |grep 'model name'>>/tmp/${ServerIp}\r"
send "ifconfig >>/tmp/${ServerIp}\r"
send "exit\r"
expect eof
EOF
) >/etc/null
echo -e "完成\n复制记录文件到mast主机"
(
/usr/bin/expect <<EOF
spawn scp -q -r -P${ServerPort} ${ServerUser}@${ServerIp}:/tmp/${ServerIp} /tmp/
expect {
"yes/no" { send "yes\n"; exp_continue }
"password" { send "${mastServerPasswd}\n" }
}
expect eof
EOF
)>/etc/null
echo -e "完成\n"
done;
echo -e "\n\n\n\n\n\n\n"
echo -e "============================================读取收到的记录文件并打印结果======================================================="
for(( i=0;i<${#serverInfo[@]};i++)); do
ServerIp=`echo ${serverInfo[i]}|awk -F@@ '{print $1}'`;
echo -e "======================${ServerIp}服务器信息=============================="
echo -e "【硬件厂商】\n`cat /tmp/${ServerIp}|awk 'NR==3'`"
echo -e "\n【系统内核】\n`cat /tmp/${ServerIp}|awk 'NR==1'|awk '{print $3}'`"
echo -e "\n【系统版本】\n`cat /tmp/${ServerIp}|awk 'NR==2'`"
echo -e "\n【CPU 信息】"
echo -e "c p u 型号:`grep 'model name' /tmp/192.168.195.130 |head -1|awk -F: '{print $2}'`"
echo -e "物理核心数:`grep -i 'core id' /tmp/${ServerIp}|sort -u|wc -l`"
echo -e "虚拟核心数:`grep -i 'processor' /tmp/${ServerIp}|sort -u|wc -l`"
echo -e "线 程 数:`grep -i 'physical id' /tmp/${ServerIp}|sort -u|wc -l`"
echo -e "\n【内存信息】"
cat /tmp/${ServerIp}|grep -i -E '^mem|^swap|total'
echo -e "\n【存储信息】"
cat /tmp/${ServerIp}|grep -i -E '^文件系统|^filesystem|^/dev'
echo -e "\n【网络设置】\n`grep -i -E 'inet|flags' /tmp/${ServerIp}`"
echo -e "\n\n\n"
done;
|