modify_VLAN_for_clients_PC

[root@ChrisTest modify_VLAN_for_clients_PC]# ls
domodify.sh main_doshowall.sh test.sh
doshow.sh SWlistwithHostname.txt worklog_2018-12-31_10:27:51
[root@ChrisTest modify_VLAN_for_clients_PC]# cat main_doshowall.sh
#!/bin/sh

#Define Variable
LIST=./SWlistwithHostname.txt
DATE=date '+%Y-%m-%d_%H:%M:%S'

#Create Worklog Folder
mkdir ./worklog_$DATE

#Ask for Hostname and PortNumber
echo ' '
read -p 'Pls input the Switch Hostname(No need to input SH): ' HOSTNAME
read -p 'Pls input the Switch Port(The Last Num Only): ' SWITCHPORT
read -p 'Pls input the Switch Port VLAN(The vlan Num Only): ' VLAN

#Recursive the IP addresses in SWList to run the Script doshow.sh
#for TARGET in cat $LIST
#do
#echo $TARGET
#./doshow.sh $TARGET "$REPLY" | tee ./worklog_$DATE/$TARGET.log
#done

#Get Switch IP from Hostname
SwitchIP=cat $LIST | grep SH"$HOSTNAME" | cut -f2

#Run the show command script
./doshow.sh "$SwitchIP" "g1/0/$SWITCHPORT" "1/0/$SWITCHPORT" | tee ./worklog_$DATE/Result.log

CURRENTVLAN=cat worklog_$DATE/Result.log | grep vlan | cut -d" " -f5
portstatus=cat worklog_$DATE/Result.log | grep unset | awk '{print $5,$6}'
macaddripphone=cat worklog_$DATE/Result.log | grep 706 | awk '{print $2}'
#Add if cluase just to avoid error when standalone PC without IP Phone
if [ -z $macaddripphone ]; then
macaddrcomputer=cat worklog_$DATE/Result.log | grep DYNA | awk '{print $2}'
else
macaddrcomputer=cat worklog_$DATE/Result.log | grep DYNA | awk '{print $2}' | grep -v $macaddripphone
fi

echo ' '
echo ' '
echo ' '
echo ' '
echo ' '

################Export All Results################

echo ' '
echo ' '
echo ' '
echo ' '

扫描二维码关注公众号,回复: 4757865 查看本文章

echo '############################################'
echo 'Switch:' SH"$HOSTNAME"
echo 'Switch IP:' "$SwitchIP"
echo 'SwitchPort:' "G1/0/$SWITCHPORT"
echo 'Current Vlan(Computer):' $CURRENTVLAN
echo 'MAC address(IP Phone):' $macaddripphone
echo 'MAC address(Computer):' $macaddrcomputer
echo 'Port Status:' $portstatus
echo '############################################'

################Export All Results################

#Result correct or wrong
echo -n 'Is this correct for your client PC to change VLAN?(y or n): '
read yesorno
case $yesorno in
yes | y)
echo "Correct"
./domodify.sh "$SwitchIP" "g1/0/$SWITCHPORT" "1/0/$SWITCHPORT" "$VLAN" | tee ./worklog_$DATE/ResultModify.log;;
no | n)
echo 'False'
exit 1;;
*) echo 'Pls input y or n';;
esac

SHOWVLAN=cat ./worklog_$DATE/ResultModify.log | grep vlan | grep -v "#" | cut -d" " -f5

################Export All Results################

echo ' '
echo ' '
echo ' '
echo ' '

echo '############################################'
echo ' '
echo Now the VLAN of G1/0/$SWITCHPORT is':' $SHOWVLAN
echo ' '
echo '############################################'

################Export All Results################

exit 1

[root@ChrisTest modify_VLAN_for_clients_PC]# cat doshow.sh
#!/usr/bin/expect -f

##Define Variables
#Define Timeout
set time 10

#Define Password
set passwd "xxxxxx"
set passwd2 "xxxxxxx"
set passwd2en "xxxxxxxxx"

#Define Prompt
set prompt "CNAPS-UAT-SW01.>$"
set prompt2 "SH.
S.>$"
#set prompt2 "SH13ES07>$"
set prompt2en "SH.
S.*#$"
#set prompt2en "SH13ES07#$"

#Define HOSTNAME, argv 0 means get the first parameters from input.
set HOSTNAME [lindex $argv 0]
#set HOSTNAME "10.110.104.117"

#Define the SwitchPort
#set switchport "g1\/0\/20"
set switchport [lindex $argv 1]
set switchport_noG [lindex $argv 2]

#Login Gateway Switch
spawn ssh [email protected]
expect {
"yes/no" { send "yes\r"; exp_continue }
"
Password:" { send "$passwd\r" }
}
expect -re "$prompt"
send "ssh $HOSTNAME\n"
expect {
"yes/no" { send "yes\r"; exp_continue }
"
Password:" { send "$passwd2\r" }
}
expect -re "$prompt2"
send "enable\n"
expect "Password:"
send "$passwd2en\n"
expect -re "$prompt2en"
send "ter len 0\n"
expect -re "$prompt2en"
send "sh ip int b | in $switchport_noG\n"
expect -re "$prompt2en"
send "sh run int $switchport | in switchport access\n"
expect -re "$prompt2en"
send "sh mac add | in $switchport_noG\n"
expect -re "$prompt2en"
send "exit\r"

[root@ChrisTest modify_VLAN_for_clients_PC]# cat domodify.sh
#!/usr/bin/expect -f

