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

There is definatly a problem somewhere in my code..... i think as it doesnt seem to be behaving the way that it should.
things that should not be being logged are, and things that i am explicitly telling not to exist are existent..... and previous help 266197 while suggesting ideas that should have worked did not do anything other then try really hard to help. at this point i am beginning to wonder if it is Expect.pm 's loging function that is the problem and not my code.... but as an uber-novice i am enclined to blame myself due to shear likelyhood that i am totally not doing something correctly, as i do that alot.

here is my code

#!/usr/bin/perl -w use strict; use Expect; $|++; open STDERR, "/dev/null"; my $username="testuser"; my $password="password"; my $host="localhost"; my $timeout=5; my $exp = new Expect; $exp->log_file(undef); print "Content-type: text/html\n\n<html>"; 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->send("/bin/sh\n"); $exp->send("/usr/bin/last -5 $username\n"); $exp->clear_accum(); $exp->log_file(\&formatoutput); $exp->send("/bin/cat /etc/passwd\n"); } ], ); $exp->soft_close(); sub formatoutput { my $input = shift; chomp($input); $input =~ tr/\r//; $input =~ s|\n|<br>\n|g; unless ($input =~ m!^(?:bin/sh|\$\s|/|\$)!) { print"${input}<p>\n"; } } sub telnet_login { my ($username, $password, $host, $exp) = @_; $$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("/usr/bin/telnet $host") || die "Cannot open telnet\n +"; $$exp->expect(7, ['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 occure d at login</center>\n"; } ], ); } </html>
here is expect's logging function from Expect.pm v1.15, im not sure if this will help at all but other then wasting space what harm can it do to post it?
sub log_file { my $self = shift; return(${*$self}{exp_Log_File}) if not @_; # we got no param, return filehandle my $file = shift; my $mode = shift || "a"; if (${*$self}{exp_Log_File} and ref(${*$self}{exp_Log_File}) ne 'C +ODE') { close(${*$self}{exp_Log_File}); ${*$self}{exp_Log_File} = undef; } return if (not $file); my $fh = $file; if (not ref($file)) { # it's a filename $fh = new IO::File $file, $mode or croak "Cannot open logfile $file: $!"; } if (ref($file) ne 'CODE') { croak "Given logfile doesn't have a 'print' method" if not $fh->can("print"); $fh->autoflush(1); # so logfile is up to date } ${*$self}{exp_Log_File} = $fh; }
here is my output, (please note it contains several differnt runs to show the differences in output i get when all i do is run it again, without changing any code at all) while the changes in times and ptys and loggin status make sense the changing of where newlines are and the addition of $'s occasionally has me confused
testuser pts/6 localhost Tue Jun 17 09:16 still logged in testuser pts/5 localhost Tue Jun 17 09:16 still logged in testuser pts/4 localhost Mon Jun 16 15:55 - 15:55 (00:00) testuser pts/4 localhost Mon Jun 16 15:53 - 15:53 (00:00) testuser pts/4 localhost Mon Jun 16 15:53 - 15:53 (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 --------- testuser pts/5 localhost Tue Jun 17 09:17 still logged in testuser pts/6 localhost Tue Jun 17 09:16 - 09:17 (00:00) testuser pts/5 localhost Tue Jun 17 09:16 - 09:17 (00:00) testuser pts/4 localhost Mon Jun 16 15:55 - 15:55 (00:00) testuser pts/4 localhost Mon Jun 16 15:53 - 15:53 (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 ------- testuser pts/5 localhost Tue Jun 17 09:18 still logged in testuser pts/5 localhost Tue Jun 17 09:17 - 09:18 (00:00) testuser pts/6 localhost Tue Jun 17 09:16 - 09:17 (00:00) testuser pts/5 localhost Tue Jun 17 09:16 - 09:17 (00:00) testuser pts/4 localhost Mon Jun 16 15:55 - 15:55 (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 ----- testuser pts/6 localhost Tue Jun 17 09:19 still logged in testuser pts/5 localhost Tue Jun 17 09:19 still logged in testuser pts/5 localhost Tue Jun 17 09:18 - 09:19 (00:00) testuser pts/5 localhost Tue Jun 17 09:17 - 09:18 (00:00) testuser pts/6 localhost Tue Jun 17 09:16 - 09:17 (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 ------ testuser pts/5 localhost Tue Jun 17 09:20 still logged in testuser pts/6 localhost Tue Jun 17 09:20 - 09:20 (00:00) testuser pts/8 localhost Tue Jun 17 09:20 - 09:20 (00:00) testuser pts/5 localhost Tue Jun 17 09:20 - 09:20 (00:00) testuser pts/7 localhost Tue Jun 17 09:19 - 09:20 (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 ----- testuser pts/5 localhost Tue Jun 17 09:29 still logged in testuser pts/5 localhost Tue Jun 17 09:28 - 09:29 (00:00) testuser pts/5 localhost Tue Jun 17 09:28 - 09:28 (00:00) testuser pts/5 localhost Tue Jun 17 09:27 - 09:27 (00:00) testuser pts/5 localhost Tue Jun 17 09:27 - 09:27 (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 $
while i do have a workaround for removing the echoing of bin/sh to the screen even that was showing up in there. and ultimatly this is going to be deployed across a few different platforms (irix, solaris, linux) and as a result i cannot count on knowing what the prompt (the $ in solaris which is my current test enviroment) will be for parsing out and in general i should be having to do that, should i? i mean if the logging was working the way i thought i was telling it to then that output should be sent nowhere.
really apprecate the time spent looking at this, and would like to thank you in advance for anyhelp that you supply.

Replies are listed 'Best First'.
Re: insconsistency and things that should not be
by talexb (Chancellor) on Jun 17, 2003 at 20:27 UTC

    Try writing the smallest possible fragment of code which demonstrates the problem you believe that you are having. We may be generous with our time, but not many kind monks have the time to wade through all of the code and program output that you have posted.

    --t. alex
    Life is short: get busy!
      thanks, i shall try to be more breif in future posts. i was attempting to be as short as possible, but i aparently failed. but i do apprecate your willingness to advise me on this methodology of posting questions
Re: insconsistency and things that should not be
by graff (Chancellor) on Jun 18, 2003 at 06:06 UTC
    This may not be the only problem, but at least it was easy to spot:
    #### this is probably not what you want: open STDERR, "/dev/null"; #### it should be: open STDERR, ">/dev/null";
    Beyond that, I have to agree with the initial reply -- show a smaller, more focused example where there is a particular output that you intend, and you get something unintended.
      thanks for the help, i really apprecate it and i will fix that error
      jcpunk

      by the way thanks