LinuxQuestions.org
Help answer threads with 0 replies.
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 03-12-2010, 10:50 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Rep: Reputation: 99
Unhappy expect help (multiple commands in argument)


Dear All,

I am stuck in between an expect script.

Here is my code.
Code:
#!/bin/bash

LOGIN ()
{
VAR=$(expect -c "
log_user 0
spawn ssh $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "$VAR"
}

HOST="192.168.1.2"
USER="vikas"
PASS="vikas123"

CMD="uname -a"
LOGIN
This works as charm, But when I try to execute multiple commands in the CMD variable as
Code:
CMD="uname -a;df -kh"
it throws this error:
Code:
-sh-3.00$ ./vik.sh 
invalid command name "df"
    while executing
"df -kh"
Any alternative to this. I want to do it through expect only NOT through ssh keys.

Last edited by vikas027; 03-12-2010 at 11:09 AM. Reason: missed something
 
Old 03-12-2010, 10:56 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
http://rcsg-gsir.imsb-dsgi.nrc-cnrc....et/node31.html
just
Code:
ssh user@host "uname -a;df -kh"
working for me with ssh key of my host on remote hosts ~/.ssh/authorized_keys.

Last edited by troop; 03-12-2010 at 10:59 AM.
 
Old 03-12-2010, 11:09 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Original Poster
Rep: Reputation: 99
Quote:
Originally Posted by troop View Post
http://rcsg-gsir.imsb-dsgi.nrc-cnrc....et/node31.html
just
Code:
ssh user@host "uname -a;df -kh"
working for me with ssh key of my host on remote hosts ~/.ssh/authorized_keys.
I knew this would be coming, thanks for the reply But I want to do it through expect only.
 
Old 03-12-2010, 11:31 AM   #4
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
put CMD inside expect:
http://mysqlpreacher.com/wordpress/2...ash-scripting/ and http://stackoverflow.com/questions/1...-a-bash-script is examples

Last edited by troop; 03-12-2010 at 11:33 AM.
 
Old 03-12-2010, 01:25 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by vikas027 View Post
I am stuck in between an expect script.
Can you put quotes arount $CMD as shown below?
Code:
spawn ssh $USER@$HOST "$CMD"
 
Old 03-12-2010, 11:36 PM   #6
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Original Poster
Rep: Reputation: 99
Unhappy

Quote:
Originally Posted by David1357 View Post
Can you put quotes arount $CMD as shown below?
Code:
spawn ssh $USER@$HOST "$CMD"
Hi David,

Thanks for your time.

I did put $CMD as "$CMD" in spawn line.

Now, when I am using variable
Code:
CMD="uname -a;df -kh"
, I am getting the below error
Code:
expect: invalid option -- a
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]
This is due to the -a switch in uname -a.

And when I remove use
Code:
CMD="uname;df -kh"
, I am getting the below error
Code:
couldn't read file ";": no such file or directory
Any idea to overcome this.
 
Old 03-13-2010, 12:11 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Original Poster
Rep: Reputation: 99
Unhappy

Quote:
Originally Posted by troop View Post
Hi Troop,

As per the examples, I tried but could not succeed. This is my new modified code :-

#!/bin/bash

Code:
LOGIN ()
{
VAR=$(expect -c "
log_user 0
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $USER@$HOST
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
send -- "uname -a\r"
expect -exact "uname -a"    

expect eof
")
echo "$VAR"
}

HOST="112.110.32.203"
USER="smsc"
PASS="smsc123"

#CMD="uname ; df -kh"
LOGIN
I get this error:-
Code:
expect: invalid option -- a
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]
I think I am missing some important thing. Please help.
 
Old 03-15-2010, 03:18 AM   #8
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Original Poster
Rep: Reputation: 99
Unhappy

Hi Gurus,

Any other ideas ?
 
Old 03-15-2010, 03:59 AM   #9
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Just a quick look !! I cannot verify your expect but you can write another shell at "112.110.32.203" which contains there two commands and then execute this command rather than uname -a and df -kh.

I know it is not the answer to your question. But another way round solution to your problem.

Another thought.

if you have autoexpect installed try it using autoexpect. !!

Last edited by PMP; 03-15-2010 at 04:07 AM. Reason: Notes for autoexpect added
 
Old 03-15-2010, 12:22 PM   #10
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by vikas027 View Post
I did put $CMD as "$CMD" in spawn line.
I think I should have suggested changing $CMD to \"$CMD\". I think you have to escape the double quotes because the $CMD is already inside a quoted string starting at
Code:
VAR=$(expect -c "
 
1 members found this post helpful.
Old 03-17-2010, 02:19 AM   #11
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,270

Original Poster
Rep: Reputation: 99
Lightbulb

Quote:
Originally Posted by David1357 View Post
I think I should have suggested changing $CMD to \"$CMD\". I think you have to escape the double quotes because the $CMD is already inside a quoted string starting at
Code:
VAR=$(expect -c "

Perfect !
This did wonders for me.

Thanks a TON !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 script - for argument loop ? nilleso Linux - Software 3 09-16-2009 09:51 PM
How To pass argument in Expect command? jprathap Linux - Newbie 3 08-27-2009 05:58 PM
Expect cannot find commands feilb Linux From Scratch 2 07-22-2008 03:00 PM
Argument List Too Long - For ALL Commands peteHelgren Linux - General 17 07-12-2008 06:44 AM
argument list to long for linux commands supersucker Linux - Newbie 1 05-10-2005 08:36 AM


All times are GMT -5. The time now is 12:45 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration