1752815 Members
5727 Online
108789 Solutions
New Discussion юеВ

Expect script...

 
SOLVED
Go to solution
Hazem Mahmoud_3
Respected Contributor

Expect script...

Ok, so this is my first time using expect. I installed it and configured it fine and for the most part it's working, but I can't seem to execute any commands after I login (at least I don't think it's executing anything). I can log in fine, but it just logs out at the end, it's not showing me that it executed the "ls" command or "find" command. Any help please! This just smells of a bunny:) Here is my script:

------------
#!/usr/local/bin/expect -d

spawn /usr/local/bin/ssh root@
expect "root@'s password: "
send "\r"
expect "# "
send "find / -name TEMPFILE \r"
send "bye\r"


--------------
To execute I just run ./backups.sh

Thanks!

-Hazem
3 REPLIES 3
Mark Greene_1
Honored Contributor

Re: Expect script...

Try using the -f option as well, and then
embed the expect commands in a "here" file in the script:

#!/bin/ksh

/usr/local/bin/expect -d -f <set timeout -1
match_max 100000
spawn /usr/local/bin/ssh root@
expect "root@'s password: "
send "\r"
expect "# "
send "find / -name TEMPFILE \r"
send "bye\r"

EOF

Note the two lines I added as well.

mark
the future will be a lot like now, only later
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Expect script...

Hi Hazem,

There should be some expectation after you send 'find / -name" commmand. So, modify it as

expect "# "
send "find / -name TEMPFILE \r"
expect "# "
send "exit\r"

You can also do a

set pid [spawn /usr/local/bin/ssh root@]

After send 'exit\r', you can add 'exec kill $pid' just to make sure the telnet session gets killed.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Hazem Mahmoud_3
Respected Contributor

Re: Expect script...

And the bunny goes to Sridhar!!!! That did it!!! Thank you!!! Thanks for your suggestions as well Mark!

-Hazem