tags:

views:

64

answers:

3

I am new to perl and I want to use screen input on my script. here is my script and I want the IOS command to be enter from keyboard. can some one show me where I am wrong.the problem i have now the script not read my keyboard input, I am not sure if work on my case. thanks!!

# ### Show ######################################################### 
               $cmd = <STDIN>;
                chomp ($cmd);
               $show_error = "";
              if ($ssh_switch eq 'yes') {
                   ssh_access();
               }
               print "\n",h2($host . ' - ' . $cmd);
                @output=$cmd;
                print hr(),"\n";
               }
}
#########################################################################
+3  A: 

CGI is designed to take in a complete HTTP request and then spit out a complete HTTP response.

It simply doesn't work interactively.

If you want to write a script that expects keyboard input, then don't use CGI.

David Dorward
not true. CGI can read parameters from an arbitrary file, or you can read them yourself and provide them to CGI and use the module to produce HTML output with no HTTP involved.
ysth
Yes, but not interactively.
David Dorward
+1  A: 

CGI does allow input through STDIN; try CGI->new(\*STDIN).

Though it may not be how you want to enter things. Can you give an example of what your input looks like?

Ah, it looks to me like you want to either:

  1. run your script from the command line, like: perl scriptname 'Submit=1&Devices=router1&Devices=router2' and then provide your cisco commands on STDIN (and get html output, which may be inconvenient), or

  2. run your script in a browser, in which case you should replace the STDIN usage with an input tag for entering commands and get the commands from that named parameter

Looking again, I see you already have an input tag "search", you just aren't using it. Try replacing <STDIN> with $cgi->param('search') and adding search to the "make sure data was input" check.

ysth
thanks much for your time, I want running different cisco commands like, " show version" or " show interfaces" like that.as input then I want to run that commands on multiple routers.
@raindrop18: updated; not completely clear on what you want
ysth
I am very interested on second approach.would you mind indicate me which line on my script I should correct/edit to do what are you suggested? thanks much again for your time.
@raindrop18: updated
ysth
here is what I edited,# ### Input from the screen - make sure data was inputif ($cgi->param('Submit') ne "" and $cgi->param('Devices') ne "" and $cgi->param('search')) {----and then$cmd = $cgi->param('search');##the script run but there is no out also I see this error on the page( Pragma: no-cache Content-Type: text/html; charset=ISO-8859-1 )- looks has some progress because the login gone well butno output,may be the test entery not pass to the device.
just to let you know start working, thanks much for your assistance.the only issue I have now, i couldn't run the script on multiple devices.I have this line on my script " -multiple => 'true'," but not sure why it's not effective.
+1  A: 

In fact, CGI does uses STDIN. It is used to pass the body of POST request. Try this script

#!/usr/bin/perl
print "Content-Type: text/plain\r\n\r\nYou entered: ";
print while <STDIN>;

and POST some data to it, e.g.

$ echo "Just test" | POST http://localhost/yourscript.pl
You entered: Just test

(POST is a program from LWP CPAN distribution)

So you can direct your script with commands read from STDIN, although it is very unsecure as is!

pwes
Indeed this is a common way of testing CGIs - by running them on the command-line, piping input into stdin and seeing what is printed on stdout.
Ether
thanks for your time. did you see any line can be add on my script. I am perl learner not know much. I want to run simple command so no worry much for security.