#!/usr/bin/expect # #========================================================== # # NAME: t # CONTRIBUTORS: Tyson Scott (tscott@ipexpert.com) # DATE : 09/08/2010 # PURPOSE: Telnet to Network Devices # REQUIREMENTS: expect. # #========================================================== #===================================== # Set Global Parameters set env(PATH) "/bin:/sbin/:/usr/bin/:/usr/sbin:/home/tyson:" set env(SHELL) "/bin/bash" set prompt "(\>|%|#|\\$)" set send_slow { 1.08} set first [lindex [split $argv ","] 0] set port [lindex [split $argv ","] 1] set prepare [lindex [split $argv ","] 2] if { [ regexp "^\[1-9]$" "$first" ] } { set port 200$first } elseif { [ string match "c1" "$first" ] } { set port 201[expr [lindex [split $first "c"] 1] - 1] } elseif { [ regexp "^c\[2-4]$" "$first" ] } { set port 201[expr [lindex [split $first "c"] 1] + 2] } elseif { [ regexp "^b\[1-3]$" "$first" ] } { set port 201[lindex [split $first "b"] 1] } elseif { [ regexp "^a\[1-2]$" "$first" ] } { set hostname "$termserv2"; set port 200[lindex [split $first "a"] 1] } elseif { [ string match "i" "$first" ] } { set hostname "$termserv2"; set port 2003 } else { set hostname [lindex [split $first ","] 0] } if { [string match "" "$port"] } { set PORT "NO" } else { set PORT "YES" } #===================================== # Set Tacacs Username and Password set tac_user "" set tac_pass "" #===================================== # Define Which Pod you are using set hostname "pod101ts1.proctorlabs.com" set termserv2 "pod101ts2.proctorlabs.com" #===================================== # Process to Login to Devices proc RUN_OPEN_TELNET_CONNECTION { } { global hostname; global PORT; global tac_user; global tac_pass if { [ string match "YES" "$PORT" ] } { global port } else { } set timeout 100; set attempt 0 while { $attempt < 5 } { expect { "Connection refused" { if { [string match "NO" "$PORT"] } { send "telnet $hostname\r"; expect "telnet $hostname\r" } else { send "telnet $hostname $port\r"; expect "telnet $hostname $port\r" } incr attempt; continue } "No route to host" { puts "\n\nDevice is not Reachable\n\n"; exit 1 } "Username" { send "$tac_user\r"; expect "$tac_user"; exp_continue } "Password" { send "$tac_pass\r"; exp_continue } "Connection closed by foreign host" { if { [string match "NO" "$PORT"] } { send "telnet $hostname\r"; expect "telnet $hostname" } else { send "telnet $hostname $port\r"; expect "telnet $hostname $port" } incr attempt; continue } -re "You are on line number" { send -s "\r\r\r"; break } -re "(\r|\r\n)\[a-zA-Z\_\.\/0-9\-]+#" { send "\r"; break } timeout { if { [string match "NO" "$PORT"] } { puts "\n\nFailed_To_Connect_To\_$hostname\n\n"; exit 1 } else { puts "\n\nFailed_To_Connect_To\_$hostname\_$port\n\n"; exit 1 } } } } if { $attempt == 5 } { if { [string match "NO" "$PORT"] } { puts "\n\nFailed_To_Connect_To\_$hostname\n\n"; exit 1 } else { puts "\n\nFailed_To_Connect_To\_$hostname\_$port\n\n"; exit 1 } } else { } } if { [string match "" "$port"] } { spawn telnet $hostname } else { spawn telnet $hostname $port } RUN_OPEN_TELNET_CONNECTION if { [ string match "1" "$prepare" ] } { send "enable\r"; expect "enable"; send "configure terminal\r"; expect "configure terminal" expect "\\(config\\)"; set commands [open "commands.txt" "r"] while {[gets $commands line] != -1} { send "$line\r"; expect -exact "$line" } close $commands } else { } {interact}