Expect, SSH and multiple passed commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect, SSH and multiple passed commands
# 1  
Old 07-08-2009
Expect, SSH and multiple passed commands

Hey Everyone,
I have found this script online that has almost all the features I am looking for. However, I do not know enough expect to debug the problem.

http://linuxgazette.net/100/misc/tip...ool.expect.txt

First, it Traps out after it collects the user's password. I do not know why it will not continue.

Secondly I have tried with this script and other ones to try to pass MORE then one command to ssh to execute on a remote system. Every time I do I get one of the following.
1. When I pass a set of commands expect interprets the ";" as the end of the line.
2. If i use hex for the ";" it cannot run the second script correctly to run.
3. If I try to use double quotes when passing the commands from bash to expect, expect now wraps those multiple entries in curly braces, breaking the expect spawn command.

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

# set Variables
set usname [lrange $argv 0 0]
set password [lrange $argv 1 1] 
set ipaddr [lrange $argv 2 2]   
set commands [lrange $argv 3 end]

# now connect to remote UNIX box (ipaddr) with given script to execute

# Set the size of the buffer, and ensure it hasn't been undefined.
match_max 10000
set expect_out(buffer) {}

# Set the timeout before expect assumes the spawn has not responded or lost link
set timeout 60

# now connect to remote UNIX box (ipaddr) with given script to execute

spawn ssh -4 -2 -k -o StrictHostKeyChecking=no $usname@$ipaddr $commands

expect {
                     "*?assword:" {
                               send "$password\r"
                   } "yes/no)?" {
                               send "yes\r"
                               set timeout -1
                   } timeout {
                               exit
				   } -re . {
							   exp_continue
                   } eof {
                               exit
                   }
}

# Keep connection active as long as text is returned to terminal.
expect -re . { 	exp_continue } eof { exit }

expect eof

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh multiple hops to execute commands with arguments

Hi I need to write a script to ssh through several hops (e.g. HostA-HostB-HostC-HostD), where Host A does not have direct assess to HostC ; HostB cannot access HostD directly. when I ssh 3 hops and run command with arg1, arg2 and redirect the output to a file, e.g. HostA> ssh -t HostB ssh -t... (3 Replies)
Discussion started by: chiensh
3 Replies

2. Shell Programming and Scripting

Run multiple commands in ssh

Hi All, I have the below requirement. I want to copy the local file to remote after that i need to run the local script on a remote machine.When i use two ssh commnds i can achieve this. But i want to achieve this using one ssh command. Below command to copy the local file to remote ssh -q... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

3. Shell Programming and Scripting

Executing multiple ssh commands inside a shell simultaneously

I would like to execute a commands in four different servers through ssh at a single instance(simultaneously). Below are the details with examples, ssh user1@server1 "grep xxxx logs" ssh user1@server2 "grep xxxx logs" ssh user1@server3 "grep xxxx logs" Each statement will take some... (4 Replies)
Discussion started by: Amutha
4 Replies

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

5. Shell Programming and Scripting

SSH + multiple commands

Sorry for the basic question here, but I can't seem to find an answer anywhere. I want to alias a command that will ssh and then open up bbedit all in 1 command. alias bbb 'ssh NAME@SERVER bbedit' returns: bash: bbedit: command not found It returns this same thing for anything except... (1 Reply)
Discussion started by: busdriver
1 Replies

6. Shell Programming and Scripting

[SSH] Accessing remote directory with user-passed path

Hi everybody, Currently, I have a script which access a remote computer via SSH, go to a folder already defined in the code and then executes a program in it, just like that: ssh user@host << EOI cd path ./file EOI It executes fine, but now I want to pass an argument in the command... (2 Replies)
Discussion started by: lgb3
2 Replies

7. Shell Programming and Scripting

connect to multiple servers using SSH and execute commands

Requirement: Run a shell script with below inputs file name checksum path the script should go to multiple servers (around 35) and verify the input cksum and if there is a mismatch display a simple message to the user that cksum verification failed. host details, user id /... (1 Reply)
Discussion started by: amicableperson
1 Replies

8. Shell Programming and Scripting

bash, ssh and expect to multiple ip addresses

Hi, I need script that will allow me to connect to multiple clients using ssh on Ubuntu terminal... I have a txt file with the ip addresses of clients, i need a script that will connect to everyone one by one and send some commands... The idea is to check some settings on every client... (2 Replies)
Discussion started by: marko07
2 Replies

9. Shell Programming and Scripting

Parameters passed to commands but not working in Bash shell

Hi, I am trying to do this thing useing my shell bash ( sorry for my english ) I have in a file 63 hostnames, i wanna ask to the DHCP admin, to reserv that reserves 63 IP addresses of this hosts, using their mac address. I have thinked this script: for ((i=1;i<63;i++)); do arp $(head... (10 Replies)
Discussion started by: Cypress
10 Replies

10. 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
Login or Register to Ask a Question