From: expect <ex...@ih...> - 2003-09-17 04:24:34
|
On Sun, 14 Sep 2003 16:27:52 -0500 "Chris Muth" <mut...@mc...> 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; $Expect::Debug = 1; > use POSIX; > > # Comment out daemonizing code starting here > ####################################### > $pid = fork(); > exit 0 if ($pid); > > POSIX::setsid(); > close STDIN; I commented this #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"); and had to do this (probably not pertinent to your problem) $exp->spawn("/usr/local/bin/ssh","usernameonremotehost\@$host"); And it worked. If it's not suitable as a solution it should provide the clue. > > # 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 > > |