Bash script - Rate change using expect for IBSng BW Limit (In Ras) - Cisco 7206 =============================================================================== Public Domain ******************************************************************************** ### rates.txt 72: police 90000 100000 conform-action transmit exceed-action transmit violate-action drop 1600: police 1800000 100000 conform-action transmit exceed-action transmit violate-action drop 1024: police 1150000 100000 conform-action transmit exceed-action transmit violate-action drop 2048: police 2300000 100000 conform-action transmit exceed-action transmit violate-action drop 96: police 110000 100000 conform-action transmit exceed-action transmit violate-action drop 256: police 300000 100000 conform-action transmit exceed-action transmit violate-action drop 512: police 570000 100000 conform-action transmit exceed-action transmit violate-action drop 513: police 570000 100000 conform-action transmit exceed-action transmit violate-action drop 768: police 850000 100000 conform-action transmit exceed-action transmit violate-action drop 192: police 220000 100000 conform-action transmit exceed-action transmit violate-action drop 128: police 150000 100000 conform-action transmit exceed-action transmit violate-action drop 384: police 420000 100000 conform-action transmit exceed-action transmit violate-action drop 385: police 420000 100000 conform-action transmit exceed-action transmit violate-action drop ******************************************************************************** ### changes.txt 72:96 128:192 256:384 384:512 385:768 512:1024 513:512 1024:1024 1600:1600 2048:2048 ******************************************************************************** ### /storage/scripts/IBSng/bwchange-cisco/afternoon.sh #!/bin/bash set -e PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin" CWD=$(dirname $(readlink -f $0)) TMP=$(mktemp) IP="192.168.11" USER="admin" PASS="123456" ENABLE="123456789" echo "set timeout -1 spawn telnet $IP match_max 100000 expect -exact \"Username: \" send -- \"$USER\\r\" sleep 1 expect -exact \"Password: \" send -- \"$PASS\\r\" sleep 1 expect \">\" send -- \"enable\\r\" sleep 1 expect -exact \"Password: \" send -- \"$ENABLE\\r\" sleep 1 expect \"#\" send -- \"conf t\\r\"" > $TMP for LINE in $(cat changes.txt); do NAME=$(echo $LINE | sed -e 's,:.*,,g') RATE=$(echo $LINE | sed -e 's,.*:,,g') POL=$(egrep "^$RATE:" rates.txt | sed -e 's,.*:,,g') echo "expect \"#\"" >> $TMP echo "send -- \"policy-map $NAME\\r\"" >> $TMP echo "expect \"#\"" >> $TMP echo "send -- \"class class-default\\r\"" >> $TMP echo "expect \"#\"" >> $TMP echo "send -- \"$POL\\r\"" >> $TMP done echo 'expect "#" send -- "end\r" expect "#" send -- "wr mem\r" expect "#" send -- "exit\r" expect eof' >> $TMP #cat $TMP expect -f $TMP rm -v $TMP ******************************************************************************** ### diff -ruN afternoon.sh morning.sh --- afternoon.sh 2013-04-15 10:02:40.000000000 +0430 +++ morning.sh 2013-04-15 10:03:06.000000000 +0430 @@ -28,7 +28,7 @@ for LINE in $(cat changes.txt); do NAME=$(echo $LINE | sed -e 's,:.*,,g') - RATE=$(echo $LINE | sed -e 's,.*:,,g') + RATE=$NAME POL=$(egrep "^$RATE:" rates.txt | sed -e 's,.*:,,g') echo "expect \"#\"" >> $TMP echo "send -- \"policy-map $NAME\\r\"" >> $TMP ******************************************************************************** ### Commands chmod +x /storage/scripts/IBSng/bwchange-cisco/afternoon.sh chmod +x /storage/scripts/IBSng/bwchange-cisco/morning.sh ******************************************************************************** ### crontab -e 00 18 * * * /storage/scripts/IBSng/bwchange-cisco/afternoon.sh &> /dev/null 00 07 * * * /storage/scripts/IBSng/bwchange-cisco/morning.sh &> /dev/null ******************************************************************************** _BY: Pejman Moghadam_ _TAG: expect, cisco, ibsng_ _DATE: 2013-04-16 12:39:51_