LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Expect script to ssh a switch (https://www.linuxquestions.org/questions/linux-newbie-8/expect-script-to-ssh-a-switch-755271/)

hasan_sust 09-15-2009 01:41 AM

Expect script to ssh a switch
 
Hi there

I want to login a switch remotely using expect script. Problem arises when
I want to pass password. Switch's password Contains a $(dollar)sign. So when I execute the script it shows error, like as follows

# ./switchtest
spawn ssh root@x.x.x.x
root@x.x.x.x's password: can't read "H": no such variable
while executing
"send " FyPyNyT@$H\r""
(file "./switchtest" line 7)


Full Code is as follows
=======================
#!/usr/bin/expect

set timeout -1
spawn ssh root@x.x.x.x
expect "root@x.x.x.x's password:"
send " FyPyNyT@$H\r"
expect ">"
send "enable\r"
expect "password:"
send " FyPyNyT@$H\r"
expect "#"
interact


My problems:
1. is there any way to pass password in the form of a variable?
2. any way to read password from any text file and send it to switch?

I need the code. Thanks in advance.

Cheers
HASAN

kbp 09-15-2009 02:15 AM

Hi,

Ref: http://en.wikipedia.org/wiki/Expect

Quote:

#read the input parameters
set user [lindex $argv 0]
set passw [lindex $argv 1]
set host [lindex $argv 2]
set location [lindex $argv 3]
set file1 [lindex $argv 4]
set file2 [lindex $argv 5]

Looks like you just need to decide how many arguments you would like to provide and then use the appropriate index for your 'host' argument


cheers

hasan_sust 09-15-2009 09:34 PM

Expect Script to ssh a switch
 
Thanks for the link. But it didn't served my purpose.

Please see my code and mention How I implement this on my code.

Thanks anyway.

TB0ne 09-15-2009 09:44 PM

Quote:

Originally Posted by hasan_sust (Post 3684425)
Thanks for the link. But it didn't served my purpose.

Please see my code and mention How I implement this on my code.

Thanks anyway.

Try putting a backslash (\) in front of the $, like
Code:

"send FyPyNyT\@\$H\r"
that normally 'escapes' characters, and treats them as such, rather than as a control-character. Also, putting a password into ANY script is VERY insecure. Since you're using SSH, I'd strongly suggest using a key-swap, so you can do passwordless-logins, and yet remain secure. It will also greatly simplify your script, too....

hasan_sust 09-15-2009 10:22 PM

Thanks man. Putting backslash do the trick. :)


All times are GMT -5. The time now is 06:14 PM.