LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem in expect (https://www.linuxquestions.org/questions/programming-9/problem-in-expect-656018/)

john83reuben 07-15-2008 08:05 PM

problem in expect
 
HI, I have problem in ssh, im trying to connect to another server through expect program, and after conecting, ill change into a particular directory to check if a lock file exist.
Below i did a if statement to check if the file exist or not, but it not working, how to solve this problem. pls advice. thanks
Quote:



#!/projects/ilinterf/bin/expect

spawn /bin/ksh

set site "server1"




send "ssh -p 22 ilinterf@$site cd /projects/ilinterf/john/kul_john;ls -la john.lock\n"

expect "ls -la /projects/ilinterf/john/kul_john" {
if {file exist "/projects/ilinterf/john/kul_john/john.lock" == 1} {
send "exit"
expect "h>"
} else {
send "ls -la /projects/ilinterf/john/kul_john
expect "ls -la /projects/ilinterf/john/kul_john"
expect "h>"

}

matthewg42 07-16-2008 06:00 AM

This is a very convoluted way to check for a file on a remote system. A simpler method which doesn't require expect at all if something like this:
Code:

#!/bin/ksh
result=$(ssh user@host '[ -a "/a/path/to/a/file" ] && echo yes || echo no')

At this point the variable result contains yes if the file exists, no if it does not, and will be blank if there was some connection error.

If you insist on using expect, I don't know why you spawn a ksh and then sending the ssh command to it - why not just spawn the ssh command?

Also, why are you expecting the command "ls -la /projects/ilinterf/john/kul_john"? This is not normal. The argument to expect is text you expect to see, not a command. You would typically expect a prompt or some other output which is produced during the login process, which is used as a trigger for sending some other command (in your case some test to see if a file exists).


All times are GMT -5. The time now is 03:11 AM.