Cisco automate the config with expect command

Cisco automate the config with expect command

Need Expect installed in Linux Ubuntu
======================================
Linux pre-requisites
=====================
apt-get install expect
mkdir /root/rcisco
mkdir /root/rcisco/logs
cd /root/rcisco
vi IP-list
Put all the IP inside IP-list
example :
10.10.10.2
20.20.20.2
Save (:wq!)
vi ssh.sh
#!/bin/bash
while read  ipadd
do
#echo $ipadd
export ipadd
./enableint.sh $ipadd
done < IP-list
# Save (:wq!)
vi enableint.sh
#!/usr/bin/expect -f
        set timeout 20
        set IPaddress [lindex $argv 0]
        set Username “bandi”
        set Password “bandi”
        set enpass “bandi123”
        set Directory logs
        log_file -a $Directory/session_$IPaddress.log
        send_log “### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r”
        spawn ssh -o “StrictHostKeyChecking no” $Username@$IPaddress
        expect “*assword: “
        send “$Password\r”
        expect “>”
        send “en\r”
        expect “*assword: “
        send “$enpass\r”
        expect “#”
        send “conf t\r”
        expect “(config)#”
        send “int g0/0\r”
        expect “(config-if)#”
        send “no shut\r”
        expect “(config-if)#”
#        send “exit”
 #       expect “(config)#”
        send “end\r”
        expect “#”
        send “wr mem\r”
        expect “#”
        send “exit\r”
        sleep 1
        send_log “\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r”
exit
# Save
chmod +x *.sh
./ssh.sh
You will see below output.
root@ubuntu:~/b# ./ssh.sh
spawn ssh -o StrictHostKeyChecking no bandi@50.50.50.2
**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS  *
* education. IOSv is provided as-is and is not supported by Cisco’s      *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any       *
* purposes is expressly prohibited except as otherwise authorized by     *
* Cisco in writing.                                                      *
**************************************************************************Password:
**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS  *
* education. IOSv is provided as-is and is not supported by Cisco’s      *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any       *
* purposes is expressly prohibited except as otherwise authorized by     *
* Cisco in writing.                                                      *
**************************************************************************sw4>en
Password:
sw4#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
sw4(config)#int g0/0
sw4(config-if)#no shut
sw4(config-if)#end
sw4#wr mem
Building configuration…
Compressed configuration from 3001 bytes to 1506 bytes[OK]
# check the Logs
cd logs
root@ubuntu:~/b/logs# ls -al
total 32
drwxr-xr-x 2 root root 4096 Oct  3 19:40 .
drwxr-xr-x 3 root root 4096 Oct  3 19:42 ..
-rw-r–r– 1 root root 1703 Oct  3 19:40 session_20.20.20.2.log
-rw-r–r– 1 root root 1781 Oct  3 19:40 session_30.30.30.2.log
-rw-r–r– 1 root root 1781 Oct  3 19:40 session_40.40.40.2.log
-rw-r–r– 1 root root 5187 Oct  3 19:43 session_50.50.50.2.log
-rw-r–r– 1 root root  580 Oct  3 19:42 session_.log
root@ubuntu:~/b/logs# more session_20.20.20.2.log
spawn ssh -o StrictHostKeyChecking no bandi@20.20.20.2:40:02 BST 2015 ###
**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS  *
* education. IOSv is provided as-is and is not supported by Cisco’s      *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any       *
* purposes is expressly prohibited except as otherwise authorized by     *
* Cisco in writing.                                                      *
**************************************************************************Password:
**************************************************************************
* IOSv is strictly limited to use for evaluation, demonstration and IOS  *
* education. IOSv is provided as-is and is not supported by Cisco’s      *
* Technical Advisory Center. Any use or disclosure, in whole or in part, *
* of the IOSv Software or Documentation to any third party for any       *
* purposes is expressly prohibited except as otherwise authorized by     *
* Cisco in writing.                                                      *
**************************************************************************sw1>en
Password:
sw1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
sw1(config)#int g0/0
sw1(config-if)#no shut
sw1(config-if)#end
sw1#wr mem
Building configuration…
Compressed configuration from 3148 bytes to 1597 bytes[OK]
### /END-SSH-SESSION/ IP: 20.20.20.2 @ Sat Oct  3 19:40:06 BST 2015 ###
happy labbiiiiiiiiiiiiiiiiiiing !