CGI calling an expect script problem

Discussion in 'Perl Misc' started by erik, Mar 24, 2005.

  1. erik

    erik Guest

    This code runs great when I pass CLI param's to the script. As soon as
    I try to make it into a CGI, I get 500 errors. Now when I comment out
    all the sub-functions except for print report, it works fine. The CGI
    side does NOT like something I am doing in the first 3 sub-functions. I
    can't figure out why. I even tried doing a "chmod 777 on the telnet
    executable" thinking maybe the nobody apache user could not telnet, but
    that did not do it. Can anyone tell me why CGI does not like my first 3
    sub-functions?? I am pulling my hair out, and I don't have much to
    spare.

    Here is my code:


    #!/usr/bin/perl
    #################################################################
    # Global Variables #
    #################################################################
    use warnings;
    use Expect;
    use CGI(":standard");

    #$Expect::Log_Stdout = 0;
    $device = param("device");
    $username = param("username");
    $password = param("password");
    $enable = "enable";
    $enable_password = param("enable_password");



    #################################################################
    # Create Session with router #
    #################################################################

    sub spawn_session
    {
    my $timeout ="4";
    #open session with router
    $command = Expect->spawn("telnet $device");
    #Wait for password prompt or send error.
    $command->expect($timeout, -re => "Username:") or do
    {
    print "\nFailed to get username prompt\n";
    exit 2;
    };

    #Send Tacacs Username to router, exit if Username is incorrect
    print $command "$username\r";

    $command->expect($timeout, -re => "Password:") or do
    {
    print "\nFailed to get password prompt\n";
    exit 3;
    };

    #Send password to router, exit if password is incorrect
    print $command "$password\r";

    $command->expect($timeout, -re => ">") or do
    {
    print "\nDid not get a \> in prompt\n";
    exit 4;
    };

    #Send enable pass to router, exit if password is incorrect
    print $command "$enable\r";

    $command->expect($timeout, -re => "assword") or do
    {
    print "\nDid not get a \> in prompt\n";
    exit 5;
    };

    #Send enable pass to router, exit if password is incorrect
    print $command "$enable_password\r";
    }#end sub

    #################################################################
    # Ensures Logging #
    #################################################################
    sub check_logging_settings

    {
    $timeout ="4";
    my $logging = "You have logging to 199.11.1.22";
    my $no_logging = "You are missing logging 199.11.1.22";

    my $bandwidth = "You have a bandwidth statement";
    my $no_bandwidth = "You are missing a bandwidth statement";

    my $vtypass = "You have passwords on vty\,con\,and aux";
    my $no_vtypass = "You are missing a password on either vty\,con\, or
    aux";

    my $aaa = "You have 'aaa new model' setup";
    my $no_aaa = "You are missing 'aaa new model'";

    my $voyence = "You have the voyence SNMP trap setup correctly";
    my $no_voyence = "You are missing the voyence SNMP trap";

    my $servicepass = "You have 'service password-encryption' setup
    correctly";
    my $no_servicepass = "You are missing 'service password-encryption'
    ";

    my $access_class = "You have an access-class on the vty";
    my $no_access_class = "You are missing the access-class on the vty
    ";


    my $tacacs = "You have tacacs setup properly";
    my $no_tacacs = "You are missing the 'tacacs-server host
    199.x.x.41'";

    $command->clear_accum();
    print $command "show config\r";
    unless ($command->expect($timeout, -re, 'end')) {
    return "Never got telnet prompt".$command->exp_error()."\n";
    }
    my $read = $command->exp_before();
    my @read = split ('\n', $read);
    $arrayLENGTH = scalar(@read);
    print "Array Length = $arrayLENGTH\n";

    #HERE WE CHECK for the TACACS server command
    if ($read =~ /tacacs-server host 199.x.x.41.*/)
    {
    push (@tacacs_tests, $tacacs);
    $tacacs_action = "OK";
    }
    else{
    push (@tacacs_tests, $no_tacacs);
    $tacacs_action = "FAIL";
    }


    }

    #################################################################
    # Exits the Telnet Session #
    #################################################################
    sub exit_session
    {
    print $command "exit\r";
    $command->soft_close();
    }#end sub
    #################################################################
    # Prints the failed test error descriptions #
    #################################################################
    sub print_error_report
    {
    print <<html1
    Content-type: text/html\n\n
    <html><head><title>QA REPORT</title></head>
    <body bgcolor="#ffffff">
    <p>
    <h2>QA Results</h2>
    <ul>
    <body>
    <b>$device</b><br>
    <b> @tacacs_tests .......... $tacacs_action </b><br>
    </body>
    </html>
    html1
    }

    #################################################################
    # Main Program Module #
    #################################################################
    spawn_session(); (If I comment out here CGI WORKS)
    check_logging_settings(); (If I comment out here CGI WORKS)
    exit_session(); (If I comment out here CGI WORKS)
    print_error_report();
     
    erik, Mar 24, 2005
    #1
    1. Advertising

  2. erik

    erik Guest

    yeah I did that, and couldn't make a whole lot of sense out of it.

    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20]
    (Missing semicolon on previous li
    ne?), referer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] Bareword found
    where operator expected a
    t /www/cgi-bin/qa.pl line 245, near "<h2>QA", referer:
    http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20]
    (Missing operator before QA?), re
    ferer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] Bareword found
    where operator expected a
    t /www/cgi-bin/qa.pl line 249, near "</body", referer:
    http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] (Might be a
    runaway multi-line // stri
    ng starting on line 248), referer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20]
    (Missing operator before body?),
    referer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] syntax error
    at /www/cgi-bin/qa.pl line
    26, near "$timeout ="", referer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] Unmatched
    right curly bracket at /www/cg
    i-bin/qa.pl line 251, at end of line, referer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] Execution of
    /www/cgi-bin/qa.pl aborted
    due to compilation errors., referer: http://hank:8000/qa.htm
    [Mon Mar 21 21:06:46 2005] [error] [client 192.168.1.20] Premature end
    of script headers: qa.pl,
    referer: http://hank:8000/qa.htm
     
    erik, Mar 24, 2005
    #2
    1. Advertising

  3. erik wrote:
    > This code runs great when I pass CLI param's to the script. As soon as
    > I try to make it into a CGI, I get 500 errors.


    The first thing to do "to make it into a CGI" is to ensure that it
    prints a content-type header before any content is printed to STDOUT.

    print header();

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 24, 2005
    #3
  4. "erik" <> wrote in news:1111632574.038817.126490
    @o13g2000cwo.googlegroups.com:

    > yeah I did that, and couldn't make a whole lot of sense out of it.


    Please quote some context when you are replying. Otherwise, it is hard
    for others to make sense of what you are saying.

    > [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20]
    > (Missing semicolon on previous line?), referer:
    > http://hank:8000/qa.htm
    > [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20] Bareword
    > found where operator expected at /www/cgi-bin/qa.pl line 245,
    > near "<h2>QA", referer: http://hank:8000/qa.htm


    [ ... after wasting some time trying to locate the original message ...
    ]

    The script you posted does not have 245 lines!

    Quoting the original code:

    sub print_error_report
    {
    print <<html1
    Content-type: text/html\n\n
    <html><head><title>QA REPORT</title></head>
    <body bgcolor="#ffffff">
    <p>
    <h2>QA Results</h2>
    <ul>
    <body>
    <b>$device</b><br>
    <b> @tacacs_tests .......... $tacacs_action </b><br>
    </body>
    </html>
    html1
    }

    You are missing a semi-colon above.

    Please read the posting guidelines from some hints on how to help others
    help you.

    Sinan
     
    A. Sinan Unur, Mar 24, 2005
    #4
  5. A. Sinan Unur wrote:


    > sub print_error_report
    > {
    > print <<html1
    > Content-type: text/html\n\n
    > <html><head><title>QA REPORT</title></head>
    > <body bgcolor="#ffffff">
    > <p>
    > <h2>QA Results</h2>
    > <ul>
    > <body>
    > <b>$device</b><br>
    > <b> @tacacs_tests .......... $tacacs_action </b><br>
    > </body>
    > </html>
    > html1
    > }
    >
    > You are missing a semi-colon above.


    Yes and no. Most people would include a semi-colon but it is not
    _required_ after the last statement in a block.
     
    Brian McCauley, Mar 24, 2005
    #5
  6. Brian McCauley <> wrote in news:d1ucd6$bhp$1
    @sun3.bham.ac.uk:

    > A. Sinan Unur wrote:
    >
    >
    >> sub print_error_report
    >> {

    ....
    >> }
    >>
    >> You are missing a semi-colon above.

    >
    > Yes and no. Most people would include a semi-colon but it is not
    > _required_ after the last statement in a block.


    Whooops! I forgot that. I was going through the code line-by-line to see
    something that matched the first few lines of the error message the OP
    posted. I did not want to try and setup Apache on Windows to use Cygwin
    Perl just to test this script.

    Thank you for the correction.

    Sinan
     
    A. Sinan Unur, Mar 24, 2005
    #6
  7. "erik" <> wrote in news:1111632574.038817.126490
    @o13g2000cwo.googlegroups.com:

    > yeah I did that, and couldn't make a whole lot of sense out of it.
    >
    > [Mon Mar 21 21:06:41 2005] [error] [client 192.168.1.20]
    > (Missing semicolon on previous line?), referer:
    > http://hank:8000/qa.htm


    I cleaned up your script a little bit so it does not exit in the middle
    of a CGI process and passes strict. You can use this as a starting point
    to better debug what is going on. The code below has not been tested.

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Expect;
    use CGI(":standard");

    use constant ENABLE => 'enable';

    my $device = param("device");
    my $username = param("username");
    my $password = param("password");
    my $enable_password = param("enable_password");

    my $command;

    #################################################################
    # Create Session with router #
    #################################################################

    sub spawn_session {
    my $timeout = 4;
    $command = Expect->spawn("telnet $device");
    $command->expect($timeout, -re => "Username:")
    or return 'Failed to get username prompt';

    print $command "$username\r";
    $command->expect($timeout, -re => "Password:")
    or return 'Failed to get password prompt';

    print $command "$password\r";

    $command->expect($timeout, -re => ">")
    or return 'Did not get a \> in prompt';

    print $command ENABLE."\r";

    $command->expect($timeout, -re => "assword")
    or return 'Did not get a \> in prompt';

    print $command "$enable_password\r";
    return;
    }

    #################################################################
    # Ensures Logging #
    #################################################################

    sub check_logging_settings {
    my ($tacacs_action, $tacacs_tests) = @_;
    my $timeout = 4;

    my %messages = (
    logging => 'You have logging to 199.11.1.22',
    no_logging => 'You are missing logging 199.11.1.22',
    bandwidth => 'You have a bandwidth statement',
    no_bandwidth => 'You are missing a bandwidth statement',
    vtypass => 'You have passwords on vty\,con\,and aux',
    no_vtypass => 'You are missing a password on either vty\,con\,
    or aux',
    aaa => "You have 'aaa new model' setup",
    no_aaa => "You are missing 'aaa new model'",
    voyence => 'You have the voyence SNMP trap setup
    correctly',
    no_voyence => 'You are missing the voyence SNMP trap',
    servicepass => "You have 'service password-encryption' setup
    correctly",
    no_servicepass => "You are missing 'service password-
    encryption'",
    access_class => 'You have an access-class on the vty',
    no_access_class => 'You are missing the access-class on the
    vty',
    tacacs => 'You have tacacs setup properly',
    no_tacacs => "You are missing the 'tacacs-server host
    199.x.x.41'",
    );

    $command->clear_accum();
    print $command "show config\r";
    unless ($command->expect($timeout, -re, 'end')) {
    return "Never got telnet prompt: ".$command->exp_error();
    }

    my $read = $command->exp_before();
    my @read = split ('\n', $read);

    if ($read =~ /tacacs-server host 199.x.x.41.*/) {
    push @{ $tacacs_tests }, $messages{tacacs};
    $$tacacs_action = "OK";
    } else {
    push @{ $tacacs_tests }, $messages{no_tacacs};
    $$tacacs_action = "FAIL";
    }
    }

    #################################################################
    # Exits the Telnet Session #
    #################################################################
    sub exit_session {
    print $command "exit\r";
    $command->soft_close();
    }

    #################################################################
    # Prints the failed test error descriptions #
    #################################################################
    sub print_error_report {
    my ($tacacs_action, @tacacs_tests) = @_;

    print header, <<HTML1
    <html><head><title>QA REPORT</title></head>
    <body bgcolor="#ffffff">
    <p>
    <h2>QA Results</h2>
    <ul>
    <body>
    <b>$device</b><br>
    <b> @tacacs_tests .......... $tacacs_action </b><br>
    </body>
    </html>
    HTML1
    ;
    }

    sub show_error {
    my $err = shift;

    print header, <<HTML
    <html><title>Error Spawning Session</title>
    <body><p>Error: $err</p></body>
    </html>
    HTML
    }


    #################################################################
    # Main Program Module #
    #################################################################

    if( my $err = spawn_session() ) {
    show_error($err);
    } else {
    my @tacacs_tests;
    my $tacacs_action;
    if($err = check_logging_settings(\$tacacs_action, \@tacacs_tests) )
    {
    show_error($err);
    } else {
    exit_session();
    print_error_report($tacacs_action, @tacacs_tests);
    }
    }

    __END__


    Sinan
     
    A. Sinan Unur, Mar 24, 2005
    #7
  8. erik

    erik Guest

    That is nice and clean. Thank you! I will add that stuff.

    I just found my issue. The issue is that I had PLAIN OLD print
    statements in the first 3 subscripts. As soon as I commented out every
    PLAIN OLD print, it suddenly worked.
     
    erik, Mar 24, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shailan
    Replies:
    2
    Views:
    988
    Shailan
    Dec 15, 2003
  2. Simon Strandgaard

    how to expect eof with expect+pty

    Simon Strandgaard, Dec 20, 2006, in forum: Ruby
    Replies:
    4
    Views:
    358
    Simon Strandgaard
    Dec 20, 2006
  3. Phil
    Replies:
    0
    Views:
    168
  4. Replies:
    12
    Views:
    382
    alpha_beta_release
    Aug 28, 2006
  5. kath
    Replies:
    4
    Views:
    633
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page