##Define Variables
#Define Timeout
set time 10

#Define Password
set passwd "xxxxxx"
set passwd2 "xxxxx"
set passwd2en "xxxxxxxxx"

#Define Prompt
set prompt "CNAPS-UAT-SW01.>$"
set prompt2 "SH.
S.>$"
set prompt2en "SH.
S.*#$"

#Define HOSTNAME, argv 0 means get the first parameters from input.
set HOSTNAME [lindex $argv 0]
#set HOSTNAME "10.110.104.117"

#Define the SwitchPort
#set switchport "g1\/0\/20"
set switchport [lindex $argv 1]
set switchport_noG [lindex $argv 2]

#Define the SwitchPort VLAN
set VLAN [lindex $argv 3]

#Login Gateway Switch
spawn ssh [email protected]
expect {
"yes/no" { send "yes\r"; exp_continue }
"
Password:" { send "$passwd\r" }
}
expect -re "$prompt"
send "ssh $HOSTNAME\n"
expect {
"yes/no" { send "yes\r"; exp_continue }
"
Password:" { send "$passwd2\r" }
}
expect -re "$prompt2"
send "enable\n"
expect "Password:"
send "$passwd2en\n"
expect -re "$prompt2en"
send "ter len 0\n"
expect -re "$prompt2en"
send "conf t\n"
expect -re "$prompt2en"
send "int $switchport\n"
expect -re "$prompt2en"
#send "des test\n"
send "switchport access vlan $VLAN\n"
expect -re "$prompt2en"
send "sh\n"
expect -re "$prompt2en"
send "no sh\n"
expect -re "$prompt2en"
send "do sh run int $switchport | in switchport access\n"
#expect -re "$prompt2en"
#send "sh ip int b | in $switchport_noG\n"
#expect -re "$prompt2en"
#send "sh run int $switchport | in switchport access\n"
#expect -re "$prompt2en"
#send "sh mac add | in $switchport_noG\n"
expect -re "$prompt2en"
#send "exit\r"
send "end\n"
expect -re "$prompt2en"
send "wr\n"
expect -re "$prompt2en"
send "exit\r"
[root@ChrisTest modify_VLAN_for_clients_PC]# cat SWlistwithHostname.txt
SH11ES01 10.110.104.71
SH11ES02 10.110.104.72
SH11ES03 10.110.104.73
SH11ES04 10.110.104.74
SH11ES05 10.110.104.75
SH11ES06 10.110.104.76
SH11WS01 10.110.104.81
SH11WS02 10.110.104.82
SH11WS03 10.110.104.83
SH11WS04 10.110.104.84
SH11WS05 10.110.104.85
SH11WS06 10.110.104.86
SH12ES01 10.110.104.91
SH12ES02 10.110.104.92
SH12ES03 10.110.104.93
SH12ES04 10.110.104.94
SH12ES05 10.110.104.95
SH12WS01 10.110.104.101
SH12WS02 10.110.104.102
SH12WS03 10.110.104.103
SH12WS04 10.110.104.104
SH12WS05 10.110.104.105
SH12WS06 10.110.104.106
SH13ES01 10.110.104.111
SH13ES02 10.110.104.112
SH13ES03 10.110.104.113
SH13ES04 10.110.104.114
SH13ES05 10.110.104.115
SH13ES07 10.110.104.117
SH13ES08 10.110.104.119
SH13WS01 10.110.104.121
SH13WS02 10.110.104.122
SH13WS03 10.110.104.123
SH13WS04 10.110.104.124
SH15ES01 10.110.104.151
SH15ES02 10.110.104.156
SH15ES03 10.110.104.157
SH15ES04 10.110.104.158

2019-01-03补充:
Bug1:当脚本运行在13F以外的楼层时会无法正常抓取IP Phone和Computer的MAC地址(已修复)

今天在实际运行Modify VLAN的脚本时,遇到了输出结果的错误.
Target Client的IP Phone的MAC地址抓不到,同时Computer的地址又出现了三个,其中两个重复.

这在之前修改脚本时,就遇到过,但我用正则表达式自认为修复了该问题.
当时是由于筛选结果时,没有考虑到有些端口只有Computer连接,而没有IP Phone(MAC为空)的情况.

这之后,我加入了[ -z ]的空值判断,假如变量$macaddripphone为空,则用不同的正则表达式区别对待。

这次的这个问题,经过询问,得知并不是每个楼层的voice vlan都是VLAN706。
macaddripphone=cat worklog_$DATE/Result.log | grep 706 | awk '{print $2}'
所以以上筛选结果在voice vlan不是706时,会使得变量$macaddripphone变为空值,从而使在这基础之上的正则表达式赋予变量

$macaddrcomputer错误的结果。

经过同事点拨,我了解到,Cisco的IP Phone的MAC地址prefix时唯一的。都是009e。所以可以简单的用这个作为关键字来筛选。
经过修改后语句如下。
macaddripphone=cat worklog_$DATE/Result.log | grep 009e | awk '{print $2}' | uniq
而底下的判断语句不变,依然有效。
#Add if cluase just to avoid error when standalone PC without IP Phone
if [ -z $macaddripphone ]; then
macaddrcomputer=cat worklog_$DATE/Result.log | grep DYNA | awk '{print $2}'
else
macaddrcomputer=cat worklog_$DATE/Result.log | grep DYNA | awk '{print $2}' | grep -v $macaddripphone
fi

猜你喜欢

转载自blog.51cto.com/7350550/2337328
PC: