Expect is really awesome to do some stuff fast and don't worry about
Perl telnet modules, sockets and stuff. I have a lab with 5 routers
and I was sick of cleaning the configuration every time I started a new
lab test, so I wrote a simple expect script (attached to this msg) to load
a default config. If you have a PM2 or something, you could change this
script to log into the PM2, reload the router and cleanup configs and IOS
image. Would be cool to have those power supplys that you can control
from the network, it should be a must have for those remote labs that rent
rack space and people mess with configuration :)

-- 
Daniel Lafraia
Senior Software Developer CCNP/CCDP
Earthlink MIS Group

On Fri, 27 Sep 2002, Howard C. Berkowitz wrote:

> Date: Fri, 27 Sep 2002 17:35:27 GMT
> From: Howard C. Berkowitz 
> To: [EMAIL PROTECTED]
> Subject: OT: Perl/Expect to Console? [7:54344]
>
> I've written some Perl scripts (on a LINUX box) to drive some router
> tests. Perl is something I've taught myself, and don't have the
> experience with it I do with C.
>
> I got some vague advice from one of our people to write TCL/Expect
> and plug that in -- another language to learn -- or to use a telnet
> client. Well, of course telnet would work manually -- but can I
> fork/spawn a subprocess and connect it to Perl, such that STDIN and
> STDOUT of telnet appear as Perl files?
>
> Examples or pointers to them welcome.
>
> Howard
#!/usr/bin/expect

set telnet "/usr/bin/telnet";
set chmod  "/bin/chmod";
set user   "lafraia";
set pass   "XXXX";
set enable "YYYY";
set tftpserver "192.168.254.254"
set tftpdir "/tftp"
set device  [lindex $argv 0];
set cfgfile [lindex $argv 1];

spawn "$chmod" "a+r" "$tftpdir/$cfgfile"
expect eof
spawn "$telnet" "$device";
expect {
        "Username:" {
                send "$user\r"
                expect "Password:"
                send "$pass\r";
        } "Password:" {
                send "$pass\r";
        }
}
expect -re "\[\>\#\]";
send "enable\r";
expect {
        "Password:" {
                send "$enable\r";
                expect "#";
        } "#"
}
send "erase startup-config\r";
expect {
        "Erasing the nvram filesystem will remove all files! Continue?" {
                send "\r"
                expect "#"
        } "#"
}
send "copy tftp start\r"
expect {
        -nocase "host or network configuration " {
                send "host\r";
                expect -nocase "of remote host";
                send "$tftpserver\r";
        }  -nocase "of remote host" {
                send "$tftpserver\r";
        }
}
expect -re "Name of configuration file|filename";
send "$cfgfile\r\r";
expect {
        "confirm" {
                send "\r";
        }
}
expect "#"
send "reload\r";
expect {
        "Save" {
                send "no\r";
        } "Proceed" {
                send "\r"
        }
}
expect "confirm";
send "\r"
expect eof
spawn "$chmod" "600" "$tftpdir/$cfgfile"
expect eof




Message Posted at:
http://www.groupstudy.com/form/read.php?f=7&i=54355&t=54344
--------------------------------------------------
FAQ, list archives, and subscription info: http://www.groupstudy.com/list/cisco.html
Report misconduct and Nondisclosure violations to [EMAIL PROTECTED]

Reply via email to