How to run scripts within a telnet session?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run scripts within a telnet session?
# 1  
Old 12-19-2012
How to run scripts within a telnet session?

I want to connect to a remote host using telnet
there is no username/password verification
just
Code:
    telnet remotehost

then I need to input some commands for initialization

and then I need to repeat the following commands:
Code:
    cmd argument

argument is read from a local file, in this file there are many lines, each line is a argument
and after runing one "cmd argument", the remote host will output some results
it may output a line with string "OK"
or output many lines, one of which is with string "ERROR"
and I need to do something according to the results.

basically, the script is like:
Code:
    initialization_cmd  #some initial comands
    while read line
    do    
      cmd $line
      #here the remote host will output results, how can I put the results into a variable?
      # here I want to judge the results, like
      if $results contain "OK";then
           echo $line >>good_result_log
      else
           echo $line >> bad_result_log
      fi     
    done < local_file
    
     the good_result_log and bad_result_log are local files

is it possible or not? thanks!
# 2  
Old 12-19-2012
You could try an expect script

Something like this should do it, command are in text file cmd_file in current directory output files are good_result_log and bad_result_log:


Code:
#!/usr/bin/expect -f
set timeout 2
spawn -noecho telnet remotehost
log_user 0
 
# Change command prompt to "PROMPT# " to make matching safer
send "PS1='PROMPT# '\r"
expect -re "PROMPT# '(.*)PROMPT# "
 
#Open Input and Output files
set cmds [open cmd_file r]
set eout [open bad_result_log w]
set gout [open good_result_log w]
 
while {[gets $cmds command] >= 0} {
    send "$command\r"
    expect -re "\r\n(.*)\r\n(.*)PROMPT# "
    set output $expect_out(1,string)
    if {[regexp {OK} $output match] == 1} {
       puts $gout $output
    } else {
       puts $eout $output
    }
}
 
close $cmds
close $eout
close $gout
 
send "exit\r"
expect eof

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

ssh session getting hung (smilar to hpux telnet session is getting hung after about 15 minutes)

Our network administrators implemented some sort of check to kill idle sessions and now burden is on us to run some sort of keep alive. Client based keep alive doesn't do a very good job. I have same issue with ssh. Does solution 2 provided above apply for ssh sessions also? (1 Reply)
Discussion started by: yoda9691
1 Replies

2. Shell Programming and Scripting

telnet and run scripts

Hello folks, I've got this script which connects to different boxes and executes a certain script in those locations. The following is the line from where i am trying to do this: (sleep 1; echo $USERID ; sleep 1; echo $PASSWD ; sleep 1 ; echo y ; sleep 1 ; echo "\r" ; sleep 1 ; echo "cd... (1 Reply)
Discussion started by: Rajat
1 Replies

3. UNIX for Dummies Questions & Answers

Disconnecting a telnet session

How can I disconnect an existing telnet session? The host is a serial port server with multiple ports. The users login using the host's name and a port, i.e. telnet host01 1235. Thanks. (14 Replies)
Discussion started by: cooldude
14 Replies

4. UNIX for Dummies Questions & Answers

Telnet Session to AIX

Hello, I have AIX 5.3 at home connected to netgear router. Port Forwarding has been enabled on the router. Problem is that if I want to telnet, I have to try 2 or 3 times before I can get a logon prompt. It times out for first or second time (Connection to session <IP_Address> failed: Connection... (1 Reply)
Discussion started by: bluebee
1 Replies

5. UNIX for Dummies Questions & Answers

Unix Telnet session

Hi Is there any way whilst in a telnet session you can view your client machine name that you are using to connect to the Unix box ? :eek: (2 Replies)
Discussion started by: mlucas
2 Replies

6. Shell Programming and Scripting

Telnet Session

{ sleep 2 echo "$user" sleep 2 echo "$password" sleep 2 echo " ls" sleep 10 echo "exit" }| telnet $server I have a machine x and i have executed the above script on machine 'x'. i entered the... (6 Replies)
Discussion started by: pathanjalireddy
6 Replies

7. Shell Programming and Scripting

Telnet session does not expire

Dear friends.. Our project has a module that runs on handheld devices. Through the handheld we telnet to solaris where the application actually runs. I noticed that after starting a session through the handheld, if i go out of range or if i remove and replace the battery in the handheld, the... (1 Reply)
Discussion started by: deepsteptom
1 Replies

8. UNIX for Dummies Questions & Answers

telnet session timeout

hi, we can set something such that if the user has been idle for a while, it will auto disconnect. where to do so? thanks (6 Replies)
Discussion started by: yls177
6 Replies

9. UNIX for Dummies Questions & Answers

Uploading and Downloading during a telnet session

Hi there, I'm trying to download files from a server and also upload new files to the same server during a telnet session. How do I go about getting certain files (.gif) from the server onto my local drive and vice versa? Can anyone help me? Thank you so much! (1 Reply)
Discussion started by: WhitneyMay
1 Replies
Login or Register to Ask a Question