2

I'm working on an Expect script for pulling configuration information from my hosts.

The problem is that some of my hosts (when the enable command is entered) will prompt for both a login and password. This is mostly older HP kit but I've run into it elsewhere as well.

A nested expect statement like the following would do it if it were legal syntax. How can I make this happen?

expect {
  default { send_user "\nEnable Mode Failed - Check Password\n"; exit 1 }
  "*\#" {}
   "*>" {
    send "enable\n"
    expect{
        "Login" {
            send "$username\n"
        }
        default {}
    }
    expect "*assword"
    send "$enablepassword\n"
    expect "*\#"
 }
}

I know I have the syntax only slightly off - I've seen plenty of references that describe using nested expects - but for the life of me I can't quite get it.

3
  • Not exactly an answer to your question, but have you considered using Rancid (shrubbery.net/rancid)? This does exactly what you're describing (and uses Expect as well).
    – Teun Vink
    Dec 10, 2013 at 22:01
  • @TeunVink thought about it however we already have scripts and a version control process in place. All I'm really looking for is importing the raw information. Dec 10, 2013 at 22:03
  • Ok. You could still take a look at the hlogin Expect script to see how they handle this of course.
    – Teun Vink
    Dec 10, 2013 at 22:07

2 Answers 2

1

You could write that like this:

exp_internal 1
expect {
    default { send_user "\nEnable Mode Failed - Check Password\n"; exit 1 }
    "*>" {
        send "enable\r"
        expect{
            "Login" {
                send "$username\r"
                exp_continue
            }
            "*assword" {
                send "$enablepassword\r"
            }
        }
        # Now, look back to the outer expect to continue expecting the "#" prompt
        exp_continue
    }
    "*#"
}
  • use exp_internal 1 to enable debugging
  • terminate send commands with \r (carriage return is "hitting enter")
  • exp_continue to loop within the containing expect to keep looking for other patterns.
1
  • Thanks Glenn. With a couple small tweaks this did exactly what I needed. Dec 11, 2013 at 16:21
0

With many thanks to Glenn I have a script that will allow me to pull information from any of my various Cisco or HP devices.

#!/usr/bin/expect -f   
set hostname [lindex $argv 0];
set username [lindex $argv 1];
set password [lindex $argv 2];
set enablepassword [lindex $argv 3];
spawn ssh -o StrictHostKeyChecking=no $username\@$hostname
expect {
    timeout { send_user "\nTimeout Exceeded - Check Host\n"; exit 1 }
    eof { send_user "\nSSH Connection To $hostname Failed\n"; exit 1 }
    "*\#" {}
    "*assword:" {
    send "$password\n"
    }
}
# This allows for handling any of the various HP login screens. 
sleep 5
send " "
sleep 5
send "3\n"
sleep 5        
expect {
    "*>" {
        send "enable\n"
        expect {
            "*ogin" {
                send "$username\n"
            }
        }
        expect {
            "*assword" {
                send "$enablepassword\n"
                exp_continue
            }
            exp_continue
        }
    }
    "*\#" {}
    default { send_user "\nEnable Mode Failed - Check Password\n"; exit 1 }
    }
# Disable paging. This is both the HP and Cisco statements.
send "terminal length 0\n"
send "no page\n"
expect "*\\#"
log_file -noappend ./$hostname
send "show run\n"
expect "#\n"
log_file


send "end\n"
expect "\#"
expect "\#"
send "exit\n"
expect ":~\$"
exit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .