Need 'expect' help, ssh/telnet and trapping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need 'expect' help, ssh/telnet and trapping
# 1  
Old 03-03-2009
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 want to tell it yes. If i get connection refused. i want it to go and spawn a telnet session.
then run my commands.

Here is what i have that is definately not working.


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

set timeout 3
set verbose 0

log_file /home/username/portsfound

spawn ssh username@[lindex $argv 0]
   
if expect { "connecting (yes/no)?" {send "yes\r"} }
                        
if expect { "assword: " {send "password\r"} }

else expect { "*refused*" { spawn telnet [lindex $argv 0]
                          log_user 0
                          expect "ogin: "
                          send "username\r"
                          expect "assword: "
                          send "password\r"
                          expect "# "
                          log_user 1 }
						 
 log_user 1
   send "show clock\r"
   expect "# "
 log_user 0
   send -- "exit\r"


Last edited by ippy98; 03-03-2009 at 07:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcl / expect need to attempt telnet if failed ssh

Morning and Happy New Year to all. I am in a situation where I need to connect to a list of devices that are using either telnet or ssh. I want to try to telnet, if I receive any of the following I want to attempt ssh : "Connection refused" "Connection timed out" timeout expiration ... (3 Replies)
Discussion started by: popeye
3 Replies

2. Shell Programming and Scripting

Ssh = ssh expect and keep everything not change include parameter postion

I have write a script which contains ssh -p 12345 dcplatform@10.125.42.50 ssh 127.0.0.1 -p 5555 "$CMD" ssh root@$GUEST_IP "$CMD" before I use public key, it works well, now I want to change to "expect", BUT I don't want to change above code and "parameter position" I can post a... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. 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

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

Help needed with expect...(If telnet fails)

Hello I'm working using expect to automate telnet commands...i want to check if telnet fails and in this case...certain message should appear or certain word should be out in a file). Thses are the messages that apeear if telnet fails: Trying 172.16.3.115... telnet: Unable to connect... (0 Replies)
Discussion started by: Hossam_Nox
0 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. Shell Programming and Scripting

could not send commands SSH session with Net::SSH::Expect

I am using Net::SSH::Expect to connect to the device(iLO) with SSH. After the $ssh->login() I'm able to view the prompt, but not able to send any coommands. With the putty I can connect to the device and execute the commands without any issues. Here is the sample script my $ssh =... (0 Replies)
Discussion started by: hansini
0 Replies

8. Shell Programming and Scripting

Telnet Expect script question

Hi all, I have written a small expect script which should spawn a telnet session login and execute some commands. #!/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... (2 Replies)
Discussion started by: stinkefisch
2 Replies

9. 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

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