LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   expect help (multiple commands in argument) (http://www.linuxquestions.org/questions/programming-9/expect-help-multiple-commands-in-argument-794974/)

vikas027 03-12-2010 09:50 AM

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.

troop 03-12-2010 09:56 AM

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.

vikas027 03-12-2010 10:09 AM

Quote:

Originally Posted by troop (Post 3895879)
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.

troop 03-12-2010 10:31 AM

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

David1357 03-12-2010 12:25 PM

Quote:

Originally Posted by vikas027 (Post 3895873)
I am stuck in between an expect script.

Can you put quotes arount $CMD as shown below?
Code:

spawn ssh $USER@$HOST "$CMD"

vikas027 03-12-2010 10:36 PM

Quote:

Originally Posted by David1357 (Post 3896007)
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.

vikas027 03-12-2010 11:11 PM

Quote:

Originally Posted by troop (Post 3895919)

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.

vikas027 03-15-2010 02:18 AM

Hi Gurus,

Any other ideas ?

PMP 03-15-2010 02:59 AM

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

David1357 03-15-2010 11:22 AM

Quote:

Originally Posted by vikas027 (Post 3896439)
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 "

vikas027 03-17-2010 01:19 AM

Quote:

Originally Posted by David1357 (Post 3899172)
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 ! :D


All times are GMT -5. The time now is 07:25 AM.