远程批量抓取UbuntuPC信息的脚本-创新互联
远程批量抓取Ubuntu PC信息的脚本(多个脚本分工完成)
成都创新互联-专业网站定制、快速模板网站建设、高性价比西塞山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式西塞山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖西塞山地区。费用合理售后完善,10余年实体公司更值得信赖。有多个知识点:
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#
统计结果到excel的脚本如下:
先对执行的输出进行下整理(挑出成功部分存到另1文档):
# cat grep_0.sh
#!/bin/bash
/bin/rm -rf /log/PC/exp2_0.txt
/bin/touch /log/PC/exp2_0.txt
for IP in `cat /log/ssh_22_0.txt`
do
/bin/cat /log/PC/exp_0.txt | grep -w $IP -A 10 | grep -v spawn | grep -v Wel | grep -v Pas | grep -v Conn | grep -v Last| grep -v https | grep -v updated | grep -v updates | grep -v exit | grep -v Unable | grep -v Received | grep -v Too | grep -v Please | grep -v Warning | grep -v ECDSA | grep -v programs | grep -v yes| grep -v Ubuntu | grep -v files | grep -v terms | grep -v The | grep -v denied >> /log/PC/exp2_0.txt
done
/bin/cat /log/PC/exp2_0.txt | grep addr:10. | grep -v ifconfig | uniq > /log/PC/exp3_0.txt
/bin/sed 's/[[:space:]]//g' /log/PC/exp3_0.txt > /log/PC/exp4_0.txt
/bin/sed 's/.\{5\}//' /log/PC/exp3_0.txt > /log/PC/exp5_0.txt
抓出成功的ip、主机名、用户名信息到excel文档中:
# cat csv_0.sh
#!/bin/bash
/bin/rm -rf /log/PC/csv_0.txt
/bin/touch /log/PC/csv_0.txt
for IP in `cat /log/PC/exp4_0.txt`
do
IPP=`echo $IP | sed 's/.\{5\}//'`
Host=`/bin/cat /log/PC/exp2_0.txt | grep -w $IP -A 10 | grep sh | grep -v @ | head -n 1`
User=`/bin/cat /log/PC/exp2_0.txt | grep -w $IP -A 10 | grep :0 | awk -F[:" "]+ '{print $1}' | head -n 1`
/bin/echo "$IPP,$Host,$User" >> /log/PC/csv_0.txt
done
/bin/sed 's/[[:space:]]//g' /log/PC/csv_0.txt > /log/PC/csv2_0.txt
/bin/cat /log/PC/csv2_0.txt | tr -s '[:blank:]'',' > /log/PC/csv_0.csv
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:远程批量抓取UbuntuPC信息的脚本-创新互联
分享路径:http://ybzwz.com/article/cosegh.html