LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Expect script package return (http://www.linuxquestions.org/questions/programming-9/expect-script-package-return-927175/)

ShadowAce 02-02-2012 09:57 AM

Expect script package return
 
I've run into an issue that I cannot resolve and Google seems to be no help, either. Hopefully, one of you gurus can help me out here:

I've been writing a bunch of expect scripts to perform functions on remote servers in our environment. Part of every script is the login process, which can get pretty tedious (it's a double login with multiple passwords).

While the login process works, it takes up a lot of space in each script. I'd like to push it off into a Tcl package, and it almost works. My new script now calls login $server and it will login to the remote server, but then it just hangs.

What I'm trying to do is similar to the following:

.
.
.
login $server

expect {
"#" {send "ls -ls /root\r"}
}
expect {
"#" {send "exit\r"}
}
.
.
.

My login script does NOT have an "expect eof", but it does have the following:

expect {
"#" {return}
}

However, somwhere between the end of the login $server line and the first expect, it just hangs. It does display the root prompt on the remote server, showing that the login was successful.

Is this an issue with process ids, threads, or something else?

How can I get my parent script to recognize the output from the login script to continue processing from the remote server?

Thanks!

devUnix 02-03-2012 04:49 AM

You need "ssh-keygen" to generate keys that you can share with the remote servers. Then you will need to do "ssh Demo@ExampleServer.com command_or_script" and it will return back from the server giving you the output of your command/script if it produces any.

First work on them and then let us know if you need more help. I am not familiar with Tcl so I do not know your codes above. But using the method I have specified above you can simply get a listing of files by issuing this command:

Code:


ssh Demo@ExampleServer.com ls -ltr /root

More Examples:

Code:

output=`ssh Demo@ExampleServer.com 'uptime; free'`
echo "System Health Info"
echo "$output"


ShadowAce 02-03-2012 06:05 AM

I appreciate you taking the time to reply. However, using ssh keys is not allowed in this environment.

I am familiar with the method you describe, and I would love to do it that way, but it is not politically feasible at this time.

I need to be able to log in, then be able to perform 1 or more commands. The "ls -ls" command in the OP was just an example. It needs to be able to perform a whole series of commands, if required.

Thank you!

ntubski 02-03-2012 09:32 AM

You can have ssh ask for the password just once, without needing to use keys:

Code:

# will ask for password here
ssh -NfM -o 'ControlPath=~/.ssh/%r@%h:%p.conn' "$HOST"

# uses existing connection, doesn't ask for password
ssh -o 'ControlPath=~/.ssh/%r@%h:%p.conn' ls -ls /root
ssh -o 'ControlPath=~/.ssh/%r@%h:%p.conn' another command
...

# close the connection
ssh -o 'ControlPath=~/.ssh/%r@%h:%p.conn' -O exit "$HOST"


ShadowAce 02-03-2012 10:14 AM

That would be fine if I didn't have to work on several hundred different remote servers. :D

ntubski 02-07-2012 07:31 PM

Hmm, I guess there aren't many expect experts on this forum, it might be easier to help if you could give a small self-contained example to demonstrate the problem.


All times are GMT -5. The time now is 01:15 AM.