From: Chris M. <mut...@mc...> - 2003-09-14 21:28:58
|
Hi, I am working on a script that daemonizes and then uses ssh to login/operate/logout of a remote machine. The code works perfectly when it is not demonized, (with the demonization code commented out). The script will run and partially work if the code (the lines indicated below) is left in,-- it will fork, detach, and sleep but fail to get the command prompt on the remote machine. I have it log to a file (LOG_FILE), because when it is daemonized I cannot see the output. The only thing I can think of is that ssh must do something to the terminal/console that works when I run it manually on the console, but when the program is daemonized, ssh cannot do whatever it is trying to do and fails. The segment below fails saying "never got command prompt". This means that it got the password prompt from the remote machine and sent the password, but never got the command prompt after that. I don't think this is an expect problem, I think that it is a problem with how ssh and expect work together. My guess is that ssh is not intentionally failing, but it is trying to do something that does not work with the terminal that expect uses. I have even tried "ssh -l root" to specify the username, "ssh -T" so ssh won't allocate a pseudo-tty, and not setting the pty to raw; all to no avail. Does anyone have any ideas? Below is the code segment. Thanks, -Chris Muth #!/usr/bin/perl5.8 -w #prompt user for host to login to, and password print"host: ";$host=3D<STDIN>;chomp$host; print"pass: ";$pass=3D<STDIN>;chomp$pass; use Expect; use POSIX; # Comment out daemonizing code starting here ####################################### $pid =3D fork(); exit 0 if ($pid); POSIX::setsid(); close STDIN; close STDOUT; close STDERR; sleep 5; ####################################### # comment out daemonizing code ending here open (LOG_FILE, "> output"); # open log file $exp =3D new Expect; $exp->raw_pty(1); # login to the remote host #$exp->spawn("/usr/local/bin/ssh",'-T',"$host"); # Even tried ssh -T $exp->spawn("/usr/local/bin/ssh","$host"); # wait for Password: prompt $retval =3D $exp->expect(30,'-re','word:\s$'); if (!defined $retval){ print LOG_FILE "Never got password prompt\n"; flush LOG_FILE; } else { $exp->send("$pass\n"); #($retval,undef,$retstr,undef,undef) =3D $exp->expect(30,'stdin: is not= a tty'); # for ssh -T ($retval,undef,$retstr,undef,undef) =3D $exp->expect(30,'# '); # do something on the remote machine, test results if (!defined $retval){ print LOG_FILE "Never got command prompt\n"; flush LOG_FILE; } else { print LOG_FILE "\"$retstr\"\n"; flush LOG_FILE; $exp->send("echo Hello World\n"); $exp->expect(5,'This will make expect sleep for 5 seconds'); $exp->send("logout\n"); print LOG_FILE "It worked\n"; flush LOG_FILE; } } $exp->hard_close(); print LOG_FILE "\n"; flush LOG_FILE; close LOG_FILE; exit 0; |