Fun and games with expect

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Fun and games with expect

Postby maxeaves » Mon Nov 14, 2011 2:42 pm

Dear all,

I think this should be an easy answer, but I cannot find out how to do this.

What I have is the following: I want to telnet into a switch, and retrieve the output to a text file. I'm using expect - so far I have is:

#!/bin/bash

expect << EOF

set timeout 20
spawn telnet xxx.xxx.xxx.xxx
expect "telnet@******************>"
send "en\r"
expect "Password:"
send "*******\r"
expect "*#"
send "show int brief\r"
expect "*"
send "exit\rexit\r"
exit
EOF

What I cannot seem to get to work is the output from the SHOW INT BRIEF command to be outputted to a file.....I've tried send "show int brief\r" >> outputfile.txt but it doesn't seem to like the output. I've also tried the send_log and file_log options, but this also doesn't do want I want it to do.

Suggestions ?

Thanks



Max
maxeaves
 
Posts: 63
Joined: Thu Oct 18, 2007 8:54 am

Fun and games with expect

Postby PeterM » Thu Dec 01, 2011 1:07 pm

Afternoon Max,
I am also having a big fight with expect but I might be able to help a little. I can get it writing to a file but it puts the wrong thing in. I have put in the lines

set fid [open wombat.txt w+];
set capturedinfo $expect_out(buffer);
puts $fid $capturedinfo;


Which opens a file called wombat.txt for writing and puts the expect_out buffer into a variable which in turn is written to the file.
My problem is that it is writing output from a previous step into the file and actually prints out the information I want twice.
Here's my work-in-progress script:

#!/usr/bin/expect
# Usage: gwinfo userid password
set timeout 10
set mgwyip [lindex $argv 0]
set userid [lindex $argv 1]
set password [lindex $argv 2]
match_max 100000
puts "logging in to $mgwyip\r"

spawn ssh $userid@$mgwyip
expect {
-re "continue connect" {send "yes\r"}
timeout {puts "Host is known"}
}

expect {
-re "(P|p)assword: " {send "$password\r"}
-re "Warning:" {send "$password\r"}
-re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}
-re "Connection closed" {puts "Host error -> $expect_out(buffer)";exit}
-re "no address.*" {puts "Host error -> $expect_out(buffer)";exit}
timeout {puts "Timeout error. Device down or unreachable? ";exit}
}

expect {
-re "super" {send "show image version\r"}
timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}
set fid [open wombat.txt w+];
set capturedinfo $expect_out(buffer);
puts $fid $capturedinfo;

expect {
-re "super" {send "exit\r"}
timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}

set output $expect_out(buffer)
send "exit\r"
puts "$output\r\n"



It's a bit messy but that's because I keep changing bits, adding bits and removing bits.
If you get your script to work, could you post the details?

Cheers

Pete
PeterM
 
Posts: 16
Joined: Wed Jan 12, 2011 11:37 am
Location: Chichester

More Fun and games with expect

Postby PeterM » Thu Dec 01, 2011 1:26 pm

Me again,

Just as I was starting to go insane, I was talking the problem out loud to the cat and it suddenly hit me. I had the write the file bit in the wrong place.
Here's some working code for you:

#!/usr/bin/expect
# Usage: gwinfo userid password
set timeout 10
set mgwyip [lindex $argv 0]
set userid [lindex $argv 1]
set password [lindex $argv 2]
match_max 100000

puts "logging in to $mgwyip\r"

spawn ssh $userid@$mgwyip

expect {
-re "continue connect" {send "yes\r"}
timeout {puts "Host is known"}
}

expect {
-re "(P|p)assword: " {send "$password\r"}
-re "Warning:" {send "$password\r"}
-re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}
-re "Connection closed" {puts "Host error -> $expect_out(buffer)";exit}
-re "no address.*" {puts "Host error -> $expect_out(buffer)";exit}
timeout {puts "Timeout error. Device down or unreachable? ";exit}
}



expect {
-re "super" {send "show image version\r"}
timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}


expect {
-re "super" {send "exit\r"}
timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}
}
set fid [open wombat.txt w+];
set capturedinfo $expect_out(buffer);
puts $fid $capturedinfo;
set output $expect_out(buffer)
send "exit\r"
puts "$output\r\n"



Now it just puts what I want in a file called wombat.txt ready for further processing. :D
Probably need to tidy up a bit now, but at least I'm happy again.
Cheers

Pete
PeterM
 
Posts: 16
Joined: Wed Jan 12, 2011 11:37 am
Location: Chichester

More Fun and games with expect

Postby PeterM » Thu Dec 01, 2011 1:28 pm

And yes, you did read it correctly, I was talking to the cat :oops:
I think the cat was listening, the expression didn't change much but at least it didn't roll it's eyes.
PeterM
 
Posts: 16
Joined: Wed Jan 12, 2011 11:37 am
Location: Chichester


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest