远程批量抓取Ubuntu PC信息的脚本(多个脚本分工完成)
有多个知识点:
a、 带密码ssh登录并批量执行
b、 nc命令检查端口是否开放
c、 grep –w (对词精确匹配)
d、 grep –A (显示匹配行之后行的内容)
1、 从所有PC网段里筛选出有开放22端口的ip
root@archivebak:~# cat/usr/local/bin/pingubt.sh
#!/bin/bash
rm -rf /log/ip_ubt.txt
rm -rf /log/ssh_22_0_ok.txt
rm -rf /log/ssh_22_0.txt
touch /log/ssh_22_0_ok.txt
touch /log/ssh_22_0.txt
rm -rf /log/ssh_22_1_ok.txt
rm -rf /log/ssh_22_1.txt
touch /log/ssh_22_1_ok.txt
touch /log/ssh_22_1.txt
rm -rf /log/ssh_22_29_ok.txt
rm -rf /log/ssh_22_29.txt
touch /log/ssh_22_29_ok.txt
touch /log/ssh_22_29.txt
Net1="10 11 12 13 "
Net2="10 11 12 13 "
Net3="10 11 12 13"
for Net in $Net1
do
for inum in `seq 1 253`
do
IPP="10.0.$Net.$inum 22"
IP="10.0.$Net.$inum"
/bin/nc -v -w 5 -z $IPP > /dev/null2>&1
if [ $? -eq 0 ];then
echo $IPP:ok >> /log/ssh_22_0_ok.txt
echo $IP >> /log/ssh_22_0.txt
fi
done
done
for Net in $Net2
do
for inum in `seq 1 253`
do
IPP="10.1.$Net.$inum 22"
IP="10.1.$Net.$inum"
/bin/nc -v -w 5 -z $IPP > /dev/null2>&1
if [ $? -eq 0 ];then
echo $IPP:ok >> /log/ssh_22_1_ok.txt
echo $IP >> /log/ssh_22_1.txt
fi
done
done
for Net in $Net3
do
for inum in `seq 1 253`
do
IPP="10.29.$Net.$inum 22"
IP="10.29.$Net.$inum"
/bin/nc -v -w 5 -z $IPP > /dev/null2>&1
if [ $? -eq 0 ];then
echo $IPP:ok >> /log/ssh_22_29_ok.txt
echo $IP >> /log/ssh_22_29.txt
fi
done
done
2、远程带密码ssh登录脚本(expect脚本)
root@archivebak:~/pcu# cat/usr/local/bin/ssh_PC.exp
#!/usr/bin/expexct -f
set ip [lindex $argv 0]
set timeout 5
spawn ssh ming\\zhi.yang@$ip
expect {
"*(yes/no)*" { send "yes\r"; exp_continue}
"Password:" { send "****\r" }
}
expect "$*"
send "ifconfig | grep addr:10 | cut -b16-32\r"
send "hostname\r"
send "who | grep ming | head -n1\r"
send "cat /etc/issue\r"
send "exit\r"
expect eof
root@archivebak:~/pcu#
3、通过for循环对开放22端口的ip进行批量登录(执行时须将输出进行保存, sh host.sh&>> /log/pcu/users_0.txt)
root@archivebak:~/pcu# cat hosts.sh
#!/bin/bash
for IP in `cat /log/ssh_22_0.txt`
do
/usr/bin/expect /usr/local/bin/ssh_PC.exp$IP
done
root@archivebak:~/pcu#
4、统计ssh登录成功ip的脚本
root@archivebak:~/pcu# cat ssh_22_0_no.sh
#!/bin/bash
rm -rf /log/pcu/ubt_ip_0.txt
rm -rf /log/pcu/ubt_ip_0_no.txt
touch /log/pcu/ubt_ip_0.txt
touch /log/pcu/ubt_ip_0_no.txt
for IP in `cat /log/ssh_22_0.txt`
do
Co=`cat /log/users_0_bak.txt | grep -w $IP| tail -n 1 | awk -F[:" "]+ '{print $3}'`
if [ $Co = $IP ];then
echo $IP >> /log/pcu/ubt_ip_0.txt
else
echo $IP >> /log/pcu/ubt_ip_0_no.txt
fi
done
root@archivebak:~/pcu#
5、从成功登录输出里抓取需要的主机信息的脚本
root@archivebak:~/pcu# cat hosts2_0.sh
#!/bin/bash
#cat /log/users.txt | grep addr:10. |grep -v ifconfig
/bin/rm -rf /log/pcu/ubt_0_lo.txt
/bin/touch /log/pcu/ubt_0_lo.txt
for IP in `cat /log/pcu/ubt_ip_0_lo.txt`
do
Host=`cat /log/pcu/users_0_lo.txt | grep -w$IP -A 4 | grep sh0 | grep -v who | grep -v hostname | grep -v ifconfig | grep-v login | cut -b 1-20`
Hostt=`cat /log/pcu/users_0_lo.txt | grep-w $IP -A 4 | grep ubtpc | grep -v who | grep -v hostname | grep -v ifconfig |grep -v login | cut -b 1-20`
User=`cat /log/pcu/users_0_lo.txt | grep -w$IP -A 4 | grep '201' | awk -F[:" "]+ '{print $1}'`
Ub=`cat /log/pcu/users_0_lo.txt | grep -w$IP -A 4 | grep Welcome | head -n 1 | awk -F[:" "]+ '{print $4}'`
echo "$IP,$Host,$Hostt,Ubuntu$Ub,$User" >> /log/pcu/ubt_0_lo.txt
done
root@archivebak:~/pcu#
本文转自linux博客51CTO博客,原文链接http://blog.51cto.com/yangzhiming/1902770如需转载请自行联系原作者
yangzhimingg