Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fun and games with expect

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
maxeaves



Joined: Thu Oct 18, 2007 9:54 am
Posts: 63

PostPosted: Mon Nov 14, 2011 2:42 pm    Post subject: Fun and games with expect Reply with quote

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
Back to top
View user's profile Send private message
PeterM



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

PostPosted: Thu Dec 01, 2011 1:07 pm    Post subject: Fun and games with expect Reply with quote

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
Back to top
View user's profile Send private message
PeterM



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

PostPosted: Thu Dec 01, 2011 1:26 pm    Post subject: More Fun and games with expect Reply with quote

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. Very Happy
Probably need to tidy up a bit now, but at least I'm happy again.
Cheers

Pete
Back to top
View user's profile Send private message
PeterM



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

PostPosted: Thu Dec 01, 2011 1:28 pm    Post subject: More Fun and games with expect Reply with quote

And yes, you did read it correctly, I was talking to the cat Embarassed
I think the cat was listening, the expression didn't change much but at least it didn't roll it's eyes.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast