ashvillerob
asked on
Need help modifiying this script
I use the following script to create a simple web interface that allows users to run some basic commands on a cisco router. I need to modify it to allow the user to imput some values.
For example this script will allow a user to run the command(by selecting it from a drop-down menu) "show cable modem". I want to add the funtionality for the user to be able to input a specefic cable modem MAC address.
So the user would select the commnad to run (from the drop down) and then be promted for the MAC address to use with the command.
Her is my current code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use Net::Telnet;
my $q = new CGI;
open FILE,"/inetpub/wwwroot/_vt i_script/c mdlist" or die "Can't open cmdlist $!";
chomp(my @cmdlist=grep{/\S/}<FILE>) ;
my %cmdlabels;
@cmdlabels{0..$#cmdlist}=@ cmdlist;
close FILE;
print $q->header,
$q->start_html ('IP Engineering'),
$q->start_form,
"Select CMTS: ",
$q->popup_menu('ip', get_ip_list()),
$q->popup_menu(-name=>'cmd ',
-values=>\@cmdlist,
-labels=>\%cmdlabels,
),
$q->submit,
$q->end_form
;
print '<hr>',
'<h2>Here Are Your Results For: ', $q->param('ip'), '</h2>',
'<pre>',
get_config($q->param('ip') ,$q->param ('cmd')),
'</pre>'
if $q->param('ip');
print $q->end_html();
sub get_ip_list {
my $myfile="/inetpub/wwwroot/ _vti_scrip t/iplist";
open FILE, $myfile or return "<strong>Error opening ip list $myfile: $!</strong>";
chomp(my @ips = <FILE>);
close FILE;
return \@ips;
}
sub get_config {
my $ip = shift;
my $cmd = shift;
if( $cmd < 0 || $cmd > $#cmdlist ){
return "<strong>invalid command</strong>";
}
my $t = Net::Telnet->new(Timeout => 240,
Prompt => '/.*#/',
Host => $ip); #warning: $q->param('ip') returned from the user could be anything, so they could be tricking you into connecting to any host
$t->login('xxxxxxx','xxxxx xx');
$t->cmd("terminal length 0");
#my @lines = $t->cmd($cmdlist[$cmd]);
my @lines = $t->cmd($cmd);
$t->cmd("");
$t->close;
return @lines;
}
For example this script will allow a user to run the command(by selecting it from a drop-down menu) "show cable modem". I want to add the funtionality for the user to be able to input a specefic cable modem MAC address.
So the user would select the commnad to run (from the drop down) and then be promted for the MAC address to use with the command.
Her is my current code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use Net::Telnet;
my $q = new CGI;
open FILE,"/inetpub/wwwroot/_vt
chomp(my @cmdlist=grep{/\S/}<FILE>)
my %cmdlabels;
@cmdlabels{0..$#cmdlist}=@
close FILE;
print $q->header,
$q->start_html ('IP Engineering'),
$q->start_form,
"Select CMTS: ",
$q->popup_menu('ip', get_ip_list()),
$q->popup_menu(-name=>'cmd
-values=>\@cmdlist,
-labels=>\%cmdlabels,
),
$q->submit,
$q->end_form
;
print '<hr>',
'<h2>Here Are Your Results For: ', $q->param('ip'), '</h2>',
'<pre>',
get_config($q->param('ip')
'</pre>'
if $q->param('ip');
print $q->end_html();
sub get_ip_list {
my $myfile="/inetpub/wwwroot/
open FILE, $myfile or return "<strong>Error opening ip list $myfile: $!</strong>";
chomp(my @ips = <FILE>);
close FILE;
return \@ips;
}
sub get_config {
my $ip = shift;
my $cmd = shift;
if( $cmd < 0 || $cmd > $#cmdlist ){
return "<strong>invalid command</strong>";
}
my $t = Net::Telnet->new(Timeout => 240,
Prompt => '/.*#/',
Host => $ip); #warning: $q->param('ip') returned from the user could be anything, so they could be tricking you into connecting to any host
$t->login('xxxxxxx','xxxxx
$t->cmd("terminal length 0");
#my @lines = $t->cmd($cmdlist[$cmd]);
my @lines = $t->cmd($cmd);
$t->cmd("");
$t->close;
return @lines;
}
<quote>
So the user would select the commnad to run (from the drop down) and then be promted for the MAC address to use with the command.
</quote>
if you want that to happen dynamically in the browser at the time the user selects a command from the menu you need to use javascript. But if the user selects a command and submits the form then is prompted for more input you can do that server side with the script.
So the user would select the commnad to run (from the drop down) and then be promted for the MAC address to use with the command.
</quote>
if you want that to happen dynamically in the browser at the time the user selects a command from the menu you need to use javascript. But if the user selects a command and submits the form then is prompted for more input you can do that server side with the script.
ASKER
Perl Diver
I simple want a text box where the user can enter the "mac address" and the from the drop down they would select the command to run based on that particular "mac address"
I simple want a text box where the user can enter the "mac address" and the from the drop down they would select the command to run based on that particular "mac address"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Additional:
if you need the text entered in the textfield to dynamically affect the list of commands in the drop down menu that will have to be done with javascript or the form data has to sent to the script and a new form generated based on the MAC address entered.
if you need the text entered in the textfield to dynamically affect the list of commands in the drop down menu that will have to be done with javascript or the form data has to sent to the script and a new form generated based on the MAC address entered.
is this question dead?
ASKER
No, just waiting on a complete answer. I need the exact code!
OK, good luck, looks like this thread is going to die though.
you'll need to modify, and/or extend:
$q->popup_menu('ip', get_ip_list()),
$q->popup_menu(-name=>'cmd
-values=>\@cmdlist,
-labels=>\%cmdlabels,
If you have a look here are many examples:
http://www.wiley.com/legacy/compbooks/stein/source.html
Regards,
~K Black