|
Home > Archive > Unix Shell > June 2005 > tell the difference between url and ip address
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
tell the difference between url and ip address
|
|
| juicymixx@mailinator.com 2005-06-24, 6:00 pm |
|
is there a nifty command line way (in bash) to tell the difference
between an ip address and a url?
If I have a shell script that gives out only ip addresses
1) if it's given an ip address - then that's fine, it can just echo it
out
2) if it's given a url - then it needs to look up the ip address and
echo out the result.
The problem is that I don't know of a nice way to tell the difference
between an ip address (like 206.168.1.4), a url (like
www.microsoft.com), and garbage (anything else)...
thanks!
| |
| Chris F.A. Johnson 2005-06-24, 6:00 pm |
| On 2005-06-24, juicymixx@mailinator.com wrote:
>
> is there a nifty command line way (in bash) to tell the difference
> between an ip address and a url?
> If I have a shell script that gives out only ip addresses
> 1) if it's given an ip address - then that's fine, it can just echo it
> out
> 2) if it's given a url - then it needs to look up the ip address and
> echo out the result.
>
> The problem is that I don't know of a nice way to tell the difference
> between an ip address (like 206.168.1.4), a url (like
> www.microsoft.com), and garbage (anything else)...
If the string contains anything other than digits and periods, it's
not an IP address:
case $addr in
*[!0-9.]*) echo "URL: $addr" >&2
url2ip "$addr" ## you provide url2ip to set $url
;;
*) ip=$addr ;;
esac
You can be more restrictive if you think it necessary (e.g., check
that each element is in the 0-255 range).
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
| |
| Kenny McCormack 2005-06-24, 6:00 pm |
| In article <1119647537.106635.95440@g14g2000cwa.googlegroups.com>,
<juicymixx@mailinator.com> wrote:
....
>The problem is that I don't know of a nice way to tell the difference
>between an ip address (like 206.168.1.4), a url (like
>www.microsoft.com), and garbage (anything else)...
www.microsoft.com is not a URL. Sounds to me like you are out of your
depth.
| |
| William Park 2005-06-24, 8:48 pm |
| Kenny McCormack <gazelle@yin.interaccess.com> wrote:
> In article <1119647537.106635.95440@g14g2000cwa.googlegroups.com>,
> <juicymixx@mailinator.com> wrote:
> ...
>
> www.microsoft.com is not a URL. Sounds to me like you are out of your
> depth.
Another homework?
--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
| |
| Lew Pitcher 2005-06-24, 8:48 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
juicymixx@mailinator.com wrote:
> is there a nifty command line way (in bash) to tell the difference
> between an ip address and a url?
> If I have a shell script that gives out only ip addresses
> 1) if it's given an ip address - then that's fine, it can just echo it
> out
> 2) if it's given a url - then it needs to look up the ip address and
> echo out the result.
>
> The problem is that I don't know of a nice way to tell the difference
> between an ip address (like 206.168.1.4), a url (like
> www.microsoft.com), and garbage (anything else)...
FWIW, www.microsoft.com is not an URL. It is a fully qualified domain name,
but it is /not/ an URL.
An URL would be http://www.microsoft.com/
or http://www.w3.org/Addressing/URL/url-spec.txt
or http://www.w3.org/Addressing/rfc1738.txt
- --
Lew Pitcher
Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFCvK7FagVFX4UWr64RAtZCAJ0RaU0+GQ4/IbZFur3cTwIxMNyfFwCfcUeq
DsEINAhL/MbviFWPrl7fF+U=
=HQnk
-----END PGP SIGNATURE-----
| |
| Michael Heiming 2005-06-25, 7:48 am |
| In comp.unix.shell Robert Bonomi <bonomi@host122.r-bonomi.com>:
> In article <u6auo2-4jm.ln1@rogers.com>,
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
[vbcol=seagreen]
> Shall we mention IPv6? <evil grin>
Perl seems to be the easiest, despite requiring Validate::Net,
dunno if it works with IPv6.
perl -MValidate::Net -le'print !Validate::Net->ip("216.239.39.99")&&"in","valid"'
Don't have Validate::Net installed right now.
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 384: it's an ID-10-T error
| |
| Robert Bonomi 2005-06-25, 7:48 am |
| In article <u6auo2-4jm.ln1@rogers.com>,
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>On 2005-06-24, juicymixx@mailinator.com wrote:
>
> If the string contains anything other than digits and periods, it's
> not an IP address:
Shall we mention IPv6? <evil grin>
| |
| John W. Krahn 2005-06-25, 7:48 am |
| Michael Heiming wrote:
> In comp.unix.shell Robert Bonomi <bonomi@host122.r-bonomi.com>:
>
>
>
> PERL seems to be the easiest, despite requiring Validate::Net,
> dunno if it works with IPv6.
>
> PERL -MValidate::Net -le'print !Validate::Net->ip("216.239.39.99")&&"in","valid"'
>
> Don't have Validate::Net installed right now.
You don't have to install it, you can use the Socket module which is installed
with Perl:
perl -MSocket -le'eval { inet_ntoa inet_aton "216.239.39.99" }; print $@ &&
"in", "valid"'
John
--
use Perl;
program
fulfillment
| |
| Michael Heiming 2005-06-25, 7:48 am |
| In comp.unix.shell John W. Krahn <someone@example.com>:
> Michael Heiming wrote:
[..]
[vbcol=seagreen]
[vbcol=seagreen]
> You don't have to install it, you can use the Socket module which is installed
> with Perl:
> PERL -MSocket -le'eval { inet_ntoa inet_aton "216.239.39.99" }; print $@ &&
> "in", "valid"'
Ah, great info! Works fine for IPv4 not for IPv6.
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 73: Daemons did it
| |
| John W. Krahn 2005-06-25, 7:48 am |
| Michael Heiming wrote:
> In comp.unix.shell John W. Krahn <someone@example.com>:
>
>
>
>
> Ah, great info! Works fine for IPv4 not for IPv6.
I see that Validate::Net also only works for IPv4. The main differences
between the two modules are: Validate::Net is pure PERL and validates
addresses by parsing them and only accepts dotted quad numerical addresses
(127.1 is not valid??); Socket is a wrapper around the C library functions and
accepts any valid numeric address (127.1 is valid :-)) and does a DNS lookup
for non-numeric addresses.
Also, from the Validate::Net documentation:
TO DO
This module is not all that completed. Just enough to do some basics.
:-)
John
--
use Perl;
program
fulfillment
| |
| Michael Heiming 2005-06-25, 7:48 am |
| In comp.unix.shell John W. Krahn <someone@example.com>:
> Michael Heiming wrote:
[..][vbcol=seagreen]
[vbcol=seagreen]
> I see that Validate::Net also only works for IPv4. The main differences
Ah see, never said it would, just an idea what one could try.
[..]
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 11: magnetic interference from money/credit cards
| |
| juicymixx@mailinator.com 2005-06-26, 5:53 pm |
| Hey Kenny,
This isn't a 'homework'. I'm just starting out learning how to shell
script... This is one thing I've been wanting to do for a while.
I may have used the wrong terminology. Sorry about it. I just
wanted the get ip addresses only. Not domain names, urls, etc.
Thanks to everyone who has been helping out.
| |
| Ed Morton 2005-06-26, 5:53 pm |
|
Chris F.A. Johnson wrote:
> On 2005-06-24, juicymixx@mailinator.com wrote:
>
>
>
> If the string contains anything other than digits and periods, it's
> not an IP address:
>
> case $addr in
> *[!0-9.]*) echo "URL: $addr" >&2
> url2ip "$addr" ## you provide url2ip to set $url
> ;;
> *) ip=$addr ;;
> esac
That's about what I was going to suggest too, but the OP eventually says
he want's to distinguish between 3 things ("an ip address (like
206.168.1.4), a url (like www.microsoft.com), and garbage (anything
else)") rather than just the 2 he put in the subject and the earlier
part of the posting. We could make the case statement more restrictive
but once you allow for the possibility of the input being "garbage" it's
going to be pretty complicated to do this accurately in shell.
Ed.
| |
| juicymixx@mailinator.com 2005-06-27, 5:53 pm |
| Hey, I must be doing something wrong here. Every time I try the
example you suggested, I'm dumped to a prompt which looks like it's
waiting for something else (not my normal command prompt). Any
suggestions?
Thanks.
| |
| Chris F.A. Johnson 2005-06-27, 5:53 pm |
| On 2005-06-27, juicymixx@mailinator.com wrote:
> Hey, I must be doing something wrong here. Every time I try the
> example you suggested, I'm dumped to a prompt which looks like it's
> waiting for something else (not my normal command prompt). Any
> suggestions?
What example? Posted by whom?
Please quote relevant portions of the message to are following up.
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
| |
| Robert Maas, see http://tinyurl.com/uh3t 2005-06-29, 5:53 pm |
| > From: Lew Pitcher <lpitcher@sympatico.ca>
> An URL would be http://www.microsoft.com/
> or http://www.w3.org/Addressing/URL/url-spec.txt
> or http://www.w3.org/Addressing/rfc1738.txt
Ah, you have a good sense of humor. You casually give him three
examples of URLs, one an obvious correction of the FQDN he thought was
a URL, and the other two just thrown in as if they were just random
more-complicated sample URLs to illustrate the full syntax, letting the
OP realize for himself, if he has the brains to even look, that those
latter two are in fact pointers to the "fine manuals" (as in RTFM) he
needs to read to understand what a URL is. (One says it's a URL SPEC
right in the directoryPath and filename parts of the URL, and the other
says RFC which everyone should know is a spec for something and one
guess what it is given the context.)
|
|
|
|
|