• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

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/_vti_script/cmdlist" 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_script/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','xxxxxxx');
    $t->cmd("terminal length 0");
    #my @lines = $t->cmd($cmdlist[$cmd]);
    my @lines = $t->cmd($cmd);


    $t->cmd("");
    $t->close;
    return @lines;
}
0
ashvillerob
Asked:
ashvillerob
  • 5
  • 2
1 Solution
 
kblack05Commented:
This would most likely be the segment of code
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
0
 
Perl_DiverCommented:
<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.
0
 
ashvillerobAuthor Commented:
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"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Perl_DiverCommented:
print $q->header,
        $q->start_html ('IP Engineering'),
        $q->start_form,
       "Enter MAC address: ",
        $q->textfield(-name=>'mac_add',
                       -size=>30,
                       -maxlength=>20
        ),
        '<br />',        
       "Select CMTS: ",
        $q->popup_menu('ip', get_ip_list()),
        $q->popup_menu(-name=>'cmd',
                       -values=>\@cmdlist,
                       -labels=>\%cmdlabels,
        ),
        $q->submit,

change -size and -maxlength to your requirements. And I assume you will have to pass that to the get_config sub routine:

get_config($q->param('ip'),$q->param('cmd'),$q->param('mac_add'))

and import it here:

sub get_config {
    my $ip = shift;
    my $cmd = shift;
    my $mac_add = shift;

but how you use it after that I don't  know.
0
 
Perl_DiverCommented:
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.
0
 
Perl_DiverCommented:
is this question dead?
0
 
ashvillerobAuthor Commented:
No, just waiting on a complete answer. I need the exact code!
0
 
Perl_DiverCommented:
OK,  good luck, looks like this thread is going to die though.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now