Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Script for remote batch crawling of Ubuntu PC information

Shulou Source: shulou.com Published: 2022-06-03 07:37:36 09月23日 Update

Scripts for remote batch crawling of Ubuntu PC information (multiple scripts are completed)

There are several points of knowledge:

A. Log in with password ssh and execute in batch

B, nc command to check whether the port is open

C, grep-w (exact matching of words)

D, grep-A (display the contents of the line after the matching line)

1. Select ip with open port 22 from all PC network segments.

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. Remote ssh login script with password (expect script)

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. Log in batch to the ip with open port 22 through for loop (save the output when executing, 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. Count the script of ssh login success 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 [: "] +'{cat $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#

The script for statistical results to excel is as follows:

First sort out the executed output (pick out the successful part and save it to another document):

# 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 | programs-v programs | grep-v grep | yes-v yes | | | 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 bin/sed.\ {5\} / /'/ log/PC/exp3_0.txt > / log/PC/exp5_0.txt

Capture the successful ip, hostname, and user name information to the excel document:

# 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 Compact.\ {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

Tags: Script login success port open information multiple password document statistics output precision host content command user user name knowledge knowledge point result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Shulou Tech Info MariaDB Shulou Information