본문 바로가기

Script/Search Info 관련

Cisco Switch MAC(ver2) 정보 자동화

*세팅값 작성을 생활화 하자

Version : 모든 운영체제에서 문제 없이 동작

*단 bash, expect를 지원 해야합니.

 

주석이 처리된 부분은 제외하고 사용하셔도 됩니다.

*추가적으로 업데이트 및 수정 사항이 생길때마다 수정할 예정

*추가 여러분들의 댓글 의견도 바로 반영하겠습니다.

 

#!/bin/bash
#!/usr/bin/expect

#==========  스위치ip정보확인 데이터 정리 ==========#
ip_list=`cat ./switch_ip | sed 's/ //g'`

if [ -f "switch_ip_list.txt" ] ; then
        rm -rf switch_ip_list.txt
fi

for ipList in ${ip_list} ; do
        echo  ${ipList} | awk -F "#" '{print $1}'
done >> switch_ip_list.txt

#==========  스위치에 적용할 MAC정보 확인 데이터 정리 ==========#
mac_list=`cat ./switch_mac`

if [ -f "mac_deny_list.txt" ] ; then
        rm -rf mac_deny_list.txt
fi

for macList in ${mac_list} ; do
        #echo  ${macList} | awk -F '[-]' '{print "mac address static " $1$2 "." $3$4 "." $5$6 " vlan 1 drop"}'
        echo  ${macList} | awk -F '[-]' '{print $1$2 "." $3$4 "." $5$6}'
done >> mac_deny_list.txt

#==========  맥정보리스트를 가지고 스위치에 drop 적용 ==========#
function MAC_DROP_ADD {
for mac_list in `cat switch_mac_list.txt` ; do
#echo "mac_list : ${mac_list}"

IP_TOTAL_CNT=`cat ./switch_ip_list.txt | grep -Ev "^$" |wc -l | tr -d " "`
IP_SNUM=0
IP_FNUM=0

for ip_info in `cat ./switch_ip_list.txt` ; do
echo "==============================================="
ping -c 1 ${ip_info} > /dev/null

if [ $? -eq 0 ] ;then
echo "[${IP_SNUM}/${IP_TOTAL_CNT}] telnet ${ip_info}"
expect << EOF
spawn telnet ${ip_info}
send "\r"
expect "*assword: "
send "uangelcisco\r"
expect "*>"

send "en\r"
expect "*assword: "
send "ciscouangel\r"
expect "*#"
send "terminal length 0\r"

expect "*#"
send "conf t\r"
expect "*#"
send "mac address static ${mac_list} vlan 1 drop\r"
expect "#"
send "\r"
expect "*#"
send "\r"
expect "*#"
send "end\r"

expect "*#"
send "\r"

expect "*#"
send "exit\r"
expect eof
EOF
else
echo "[${IP_SNUM}/${IP_TOTAL_CNT}]'s ping fail"
IP_FNUM=`expr ${IP_FNUM} + 1`
fi
IP_SNUM=`expr ${IP_SNUM} + 1`
done

echo "==RESULT=="
echo "Target Mac address : ${mac_list}"
echo "Success Switch : ${IP_SNUM}/${IP_TOTAL_CNT}"
echo "Ping Fail Switch : ${IP_FNUM}"
done
}

#==========  맥정보리스트를 가지고 스위치에 drop 해제 ==========#
function MAC_DROP_DELETE {
for mac_list in `cat switch_mac_list.txt` ; do
        #echo "mac_list : ${mac_list}"

        IP_TOTAL_CNT=`cat ./switch_ip_list.txt | grep -Ev "^$" |wc -l | tr -d " "`
        IP_SNUM=0
        IP_FNUM=0

        for ip_info in `cat ./switch_ip_list.txt` ; do
                echo "==============================================="
                ping -c 1 ${ip_info} > /dev/null

                if [ $? -eq 0 ] ;then
                        echo "[${IP_SNUM}/${IP_TOTAL_CNT}] telnet ${ip_info}"
expect << EOF
spawn telnet ${ip_info}
send "\r"
expect "*assword: "
send "uangelcisco\r"
expect "*>"

send "en\r"
expect "*assword: "
send "ciscouangel\r"
expect "*#"
send "terminal length 0\r"

expect "*#"
send "conf t\r"
expect "*#"
send "no mac address static ${mac_list} vlan 1 drop\r"
expect "#"
send "\r"
expect "*#"
send "\r"
expect "*#"
send "end\r"

expect "*#"
send "\r"

expect "*#"
send "exit\r"
expect eof
EOF
else
echo "[${IP_SNUM}/${IP_TOTAL_CNT}]'s ping fail"
IP_FNUM=`expr ${IP_FNUM} + 1`
fi
IP_SNUM=`expr ${IP_SNUM} + 1`
done

echo "==RESULT=="
echo "Target Mac address : ${mac_list}"
echo "Success Switch : ${IP_SNUM}/${IP_TOTAL_CNT}"
echo "Ping Fail Switch : ${IP_FNUM}"
done
}

#==========  스위치에서 맥정보를 검색 ==========#
function MAC_FIND {
if [ -f "switch_mac_list_find.txt" ] ; then
         rm -rf switch_mac_list_find.txt
fi

echo -n "Find Mac address input (xxxx.xxxx.xxxx) : "
read MAC_INPUT

IP_TOTAL_CNT=`cat ./switch_ip_list.txt | grep -Ev "^$" |wc -l | tr -d " "`
        IP_SNUM=0
        IP_FNUM=0

for ip_info in `cat ./switch_ip_list.txt` ; do

ping -c 1 ${ip_info} > /dev/null

if [ $? -eq 0 ] ;then
echo "telnet ${ip_info}"

expect << EOF
spawn telnet ${ip_info}
send "\r"
expect "*assword: "
send "uangelcisco\r"
expect "*>"

send "en\r"
expect "*assword: "
send "ciscouangel\r"
expect "*#"
send "terminal length 0\r"

expect "*#"
send "show mac address-table address ${MAC_INPUT}\r"
expect "*#"
send "end\r"

expect "*#"
send "\r"

expect "*#"
send "exit\r"
expect eof
EOF
                else
                        echo "[${IP_SNUM}/${IP_TOTAL_CNT}]'s ping fail"
                        IP_FNUM=`expr ${IP_FNUM} + 1`
                fi
                        IP_SNUM=`expr ${IP_SNUM} + 1`
done >> switch_mac_list_find.txt

`grep ${MAC_INPUT} ./switch_mac_list_find.txt` >> switch_find_list.txt

echo "==RESULT=="
        echo "Target Mac address : ${MAC_INPUT}"
        echo "Success Switch : ${IP_SNUM}/${IP_TOTAL_CNT}"
        echo "Ping Fail Switch : ${IP_FNUM}"
}

##실행 메인
main () {
echo ""
echo ""

echo " 1. Mac address Deny add"
echo " 2. Mac address Deny delte"
echo " 3. Mac address Find"
read sel_num

echo ""

case "${sel_num}" in
1 )
MAC_DROP_ADD
;;
2 )
MAC_DROP_DELETE
;;
3)
MAC_FIND
;;
* )
echo "Script STOP !!"
exit 1
;;
esac
}
main $*