CGI calling an expect script problem

E

erik

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();
 
E

erik

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
 
G

Gunnar Hjalmarsson

erik said:
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();
 
A

A. Sinan Unur

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
 
B

Brian McCauley

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.
 
A

A. Sinan Unur

A. Sinan Unur wrote:



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

A. Sinan Unur

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
 
E

erik

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top