user1535265
user1535265

Reputation: 1

Transitioning Expect script from Telnet to SSH only

Any help would be appreciated. I need to transition inherited expect scripts from Telnet to SSH only logins. First of, I'm a router guy that inherited all our expect script templates, written a while back. So far, with little modifications, they've been able to run smoothly. Our client wanted to move away from Telnet and so a few months ago we prepped all the Cisco routers and switches to support both Telnet and SSH. Until now, our scripts had been fine. However, Telnet support and servers will go away soon and I need to figure out how to reconfigure all the script templates to work in SSH only environments.

So, here's an example of a simple template to get a sh ver output:

#!/usr/local/bin/expect -f
#
set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

####################################################################
# Info for command line arguments

set argv [ concat "script" $argv ]

set router [ lindex $argv 1 ]


####################################################################
set timeout 15
set send_slow {1 .05}
spawn telnet $router
match_max 100000
expect Username:
sleep .1
send  -s -- "user\r"
expect Password:
sleep .1
send  -s -- "pass\r"
expect *
send  -s -- "\r"
expect *
sleep .2
send  -s -- "sh ver\r"
expect *
sleep .2
send  -s -- "end\r"
expect *
sleep .2
send  -s -- "wr\r"
expect *
sleep .2
send  -s -- "exit\r"
expect *
sleep .2

expect eof

Upvotes: 0

Views: 1267

Answers (1)

Rostyslav
Rostyslav

Reputation: 353

Instead of:

spawn telnet $router
match_max 100000
expect Username:
sleep .1
send  -s -- "user\r"
expect Password:
sleep .1
send  -s -- "pass\r"

Try to use:

spawn ssh -M username@$router
while 1 {
        expect {
                "no)?"          {send "yes\r"}
                "sername:"      {send "username\r"}
                "assword:"      {send "password\r"}
                ">"             {break}
                "denied"        {send_user "Can't login\r"; exit 1}
                "refused"       {send_user "Connection refused\r"; exit 2}
                "failed"        {send_user "Host exists. Check ssh_hosts file\r"; exit 3}
                timeout         {send_user "Timeout problem\r"; exit 4}
                }
        }

Upvotes: 0

Related Questions