Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to deal with multiple functions served from only one input field?

by taint (Chaplain)
on Nov 15, 2013 at 00:38 UTC ( [id://1062671]=perlquestion: print w/replies, xml ) Need Help??

taint has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks.

I've been struggling (needlessly, I'm sure) with this so long. I wouldn't be able to do it correctly, if the answer was right in front of my face. It appears I have a bad perpensity(sp) for making the easy things overly complicated. :(

Anyway, to my question;
I have a single text field that, depending on the content placed into the field, will get directed to the correct sub/function.

Specifically; The 2 subs/functions are intended to either receive a Domain Name, or an IP address. I've built the subs for the functions. But sadly, I can only seem to make it work with one, or the other -- not both. FWIW I'm using Net::Whois::Raw and Net::Whois::ARIN

Here's the bulk of it:

#!/usr/bin/perl -Tw print qq(content-type:text/html; charset=utf-8\n\n); use strict; use CGI; use Net::Whois::Raw; # Net::Whois::Raw options ... my $q = new CGI; my $domname = $q->param("domname"); if (!$domname){print "";}else{ my $text = get_whois($domname, undef, "QRY_LAST"); print $text; }
This much works. But, for the life of me, I can't figure out how to add an additional my $ip = $q->param("ip"); for use with Net::Whois::ARIN, and get it to work.

Here's the bulk of the IP version I have, that works:

#!/usr/bin/perl -wT use CGI; use Net::Whois::ARIN; my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); print "content-type:text/html; charset=utf-8\n\n"; my $q = new CGI; my $ip = $q->param('ip'); if (!$ip){print "";}else{ my @records = $w->network($ip); foreach my $net (@records) { # print ...; } }
I'd greatly appreciate some guidance on this. It's driving me loony (assuming I'm not already there).

Thank you for all your consideration, and apologies if this all seems silly fodder.

--Chris

#!/usr/bin/perl -Tw
use Perl::Always or die;
my $perl_version = (5.12.5);
print $perl_version;

Replies are listed 'Best First'.
Re: How to deal with multiple functions served from only one input field?
by ig (Vicar) on Nov 15, 2013 at 03:11 UTC

    You say "a single text field" but your examples refer to two different fields: address and ip, so I am unsure what you want to do.

    If you have text fields address and ip, perhaps you could do something like:

    use strict; use warnings; use CGI; use Net::Whois::Raw; use Net::Whois::ARIN; my $q = new CGI; my $domname = $q->param("domname"); my $ip = $q->param('ip'); print "content-type:text/plain; charset=utf-8\n\n"; if (!$domname) { print ""; } else { my $text = get_whois($domname, undef, "QRY_LAST"); print $text; } my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); if (!$ip) { print ""; } else { my @records = $w->network($ip); foreach my $net (@records) { # print ...; } }

    But, if you have a single text field (I have guessed the single field is named 'server'), maybe something like this:

    use strict; use warnings; use CGI; use Net::Whois::Raw; use Net::Whois::ARIN; my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); my $q = new CGI; my $server = $q->param("server"); print "content-type:text/plain; charset=utf-8\n\n"; if($server and $server ne "") { print "get_whois says:\n"; print get_whois($server, undef, "QRY_LAST"); print "\n\n"; print "ARIN says:\n"; my $output = $w->query("n + $server"); print "$output\n"; } else { print "No server\n"; }

    You could check the server parameter to see if it looks like a domain name or a network address then call one function or the other accordingly, or call both functions and report both results, or check the results and only report "successful" results, for whatever you consider a successful lookup.

      ++ to you ig.

      Firstly. Please let me apologize for creating such a jumbled mess of a question. :(

      Secondly. Thank you very much for your offered solutions.

      While it was my intention to use just one form field to process either type of request. My posted attempts came from 2 different scripts. Because I haven't been able to figure out how to splice them together. Hence my question, and the confusing examples -- sorry.

      OK I ran your first example up the flag pole. Here's the whole thing (note I haven't yet attempted to finalize the IP portion):

      #!/usr/bin/perl -w use strict; use warnings; use CGI; use Net::Whois::Raw; use Net::Whois::ARIN; $Net::Whois::Raw::OMIT_MSG = 1; $Net::Whois::Raw::CACHE_TIME = 1; $Net::Whois::Raw::TIMEOUT = 8; my $q = new CGI; my $domname = $q->param("domname"); my $ip = $q->param("ip"); print "content-type:text/html; charset=utf-8\n\n"; print qq(<!DOCTYPE html> <head><title>igwhois</title></head><body>); print qq(<form method="post" action="/igwhois.cgi"> <fieldset> <label for="domname">Domain: </label><input type="text" name="domn +ame" /><br /> <label for="ip">IP: </label><input type="text" name="ip" /> </fieldset> </form>); if (!$domname) { print ""; } else { my $text = get_whois($domname, undef, "QRY_LAST"); print qq(<pre>); print $text; print qq(</pre>); } my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); if (!$ip) { print ""; } else { my @records = $w->network($ip); foreach my $net (@records) { # print ...; } } print qq(</body></html>);
      Sadly. Entering a domain name in the top field, and hitting enter, does nothing. It doesn't even post a query.
      However. If I comment out the ip text field, and try it again. It works.
      ???

      In a sick sort of way; this is refreshing. As I attempted a similar solution to the one you provided, and it reacted exactly the same.
      So I don't feel quite so stupid now -- which is not to imply you are.

      I'll take a shot at your second example, and see if I can get that one to work

      Thank you again, ig, For all your time, and effort! I really appreciate it.

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;
        Sadly. Entering a domain name in the top field, and hitting enter, does nothing. It doesn't even post a query. However. If I comment out the ip text field, and try it again. It works. ???

        If you add a submit button, then Enter will submit the form even if there are multiple text inputs, at least in Firefox 25.0 (the only browser I tested). Without a submit button, Enter submits the form only if there is a single text input but doesn't submit the form if there are multiple text inputs.

        The following works for me:

        #!/strawberry/perl/bin/perl.exe use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use Net::Whois::Raw; use Net::Whois::ARIN; $Net::Whois::Raw::OMIT_MSG = 1; $Net::Whois::Raw::CACHE_TIME = 1; $Net::Whois::Raw::TIMEOUT = 8; my $q = new CGI; my $domname = $q->param("domname"); my $ip = $q->param("ip"); print "content-type:text/html; charset=utf-8\n\n"; print qq(<!DOCTYPE html> <head><title>igwhois</title></head><body>); print qq( <form method="post"> <fieldset> <label for="domname">Domain: </label><input type="text" name="domn +ame" /><br /> <label for="ip">IP: </label><input type="text" name="ip" /> </fieldset> <input type="submit" value="Submit"> </form> ); if (!$domname) { print ""; } else { my $text = get_whois($domname, undef, "QRY_LAST"); print qq(<pre>); print $text; print qq(</pre>); } my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); if (!$ip) { print ""; } else { my $text = $w->query("n + $ip"); print qq(<pre>); print $text; print qq(</pre>); } print qq(</body></html>);
Re: How to deal with multiple functions served from only one input field?
by boftx (Deacon) on Nov 15, 2013 at 00:51 UTC

    Sorry for not addressing your actual question, but I strongly suggest you run your input through some kind of untainting (especially since you are using taint mode) before doing anything else with it. You will likely save yourself some grief by not using/sending what is obvious crap (bogus user input) when it is present to begin with. Testing for a valid IP address format is fairly trivial, and there are several patterns for domain names that can be used.

    Trust me on this. :)

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      "Trust me on this. :) "

      I do, trust me. :)

      Thank you very much boftx for your thoughtful reply.
      This has already been accomplished. I really should have also included it -- crap!

      So, I'll append it now:
      REVISED

      use CGI; use Net::Whois::ARIN; use Regexp::Common qw /net/; my $w = Net::Whois::ARIN->new( host => 'whois.arin.net', port => 43, timeout => 30, ); print "content-type:text/html; charset=utf-8\n\n"; print $head; my $q = new CGI; my $ip = $q->param('ip'); if (!$ip){print $formm; }else{ my @records = $w->network($ip=~ /^$RE{net}{IPv4}$/); foreach my $net (@records) { # print ...; } }
      Sorry for the previous omission. :(

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1062671]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-24 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found