From: Roland G. <RGi...@cp...> - 2003-09-16 14:01:23
|
What system are you running on? You ARE using the latest version of IO::Tty, I suppose... The problem is: ssh is opening /dev/tty to read the password. If the script is started in a terminal session, /dev/tty is defined and Expect will redirect it for the spawned program to the pty, so the password prompt is seen. When the script is started via cron, there is no controlling terminal and obviously Expect cannot take control of it (it would have to create it), so ssh cannot get the password and thus fails. Even though I've had my share at kernel hacking, I do not know how to resolve this, especially given that this is probably highly system-dependend... Workaround: set up public key authentication for ssh, so ssh doesn't ask for a password. But then you'd probably won't need Expect at all... Hope this helps, Roland Chris Muth wrote: > 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=<STDIN>;chomp$host; > print"pass: ";$pass=<STDIN>;chomp$pass; > > use Expect; > use POSIX; > > # Comment out daemonizing code starting here > ####################################### > $pid = 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 = 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 = $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) = $exp->expect(30,'stdin: is not a tty'); # for ssh -T > ($retval,undef,$retstr,undef,undef) = $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; > > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss > |