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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script which telnets to a device, runs commands and prints output to a file
# 1  
Old 04-02-2013
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 Smilie
Thanks..
# 2  
Old 04-02-2013
You can put this statement at the top of your script so you can see what 'expect' is seeing:
Code:
exp_internal 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can i create a script that will ssh a device and type some commands?

Hi Guys, this is the scenario: ubuntu pc and I have 10 wireless devices that I need to check their firmware version. I would like to create a script that it will ask me IP, after I enter it, I hit enter then it will show me the version of the firmware. this is what i do. ssh... (9 Replies)
Discussion started by: gabak
9 Replies

2. Shell Programming and Scripting

awk runs and produces output but with error

When I run the awk below, I get an error message awk -v OFS='\t' '$(NF-1)=="Benign" || ($(NF-2) OFS $(NF-1))=="Likely Benign" {$(NF)=$(NF-2) OFS $(NF-1)} {print $0 }' input awk: cmd. line:1: (FILENAME=VUS FNR=8) fatal: attempt to access field -1 input Chr Start End Ref ... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Same sed code prints(p) correct, but writtes(w) wrong output

Dear all, I am using sed as an alternative to grep in order to get a specific line from each of multiple files located in the same directory. I am using sed because it prints the lines in the correct order (unlike grep). When I write sed code that prints out the output I get it correct, but... (1 Reply)
Discussion started by: JaNaJaNa
1 Replies

4. Shell Programming and Scripting

awk runs but output is empty

The awk below runs, however the output file is 0 bytes. It is basically matching input files that are 21 - 259 records to a file of 11,137,660 records. Basically, what it does is use the input files of which there are 4 to search and match in a large 11,000,000 record file and output the... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Script to compare 2 files and prints difference as output sidebyside

Hi All, Am trying script to compare 2 files and print the difference found from old file to new file on line by line basis on side by side display. Basically line by line comparision and files may contain blank line as well I know we have compare/diff commands but i don't how to make... (10 Replies)
Discussion started by: Optimus81
10 Replies

6. Shell Programming and Scripting

script - multiple telnets with logging

We had a system outage, and now I am trying to figure out how to get raw data to store in a log file. I have a flat file that has multiple IP port line. I want to telnet to each and log each. But as soon as I connect to the first, it stays there. This is on an HPUX 11.23 system. I dont think... (1 Reply)
Discussion started by: lhradowy
1 Replies

7. Shell Programming and Scripting

[bash] run a shell who runs commands

Hi all. On X11 I'm on a shell ...shell_1 (/bin/bash). From here I want to open another shell window shell_2 who executes commands like "ls -l" or programs like ". /program"... so the "result" of commands shows in shell_2 window and not in shell_1. Is that possible ? (4 Replies)
Discussion started by: jerold
4 Replies

8. Shell Programming and Scripting

Script Runs fine but not giving any output

Hi, My script is running with no erros but not giving any output can anyonehelp. #!/bin/ksh . /home/application/bin/application.env OUTFILE=Result.txt PROD_PASSWORD=`${GET_PWD} -f ${PWD_FILE_PATH} -s ${PROD_SERVER} -u ${PROD_USER}` echo "1)To get the book last loaded details " read... (7 Replies)
Discussion started by: jagadish_gaddam
7 Replies

9. Shell Programming and Scripting

command runs, no output

I have a script that searches for specific information from log files. #!/bin/sh sed -n '/*C/,/END/p' /sn/log/OMlog* > crit.out sed -n '/REPT INITIALIZATION/,/err:/p' /sn/log/OMlog* > switchcc.out ./start.awk /sn/log/OMlog* > ARs.out ./end.awk /sn/log/OMlog* > ARe.out cat crit.out... (1 Reply)
Discussion started by: grinds
1 Replies

10. UNIX for Dummies Questions & Answers

Output of 2 commands into 1 file

How could you put the output of two commands into one file using a single command? For example put the output of a grep command and a sort command into one file together. Here is another rough explanation of what I am trying to do; output of $ grep pattern file1 plus output of $ sort file... (8 Replies)
Discussion started by: enuenu
8 Replies
Login or Register to Ask a Question