LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2009, 08:22 PM   #1
duffrecords
LQ Newbie
 
Registered: Nov 2009
Location: Los Angeles, CA
Posts: 29

Rep: Reputation: 0
Can expect return control of a spawned process to a shell script?


Is it possible to have an Expect script spawn an SSH session, log in, then go into interactive mode and give control of the SSH session to a Bash script? Here's a simplified example of the script so far:
Code:
#!/bin/bash

expect -c "
spawn -noecho ssh $user@$host
expect { 
  "*password:*" { send $password\r\n; interact } 
  eof { exit }
}
exit
"
ls -l | zenity --list --title "Select files" --column "Files" --separator=" " --multiple
As you might guess, I'm trying to create an interactive GUI for several routine SSH commands. Basically, I need to figure out how to pass commands from a local Bash script to a remote machine and make decisions based on feedback from the remote machine. Would it be easier to do this with port forwarding, and if so, can Expect be used to supply the password? RSA is not an option on these servers, unfortunately.
 
Old 11-19-2009, 08:27 PM   #2
Bhagyesh
Member
 
Registered: Jul 2009
Posts: 35

Rep: Reputation: 17
Lets see if this help you understand it much better..

this scripts will "ssh" into the remote machine and will "execute the command" that you pass to be executed on the remote system.

- it will fetch the output to your local screen

Code:
#!/usr/bin/expect -f
# USAGE:
#----------------------------------------------------
# ./ssh-expect.exp 'command to be executed on remote machine'
#----------------------------------------------------

set password [lrange $argv 0 0]
set remote_ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1

spawn ssh root@$remote_ipaddr $scriptname $arg1
match_max 100000

# Look for passwod prompt
expect "*?assword:*"

# Send password to the remote machine
send -- "$password\r"
send -- "\r"

#terminate the connection
expect eof

Last edited by Bhagyesh; 11-19-2009 at 08:33 PM.
 
Old 11-20-2009, 01:35 PM   #3
duffrecords
LQ Newbie
 
Registered: Nov 2009
Location: Los Angeles, CA
Posts: 29

Original Poster
Rep: Reputation: 0
Thanks. This is exactly the sort of thing I'm looking for. Does $scriptname represent the command to be executed remotely, or is that $arg1? I'm a little confused by that part. It looks like the file represented by $scriptname would need to exist on the remote server, correct?
 
Old 11-22-2009, 12:01 PM   #4
duffrecords
LQ Newbie
 
Registered: Nov 2009
Location: Los Angeles, CA
Posts: 29

Original Poster
Rep: Reputation: 0
I've managed to get this Expect script to run a remote Bash script specified by $scriptname and have the output of that script appear in my local stdout. That's what I need. However, I also need to forward my local stdin to the remote Bash script. So far the prompt doesn't seem to take any input; it just waits until I press Ctrl-C. How do I do this?
 
Old 04-28-2010, 02:33 PM   #5
Bhagyesh
Member
 
Registered: Jul 2009
Posts: 35

Rep: Reputation: 17
it should be something like this :

./ssh-expect.exp <remote-root-password> <remote-server-ip> <command to be executed>

you can modify the script according to your requirement
set password [lrange $argv 0 0]
set remote_ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
#set arg1 [lrange $argv 3 3]

changing the order of the above variables

FYI..
#set arg1 [lrange $argv 3 3] // sorry about this extra argument // you can remove this and still the script will work fine.

no extra key press the output to be right on your screen

#!/usr/bin/expect -f
# USAGE:
#----------------------------------------------------
# ./ssh-expect.exp '<remote-root-password> <remote-server-ip> <command to be executed>'
#----------------------------------------------------

set password [lrange $argv 0 0]
set remote_ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set timeout -1

spawn ssh root@$remote_ipaddr $scriptname
match_max 100000

# Look for passwod prompt
expect "*?assword:*"

# Send password to the remote machine
send -- "$password\r"
send -- "\r"

#terminate the connection
expect eof

Last edited by Bhagyesh; 04-28-2010 at 02:36 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect: Prevent output from spawned process to appear on stdout jlinkels Programming 3 08-12-2009 09:20 PM
return value of expect script .. shriyer Linux - Software 9 07-10-2009 04:06 PM
expect says it cant send the string to the spawned command exceed1 Programming 0 01-17-2009 06:51 AM
How to call expect in shell script? john.daker Programming 2 08-25-2008 08:33 PM
How to combine Expect and Shell script Uday123 Programming 0 12-27-2005 09:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration