Telnet Expect script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Telnet Expect script question
# 1  
Old 08-05-2009
Telnet Expect script question

Hi all,

I have written a small expect script which should spawn a telnet session login and execute some commands.

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

spawn telnet  $env(IP)
match_max 100000
expect "login:"
send -- "******\n"
expect -exact "Password:"
send -- "****\n"
expect "%"

Now I have got the following problem..................
If the telnet service is not running on the server, I would like to see the script to fail, but the script still carrys on to execute the commands, even it is not logged in?????
How can I stop this please?

---------- Post updated at 09:43 AM ---------- Previous update was at 05:55 AM ----------

Just in case someone else has the same problem.

I could sort it with

Code:
nc -w 5 -z ${MACHINE} 23
if [ $? -eq 0 ]
then
    echo "Telnet accepting connections"
else
    echo "Telnet connections not possible"
fi


Last edited by DukeNuke2; 08-05-2009 at 11:46 AM.. Reason: added code tags
# 2  
Old 08-05-2009
How about:

Code:
catch {spawn -noecho telnet $env(IP)
set timeout 10
expect {
  timeout
    { send_user "Telnet timed out waiting for $env(IP)\n" ; exit }
  "onnection refused"
    { send_user "Connection was refused to $env(IP)\n" ; exit }
  "known host"
    { send_user "System $env(IP) is unknown\n" ; exit}
  "login:"
}
send -- "******\n"

# 3  
Old 08-06-2009
Quote:
Originally Posted by peterro
How about:

Code:
catch {spawn -noecho telnet $env(IP) }
set timeout 10
expect {
  timeout
    { send_user "Telnet timed out waiting for $env(IP)\n" ; exit }
  "onnection refused"
    { send_user "Connection was refused to $env(IP)\n" ; exit }
  "known host"
    { send_user "System $env(IP) is unknown\n" ; exit}
  "login:"
}
send -- "******\n"

Hi and thanks for the answere.

This works fine but you have missed of a close-brace at the top
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

expect telnet script execute by cronjob

hi, please help, keep getting this bolded error and look it up and people say its your environment variable though i tried to set it manually in expect..it run fine if i run it manually but once i run it by cronjob it error below..i tried to comment out ip/login info with *.. logfile:: START... (0 Replies)
Discussion started by: cssanangeles
0 Replies

2. Shell Programming and Scripting

Calling Expect Script - Telnet

Hi All, I have an Expect script which logs into Cisco switch, performs a show interface command. I want to read a file of ip addresses which will be passed to the expect script. The script to read the file works, the expect script works on it's own but when i call the 'expect' script from the... (12 Replies)
Discussion started by: trinak96
12 Replies

3. Shell Programming and Scripting

EXPECT script for Telnet automation. Need your support.

Dear experts, please help me . I've found simple EXPECT scripts and all works fine. But I need more automation in error handling and sending list of commands/output logging from multiple remote hosts. I have 10 hosts, for example: host1 192.168.1.1 LOGIN1 PASSWORD1 ...... ... (2 Replies)
Discussion started by: starchen
2 Replies

4. Shell Programming and Scripting

expect: redirect telnet to file

I've got some expect/tcl scripts. Now i want to add a function that allows to open a telnet connection and redirect the output to a logfile. On the shell/terminal i tried something like: 'telnet 192.168.123.123 12121 > /home/user/logging/log-telnet.log' and the telnet is redirected into the... (2 Replies)
Discussion started by: JaPatton
2 Replies

5. Shell Programming and Scripting

Expect script to automate telnet session

Hi all, I am currently running a daemon which creates a virtual terminal for testing purposes. Essentially, if I were to interact with it manually, this is what I get. john@test1:~$telnet localhost 7777 Trying ::1... Connected to localhost. Escape character is '^]' mip6d> pl eth2... (6 Replies)
Discussion started by: abxccd
6 Replies

6. Shell Programming and Scripting

telnet commands using expect

Hi All, I am trying to write a expect script to telnet and run a command on a remote host.The command i want to send contains a text value is contained in file.txt in the linux box from where i am running the expect script.I want to pass the contains of file.txt into a variable and call the... (1 Reply)
Discussion started by: pistachio
1 Replies

7. Infrastructure Monitoring

expect telnet unexpected delays

I must automatically monitor and manage a large number of boxes on our network. I have been using perl/Net::Telnet and expect/telnet and also perl/ssh and expect/ssh to reach the command line of the remote boxes. Scripts are working but slow. (Yes, I do use SNMP also but many boxes do not... (2 Replies)
Discussion started by: kp2a
2 Replies

8. Shell Programming and Scripting

Need 'expect' help, ssh/telnet and trapping

So here is what I am trying to do. I have a large # of switches and routers I am trying to log into. Unfortunately some have ssh only, some have telnet only. and some i have never logged into with ssh. I first want it to SSH, if i have never logged into the box it will ask for adding the ssh key. I... (0 Replies)
Discussion started by: ippy98
0 Replies

9. Shell Programming and Scripting

Question about an expect script

I am using Expect to spawn a command that loops through a text file and runs the same command for each item in the text file. The text file, named stat.txt looks something like this: 2007-04 alist 543 2008-07 blist 543 2008-03 xlist 345 2008-03 ylist 675 2003-03 zlist 567 The expect... (1 Reply)
Discussion started by: manouche
1 Replies

10. Shell Programming and Scripting

Webpage to Telnet via Perl and Expect: Telnet problem?

Somewhat long story: I have a simple Perl CGI script that uses Expect to Telnet to a device and grab some data, and then spits it back to Perl for display on the Webpage. This works for many devices I've tried, but one device just fails, it keeps rejecting the password on this device, only... (1 Reply)
Discussion started by: jondo
1 Replies
Login or Register to Ask a Question