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

In my previous question Expect, STDout, and formating I was having difficulty getting output from expect. thanks to the code snipits provided by my fellow monks I am now able to get output, but alas there is a problem making if make sense. normally i wouldnt post a question that could be titled "why wont if make sense" but alas I am totally stuck.

my problems stem from the inconsistencys of expects loging functionality. ie occassionally /bin/sh will be logged as /\nbin/sh. And to top it off dispite having duplicated every case i can thing would effect my output and compensate for the problems no luck was achieved.
hopefully my code will help explain my question more clearly

$exp->log_file(\&formatoutput); $exp->send("$CAT /etc/passwd\n"); ...... sub formatoutput { my $input = shift; chomp($input); $input =~ tr/\r//; if( ($input ne "$CAT /etc/passwd ") || ($input ne '/bin/sh') || ($input ne "/bin/sh") || ($input ne '\$ ') || ($input ne "\$ ") || ($input ne '/') || ($input ne "/") || ($input ne 'bin/sh') || ($input ne "bin/sh") ) { print"${input}\n\n"; } }
the output that I get is
/ bin/sh $ 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/spoo +l/lp: uucp:x:5:5:uucp Admin:/usr/lib/uucp: nuucp:x:9:9:uucp Admin:/va +r/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:/bi +n/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 $
neither of the $'s or the / or bin/sh should be showing up and last run i had the $ and /bin/sh which should also have not shown up, right?

Replies are listed 'Best First'.
Re: inconsisntency, if, expect, logs
by Thelonius (Priest) on Jun 13, 2003 at 18:17 UTC
    Change the || to &&. Your if will always be true with ||. Also, '/bin/sh' and "/bin/sh" are always the same. Read Quote and Quote-like operators in perlop to see when the kind of quote matters.

    Update: Apparently I didn't make myself clear since y'all are still discussing the intricacies of Expect. The simple fact is that the logic in your if statement is wrong. Try this:

    faulty_logic("fred"); faulty_logic("wilma"); faulty_logic("barney"); sub faulty_logic { my $input = shift; if (($input ne 'fred') || ($input ne 'barney')) { print "IF condition true: input=$input\n"; } } __END__ Output: IF condition true: input=fred IF condition true: input=wilma IF condition true: input=barney
    Think about it.
      You are right about his if, however the if logic being wrong does not explain the newlines being placed in his expect log file. That is what i am after.

      -Waswas
Re: inconsisntency, if, expect, logs
by waswas-fng (Curate) on Jun 13, 2003 at 17:30 UTC
    I have not seen this before, and have used expect a lot. Could you post more of your code (the parts that generate the output, and buid $exp) so I can see if it is the way $exp was formed or the commands were run that is causing this. Also a raw dump of the $exp->log_file written to a file with the portion that you show uncorrectly formatted would be helpful.

    -Waswas
      here is some more code
      my $exp = new Expect; .... $exp->raw_pty(1); $exp->log_stdout(0); $exp->spawn($telnet $host);||die; $exp->expect($timeout, ['ogin:', sub { $exp->send("$username\n"); exp_contin +ue; } ], ['assword:', sub { $exp->send("$password\n"); } ], [timeout => sub { die "<center>ERROR: <BR>A timeout h +ast occured at login</center>\n"; } ], ); $exp->log_file(\&formatoutput); $exp->send("/bin/sh\n");
      that comprises the rest of my expect usage in this file excluding the soft_close I would be willing to post the whole file, but it is around 600 lines and the rest isnt relavent i think....
        The expect flags look correct, I guess my question is if you just dump the $exp->log_file to a normal file does the file contain an extra \n in the /bin/sh line(s)?

        -Waswas