jcpunk has asked for the wisdom of the Perl Monks concerning the following question:

alas monks I am totally confused by this one, it appears that Expect's logging functions do not opperate in a standard way. If I might prevail upon you to examine the code here and determine why the output here is as it is, logging things to places it I explicitly told it NOT to log to.
Here is the program I am testing this with
#!/usr/bin/perl -w use strict; use Expect; |++; print "Content-type: text/html\n\n<html>"; my $username="testuser"; my $password="password"; my $host="localhost"; my $timeout=7; my $exp = new Expect; # create our important expet object telnet_login($username,$password,$host,\$exp); $exp->expect($timeout, ['ncorrect', sub { print "<center>ERROR:<br>Login Incorre +ct, check username or password</center>\n"; &html_stop; die; } ], [timeout => sub { $exp->log_file("/dev/null"); $exp->send("/bin/sh\n"); $exp->send("/usr/bin/last -10 $username\n"); $exp->log_file(\&formatoutput); $exp->send("/bin/cat /etc/passwd\n"); } ], ); $exp->soft_close(); #gentially close this object, it has been nice to +us we hope sub formatoutput { my $input = shift; chomp($input); $input =~ tr/\r//; $input =~ s/\n/<br>/g; unless( ($input eq "\$ ") || ($input eq "\/") || ($input eq "bin/sh") || ($input eq "bin\/sh") || ($input eq "/bin/sh") || ($input eq "/bin/sh ") ) # things not to print go in this ^ statement { print"<b>${input}</b><p>\n"; } } sub telnet_login { my ($username, $password, $host, $exp) = @_; my $TELNET = "/usr/bin/telnet"; my $timeout = 7; $$exp->raw_pty(1); #treat this terminal as a raw file $$exp->log_stdout(0); #do not show terminal output to STD Out $$exp->spawn("$TELNET $host") || die "Cannot open telnet\n"; # try + to startup $TELNET $$exp->expect($timeout, ['ogin:', sub { $$exp->send("$username\n"); exp_conti +nue; } ], ['assword:', sub { $$exp->send("$password\n"); } ], [timeout => sub { die "<center>ERROR: <BR>A timeout h +ast occured at login</center>\n"; } ], ); } </html>
as you can tell from my code I am sending everything up until the cat'ing of /etc/passwd to /dev/null but that doesnt stop my output from looking like this:
bin/sh testuser pts/5 localhost Mon Jun 16 09:16 still logged in testuser pts/5 localhost Mon Jun 16 09:14 - 09:15 (00:00) testuser pts/5 localhost Mon Jun 16 09:09 - 09:09 (00:00) testuser pts/5 localhost Mon Jun 16 09:08 - 09:08 (00:00) testuser pts/6 localhost Mon Jun 16 09:07 - 09:08 (00:00) testuser pts/5 localhost Mon Jun 16 09:07 - 09:07 (00:00) testuser pts/5 testuser Fri Jun 13 15:54 - 15:55 (00:00) testuser pts/5 testuser Fri Jun 13 15:53 - 15:54 (00:00) testuser pts/5 testuser Fri Jun 13 15:52 - 15:53 (00:00) testuser pts/5 testuser Fri Jun 13 15:52 - 15:52 (00:00) root:x:0:1::/:/sbin/sh daemon:x:1:1::/: bin:x:2:2::/usr/bin: sys:x:3:3::/: adm:x:4:4:Admin:/var/adm: lp:x:71:8:Line Printer Admin:/usr/spool/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico listen:x:37:4:Network Admin:/usr/net/nls: nobody:x:60001:60001:Nobody:/: noaccess:x:60002:60002:No Access User:/: nobody4:x:65534:65534:SunOS 4.x Nobody:/: fast:x:204:1:FastTrack Server:/web:/bin/sh postfix:x:100:1:postfix:/dev/null:/bin/false vscan:x:1001:10:Amavis User:/usr/local/encap/amavis-perl-11:/bin/sh sshd:x:22000:22000:SSHD privsep User:/var/empty:/bin/false
any thoughts as to why I get both the bin/sh and the last sent to my output?

Replies are listed 'Best First'.
Re: phantom log from hell?
by pzbagel (Chaplain) on Jun 16, 2003 at 15:20 UTC

    Well, it looks like you are attempting to set autoflush in line 4 but you forgot the $ in front of the |. Or is that just a bad pasting job?

    Also, regarding that big unless statement. Is that an attempt to get rid of the /bin/sh in the output? You would do better with a single regex. Something like:

    unless $input=~m|^(?:/bin/sh|\$\s|/)|;

    Bye

      bad pasteing job, but i apprecate your looking for errors though
      and the reg-ex, i apprecate that a lot
Re: phantom log from hell?
by monsieur_champs (Curate) on Jun 16, 2003 at 15:25 UTC

    Seems to me that the log you're receiving at the top of your desired output come from the STDERR. Try something like open STDERR, '/dev/null'; in your code. This should stop the "Phantom Log From Hell"tm.

    Alternativelly, you could consider redirecting (by other means)the telnet standard error to /dev/null.

    I think you should try to use the Net::Telnet module instead of the Expect module.

    Hope that helps.


    updated:The STDERR is used as the default way to tell things that aren't the expected output of a program. I tought that telnet or expect could be using the STDERR to output unexpected information about the operation status. Sorry if this does not work.

    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Just Another Perl Monk

      i will look into Net::Telnet, but as for the STDERR i do not think that is correct, i will test it out, but shouldnt stderr have some sort of failure messages in it and not output that i asked for (admitedly sent to /dev/null) as removing the log_file("/dev/null"); line does not change my output at all. but i apprecate your assistance.

      updated:implemented the stderr part, no dice sorry same problems. output stays untill i comment out the print statement

Re: phantom log from hell?
by bbfu (Curate) on Jun 17, 2003 at 00:32 UTC

    Instead of opening the log_file to /dev/null, you might instead try calling $expect->log_file(undef); to turn off logging. Then, just before setting log_file to your callback, you might want to call $expect->clear_accum(); to flush any waiting output.

    bbfu
    Black flowers blossom
    Fearless on my breath

      i apprecate the help, however, your solution, like all the others, looks like it should work, but it just doesnt for some strange reason.
Re: phantom log from hell?
by pzbagel (Chaplain) on Jun 16, 2003 at 17:24 UTC

    According to the Expect.pm documentation, output is sent to STDOUT by default, perhaps you have to disable this by calling:

    $exp->log_stdout(0);
      thanks for the help, but i do have that line in there (admintedly it is burried in a mess of other stuff (the expect_login function... but perhaps it isnt treating it like i thought it would in the function even though i passed by reffernce.) i will look into that, again thanks for your help

        Have you tried calling $exp->log_file(undef) before you do $exp->log_file(\&formatoutput)? That closes the current log_file that you specified which should theoretically flush the buffer before you open the new log_file.

        HTH