Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Script which telnets to a device, runs commands and prints output to a file

Shell Programming and Scripting


Tags
automate script, command line arguments, shell, telnet script

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-02-2013   -   Original Discussion by samantha123
samantha123's Unix or Linux Image
samantha123 samantha123 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 2 April 2013, 1:48 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Script which telnets to a device, runs commands and prints output to a file

I am connecting to a device using telnet, I want my script to perform certain commands : ie- show device , show inventory..etc and write the output it sees from the terminal to a file.
this is what I have got :

Code:
#!/usr/bin/expect -- 

set running 1 
spawn telnet <ip address> 
expect  {'^]'.} 
expect "login:"    
send "admin\r"    
expect "Password:"    
send "********\r"  


set fh [open master1.txt w]    
set fh1 [open master2.txt w]  
expect "master>" 
send "show device\r"  

while $running { 
expect {      
"\n" { puts $fh "$expect_out(buffer)"}      
eof {set running 0}          
timeout {set running 0}         
}         
} 


close $fh 
puts done  

expect "master>" 
send "show status\r"  
while $running { 
expect {      
"\n" { puts $fh1 "$expect_out(buffer)"}  
eof {set running 0}      
    timeout {set running 0}       
  }       
  } 
close $fh1 
puts done    

expect -re {# $}   
exit


So what is happening now is after the first loop it is printing the data of the result to master1.txt file...but after the execution it comes to:
master>
and stops.. I tried modifying the second loop to say:
expect "master>" send "exit\r"
just to see if it exits out but its not reading the command and not exiting, I have to manually exit out. Please help me on what I am doing wrong. This is my first time with "expect" so "expect-ing" to learn here Linux
Thanks..
Sponsored Links
    #2  
Old Unix and Linux 04-02-2013   -   Original Discussion by samantha123
spacebar's Unix or Linux Image
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 18 May 2017, 3:08 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 71 Times in 70 Posts
You can put this statement at the top of your script so you can see what 'expect' is seeing:

Code:
exp_internal 1

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to compare 2 files and prints difference as output sidebyside Optimus81 Shell Programming and Scripting 10 01-15-2013 04:19 PM
script - multiple telnets with logging lhradowy Shell Programming and Scripting 1 10-05-2012 12:20 PM
[bash] run a shell who runs commands jerold Shell Programming and Scripting 4 07-28-2011 11:26 AM
Script Runs fine but not giving any output jagadish_gaddam Shell Programming and Scripting 7 03-29-2010 11:19 PM



All times are GMT -4. The time now is 07:37 AM.