抓取指定IP所在端口脚本

抓取指定IP所在端口脚本

此脚本需要三个文件,一个是.sh的文件,二个是.exp的文件,针对cisco和华三交换机做了两个脚本。还有一个脚本是抓取整个机房服务器IP、服务器mac、服务器所在交换机IPod、服务器所在交换机端口的脚本。

1、华三交换机相关脚本

#!/bin/bash

echo -e "\033[31m 输入一个IP地址关闭端口命令脚本

作者:王阳

编写日期:2013/09/24

注:本脚本还有待优化(交换机是华三的交换机,如是其它厂家则不行,本脚本起到抛砖引玉的作用,希望网友朋友多加改进!!!!!!\033[0m"

echo ""

read -p "请输入IP:" i

cat /var/wangyang/block/switchlist.txt | while read IP LOGIN PASSWD

do

/var/wangyang/block/check.exp ${IP} ${LOGIN} ${PASSWD} ${i} > /var/wangyang/block/core.txt

done

port=`cat core.txt |grep GE |awk '{print $4}'|sed 'y/GE/gi/'`

echo -e "\033[05m 核心交换机端口是 $port \033[0m"

cat /var/wangyang/block/switchlist.txt | while read IP LOGIN PASSWD

do

/var/wangyang/block/access.exp ${IP} ${LOGIN} ${PASSWD} ${port} > /var/wangyang/block/port.txt

done

acip=`cat port.txt |grep 1000 |awk '{print $1}'|grep -v JY`

echo -e "\033[05m 接入交换机 $acip \033[0m"

echo "$acipadminadmin" > /var/wangyang/block/access.txt

cat /var/wangyang/block/access.txt | while read IP LOGIN PASSWD

do

/var/wangyang/block/shutdown.exp ${IP} ${LOGIN} ${PASSWD} ${i} > /var/wangyang/block/shutdown.txt

done

acport=`cat /var/wangyang/block/shutdown.txt |grep GE|awk '{print $4}'|sed 'y/GE/gi/'`

echo -e "\033[05m 接入交换机所连端口 $acport \033[0m"

cat /var/wangyang/block/access.txt | while read IP LOGIN PASSWD

do

/var/wangyang/block/shutdownport.exp ${IP} ${LOGIN} ${PASSWD} ${acport} > /var/wangyang/block/shutdownport.txt

done

log="error"

bao=`cat /var/wangyang/block/shutdownport.txt`

if [ $log = $bao ]

then

echo "本次抓取指这定端口有问题,需要手动抓取."

else

echo "抓取完成!!!"

echo "$i $acportyes" >> /var/wangyang/block/complete.txt

fi

cat /var/wangyang/block/complete.txt

check.exp该脚本主要是查出指定IP所在交换机中的mac地址,内容如下:

#!/usr/bin/expect -f

if { $argc != 4 } {

send_user "error\n"

exit

}

set timeout 30

set IP [lindex $argv 0]

set LOGIN [lindex $argv 1]

set PASSWD [lindex $argv 2]

set i [lindex $argv 3]


spawn telnet

expect "telnet> "

send "open $IP\r"

sleep 1

expect "Username:"

sleep 1

send "$LOGIN\r"

sleep 1

expect "Password:"

sleep 1

send "$PASSWD\r"

sleep 1

expect "*>"

sleep 1

send "sys\r"

sleep 1

expect "*]"

sleep 1

send "dis arp | include $i\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*>"

sleep 1

send "quit\r"

access.exp主要是抓取出连接二层交换机IP,内容如下:

#!/usr/bin/expect -f

if { $argc != 4 } {

send_user "error\n"

exit

}

set timeout 30

set IP [lindex $argv 0]

set LOGIN [lindex $argv 1]

set PASSWD [lindex $argv 2]

set port [lindex $argv 3]


spawn telnet

expect "telnet> "

send "open $IP\r"

sleep 1

expect "Username:"

sleep 1

send "$LOGIN\r"

sleep 1

expect "Password:"

sleep 1

send "$PASSWD\r"

sleep 1

expect "*>"

sleep 1

send "sys\r"

sleep 1

expect "*]"

sleep 1

send "dis arp int $port | include 1000\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*>"

sleep 1

send "quit\r"

shutdown.exp内容如下:

#!/usr/bin/expect -f

if { $argc != 4 } {

send_user "error\n"

exit

}

set timeout 30

set IP [lindex $argv 0]

set LOGIN [lindex $argv 1]

set PASSWD [lindex $argv 2]

set i [lindex $argv 3]


spawn telnet

expect "telnet> "

send "open $IP\r"

sleep 1

expect "Username:"

sleep 1

send "$LOGIN\r"

sleep 1

expect "Password:"

sleep 1

send "$PASSWD\r"

sleep 1

expect "*>"

sleep 1

send "sys\r"

sleep 1

expect "*]"

sleep 1

send "dis arp | include $i\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*>"

sleep 1

send "quit\r"

shutdownport.exp内容如下:

#!/usr/bin/expect -f

if { $argc != 4 } {

send_user "error\n"

exit

}

set timeout 30

set IP [lindex $argv 0]

set LOGIN [lindex $argv 1]

set PASSWD [lindex $argv 2]

set acport [lindex $argv 3]


spawn telnet

expect "telnet> "

send "open $IP\r"

sleep 1

expect "Username:"

sleep 1

send "$LOGIN\r"

sleep 1

expect "Password:"

sleep 1

send "$PASSWD\r"

sleep 1

expect "*>"

sleep 1

send "sys\r"

sleep 1

expect "*]"

sleep 1

send "in $acport\r"

sleep 1

expect "*]"

sleep 1

send "dis this\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*>"

sleep 1

send "quit\r"

switch.exp内容如下:

#!/usr/bin/expect -f

if { $argc != 4 } {

send_user "error\n"

exit

}

set timeout 30

set IP [lindex $argv 0]

set LOGIN [lindex $argv 1]

set PASSWD [lindex $argv 2]

set PORTHAO [lindex $argv 3]


spawn telnet

expect "telnet> "

send "open $IP\r"

sleep 1

expect "Username:"

sleep 1

send "$LOGIN\r"

sleep 1

expect "Password:"

sleep 1

send "$PASSWD\r"

sleep 1

expect "*>"

sleep 1

send "sys\r"

sleep 1

expect "*]"

sleep 1

send "in gi1/0/$PORTHAO\r"

sleep 1

expect "*]"

sleep 1

send "packet-filter 3000 inbound\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*]"

sleep 1

send "quit\r"

sleep 1

expect "*>"

sleep 1

send "quit\r"

我在这里只写出华三交换机的范例,cisco的我就不写了,原理是一样的,只是有些命令不一样而以,我把相关脚本都上传到附件里,大家有什么不明白的可以邮件给我[email protected]

你可能感兴趣的:(服务器,朋友,IP地址,交换机)