LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-04-2010, 05:35 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Help with expect command and multiple arguements


I have the following code which I've thieved from here, there and everywhere:

Code:
[root@localhost /]# cat autossh
#!/usr/bin/expect -f
set ipaddr [lrange $argv 0 0]
set username [lrange $argv 1 1]
set password [lrange $argv 2 2]
set command [lrange $argv 3 11]

set timeout -1
spawn ssh $username@$ipaddr
match_max 100000

expect "*?assword:*"

send -- "$password\r"
expect {
        "#*" {}
        "*>*" {}
        }
send -- "$command\r"
#send -- "\r"
send -- "exit\r"
expect eof
This works great unless I put stuff in speech marks or when I need to run multiple commands, e.g the following wont work:

autossh 123.123.123.123 root password ls -al && cd / && ls -al

It gives me the contents of ~ on my made up 123 host, then exits and gives me the contents of / on my local machine. If I do the same thing with speech marks I get the following error:

Code:
root@hostname # {ls -al && cd / && ls -al}
exit
{ls: not found
Ideally I'd like to be execute either of the above commands properly. Can anyone help.

Thanks

Last edited by genderbender; 10-04-2010 at 05:37 AM.
 
Old 10-04-2010, 07:16 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am no expect guru, but how are {} considered speech marks?
 
Old 10-04-2010, 07:19 AM   #3
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Sorry, that was an example of the error: Exact input and output below:

Code:
[root@localhost scripts]# autossh 123.123.123.123 root password "ls -al && cd / && ls -al"
spawn ssh root@123.123.123.123
Password:
root@hostname # {ls -al && cd / && ls -al}
exit
{ls: not found
root@hostname # Connection to 123.123.123.123 closed.
 
Old 10-04-2010, 07:26 AM   #4
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
It's cool, I've just got it working Did this:

#set command [lrange $argv 3 11]
set command [lindex $argv 3]<--- Use this instead.
 
  


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
[SOLVED] expect help (multiple commands in argument) vikas027 Programming 10 03-17-2010 01:19 AM
[SOLVED] Execute a command using expect visu_kvg Linux - Newbie 4 08-05-2009 10:08 AM
How to use expect command? jprathap Linux - Newbie 1 11-21-2008 10:30 PM
"make" does not work with command line arguements %$hfydt%$ Linux - General 7 04-01-2008 10:59 AM
alias command line arguements Henster Linux - Newbie 2 07-01-2005 03:40 AM

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

All times are GMT -5. The time now is 12:02 AM.

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