Cisco Command Repeat

Expect script to repeatedly run a command on Cisco IOS, like the Linux/Unix "watch" command.

#!/usr/bin/expect

# Expect script to repeatedly run the same command
# by jwbensley<at>gmail.com
# If you don't like it, fuck off.

# Long delay for those tricky hostnames

set timeout 60

# Prompt user for device name/IP address, username, password,
# and command to run (example: show cdp neigh)

send_user "Device name: "
expect_user -re "(.*)\n"
set host $expect_out(1,string)

send_user "Username: "
expect_user -re "(.*)\n"
set user $expect_out(1,string)

# Don't show the password it is typed
stty -echo
send_user "Password: "
expect_user -re "(.*)\n"
set pass $expect_out(1,string)
send_user "\n"
stty echo

send_user "Enable mode by default \[yes\/no\]: "
expect_user -re "(.*)\n"
set enable $expect_out(1,string)
# Expect enable mode by default
if { $enable == "" } {
 set enable "yes"
}

send_user "SSH or telnet \[ssh\/telnet\]: "
expect_user -re "(.*)\n"
set protocol $expect_out(1,string)
# Use SSH as default
if { $protocol == "" } {
  set protocol "ssh"
}

send_user "command: "
expect_user -re "(.*)\n"
set command $expect_out(1,string)
if { $command == "" } {
  send_user "Need interface name\!\n"
  exit
}

send_user "Interval in seconds \[3\]: "
expect_user -re "(.*)\n"
set delay $expect_out(1,string)
# Use 3 seconds as the default execution delay if none is given
if { $delay == "" } {
  set delay 3
}

send_user "Show date and time \[yes\/no\]: "
expect_user -re "(.*)\n"
set datetime $expect_out(1,string)
# Show date and time of command as default
if { $datetime == "" } {
  set datetime "yes"
}

send_user "\n"

# Connect to the host and login
if { $protocol == "ssh" } {
  spawn ssh $host -l $user
  # Expect "assword:" as the 'p' in 'password' could be upper or lower case
  expect "assword:"
  send "$pass\r"
  if { $enable == "yes" } {
   expect "#"
  } else {
   expect ">"
  }
  puts ""
} else {
  spawn telnet $host
  expect "Username:"
  send "$user\r"
  # Expect "assword:" as the 'p' in 'password' could be upper or lower case
  expect "assword:"
  send "$pass\r"
  if { $enable == "yes" } {
   expect "#"
  } else {
   expect ">"
  }
  puts ""
}

# From this point on we shall disable user output or they will see the command being run repeatedly
log_user 1

# Enter a continuous loop to repeatedly run the command

while { true } {

  set datet [exec date +%d-%m-%y--%H:%M:%S]
  if { $datetime == "yes" } {
    send_user "$datet\n"
  }

  send "$command\r"
  if { $enable == "yes" } {
   expect "#"
  } else {
   expect ">"
  }

  send_user "\n\n"

  sleep $delay
}
[[email protected] ~]$ ./cisco-watch.sh
Device name: 10.254.253.1
Username: james.bensley
Password:
Enable mode by default [yes/no]: yes
SSH or telnet [ssh/telnet]: ssh
command: show ver | i uptime
Interval in seconds [3]: 1
Show date and time [yes/no]: yes
spawn ssh 10.254.253.1 -l james.bensley
C
********************************************************************************
Unauthorised connection and/or use of this system is a criminal offence.
Disconnect NOW, if you have not been expressly authorised to use this system.
By logging onto this system, you accept that your activity may be subject to
monitoring for compliance purposes and may be recorded
********************************************************************************

********************************************************************************
All logins and commands executed are logged to a central security platform.
********************************************************************************
password:

17-07-15--14:52:36
show ver | i uptime
router1 uptime is 1 year, 5 weeks, 6 days, 15 hours, 47 minutes
router1#

17-07-15--14:52:37
show ver | i uptime
router1 uptime is 1 year, 5 weeks, 6 days, 15 hours, 47 minutes
router1#

17-07-15--14:52:38
show ver | i uptime
router1 uptime is 1 year, 5 weeks, 6 days, 15 hours, 47 minutes
router1#

17-07-15--14:52:39
show ver | i uptime
router1 uptime is 1 year, 5 weeks, 6 days, 15 hours, 47 minutes
router1#