我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

远程批量抓取UbuntuPC信息的脚本

远程批量抓取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#

 

 

统计结果到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


本文名称:远程批量抓取UbuntuPC信息的脚本
网页网址:http://mswzjz.cn/article/gchijp.html

其他资讯