ssh via expect

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh via expect
# 1  
Old 06-06-2011
ssh via expect

(Crossposting note: I have already posted this article on comp.lang.tcl 6 days ago and on the tek-tips dot com forum 3 days ago. This is posted here again, because I didn't get any response on my original articles there).

I use the following script on Solaris to log into a remote host:

spawn ssh other@[exec uname -n]
expect {$}
send ". xxx/s\r"

This script works well. However, I don't quite understand the output
printed by this script on stdout:

spawn ssh other@rom38
Last login: Tue May 31 10:01:35 2011 from rom38.muc.infin
bash-2.03$ . xxx/s
[other] rom38@~>

The last line is the prompt after logging in. However, why do I see
the line

. xxx/s

three times? I would have expected it only once (the last occurance),
but why do I see it already before twice? My guess is that this is
some timing issue (that I send the command just a bit too early),
but even then, I should see only one extra occurance.

Last edited by pludi; 06-06-2011 at 08:47 AM..
# 2  
Old 06-06-2011
I believe it's the terminal that is making you see what you typed ahead multiple times (and I believe it's somehow related to the amount of output you're getting while logging in). Me too, I get multiple occurrences of the sent string as far as my expect command matches the first line of a multi-line input.

I suppose you won't see those lines if you adjust your expect pattern {$} in order to match your prompt.

I believe that you need to escape the $ in order to match a literal dollar sign and not the end of the string (i.e. the braces are not sufficient in this case).
This User Gave Thanks to radoulov For This Post:
# 3  
Old 06-06-2011
I created my own solution for copying files to remote machines and performing commands on those machines with expect in the thread "Need expect script to copy trusted keys". Sorry they won't let me post a url because I don't have 5 posts yet.
This User Gave Thanks to master-of-puppe For This Post:
# 4  
Old 06-07-2011
Thanks a lot - escaping the $ at least reduced the three occurances to two!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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@ ssh -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

2. Shell Programming and Scripting

Script using SSH with expect command

Hi all, I want to connect to some host with "ssh". I have googled and got some commands of "expect" and "spawn". I was not aware of these commands and tried below script. $ cat set host xx.xx.xx.xx set password abcd@1234 set user root spawn ssh $user@$host expect "*?assword:*"... (4 Replies)
Discussion started by: divya bandipotu
4 Replies

3. Shell Programming and Scripting

Expect Script to Automate SSH

How would I write an expect script to automate ssh and what file extention do expect files use? (11 Replies)
Discussion started by: Prodiga1
11 Replies

4. Shell Programming and Scripting

Pass subshell through expect and SSH

Hi folks, What I want to do is to check if there is an instance of running vlc on a remote server, then kill it, and start it again. The code I came up with is: #!/bin/bash expectFcn() { expect -c " set timeout -1 spawn ssh \"$1@$2\" \"$4\" match_max 100000 expect { -re... (2 Replies)
Discussion started by: dukevn
2 Replies

5. Shell Programming and Scripting

Need Help with expect when ssh fails

I'm trying to write a script using expect. I'd like the script to execute several commands when the ssh succeeds and i want it to exit if the ssh fails. Does this require to define a time out for the ssh command so that if the prompt is back before this defined time the next commands are executed??... (2 Replies)
Discussion started by: Hossam_Nox
2 Replies

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

7. Shell Programming and Scripting

SSH Expect Script

Ok, i don't know if anyone else here have had to deal with something like this before, but here's my situation. I have about 1000+ servers I need to log into to do something. What i need to do is to log into each server, go to a certain directory on each of the servers, copy the files that... (3 Replies)
Discussion started by: SkySmart
3 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

how do I handle ssh response with expect

I am trying to write an expect script that trys to telnet, if telnet fails, trys to ssh to a remote network devices. The script works fine until the following is received : spawn telnet Trying telnet: connect to address Connection refused is... (2 Replies)
Discussion started by: popeye
2 Replies

10. UNIX for Advanced & Expert Users

expect and ssh

hello I installed expect on my solaris box. now I want to execute this command on several servers as root (all of them have the same root password): for i in 1 2 3; do ssh root@"srv"$i" ls /; done; I want of course to skip these 2 steps: The authenticity of host 'srv3 ('... (4 Replies)
Discussion started by: melanie_pfefer
4 Replies
Login or Register to Ask a Question