[SOLVED] Detect if input is a valid ip & subnet
I am doing a script that requires the user to insert an ip , in case user writes text then it will abort the script .
I want 2 options to be available . 1st - user can enter a single ip (ex: 100.0.0.0) 2nd - user can enter a single ip & subnet (ex: 100.0.0.0/24) the first part i was able to figure out with this code : Quote:
[[ $ip =~ ^[0-255]+\.[0-255]+\.[0-255]+\.[0-254]+$ ]] the possibility of a subnet input between 0 & 32 (ex: 100.0.0.0/24)? I only need the if instruction so i can write an "elif" instruction if the first code . |
What exactly are you trying to check on the subnet? That a subnet exists?
I'm thinking of my router. The internet sees a single address in the 84.xxx.xxx.xxx range; I see an address 192.168.178.1 for the router, and pretty random addresses in the 192.168.178.xxx range for the various attached devices (this pc, printer, poser phone, tablets, etc.). 84.xxx.xxx.xxx passes trasffic to and from 192.168.178.xxx but never the twain shall meet. It's a tall order to ferret out the 192.168.178.xxx at all, let alone with a few lines of bash scripting. Have you played with nmap? Read the man page and have a go. It's probably a better way. |
Quote:
0.0.0.0/0 - 255.255.255.254/32 Quote:
|
Code:
if [[ $ip =~ ^[0-255]+\.[0-255]+\.[0-255]+\.[0-254]+$ ]] Code:
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]] Code:
if [[ $ip =~ /[0-9]{1,2}$ ]]; then |
Thanks Michaelk , it is working perfectly .
I will leave here the final code for others : Quote:
One idea was to put the first check of netmask to see if leters was inputed by user in that field . Quote:
In case if detects : $ip = 192.168.1.1/ewwer then $ip = 192.168.1.1 but if : $ip = 192.168.1.1/24 then it is ok and do not change it . |
Code:
if [[ "$ip" =~ (([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.([01]{,1}[0-9]{1,2}|2[0-4][0-9]|25[0-5])) ]]; then |
Quote:
192.168.0.1/345 = ok 192.168.0.1/tuy = ok I believe that expression is only to check the ip address . |
I would be monumentally disappointed - to the point of disbelief - if CPAN didn't have something to handle this trivially. Including the subnet check.
Why (re-)invent a square wheel ?. |
Yes, there are several modules available in CPAN. It's trivial to pound out a script to check addresses or networks. I'd hate to re-implement the whole module in the shell:
Code:
#!/usr/bin/perl -T Additionally, if there is something wrong with the address or network then an error describing the problem goes to stderr. |
My whole script is written in bash , to adapt it to perl would take me a course of perl , which i don't want to get in now .
This issue is "easy" if we understand the logic that must take . Many of the impossible bash code that we think that is not possible , is because we are unable to figure out a logic way to do it . the target is an ip and subnet (192.168.1.1/24) logic to be acquired in code : 0 - check if there is a slash in the var (/) , case "/" does not exist then go to step 5 1 - split the var in / and check if it is a number or letters in the subnet . 2 - case it is letters then it is invalid 2a- set the current var with user input until the "/" ex user input : 100.0.0.0/sfg 2a if statement = change original var from 100.0.0.0/sfg to 100.0.0.0 3 - case it is numbers then it must between 0 and 32 4 - check the var until the "/" 5 - it is an ip ? then it must be between 0.0.0.0 and 255.255.255.255 6 - if it not an ip then the value is not accepted . The big problem is to make all these steps with if statements and regex . My experience with "if statements" is very limited , this is why i post it here , because many people here programming is their everyday job . |
I'd be pretty comfortable stating all the current respondents would be more than adept at bash. And can recognise it limitations.
There is no need to re-write all your script, just the IP testing bit. Turbocapitalist has given you a more than adequate solution that can be simply called and the return code tested, just like any other command you would use. You would likely have to install Net::IP from CPAN - search the web for instructions. Might even be in your repos, it is for Fedora. |
eventually i will figure out a way to do it , it would take 50 lines or more of code , but i will make it .
Thanks all for your help . |
Like i told in the previous post , i would figure out a way to do it in bash , and i did it .
However the regex michael give allows to input an ip like 999.999.999.999 , and that cannot happen . But the rest of the code is working as it should be . Quote:
|
For IPv4 addresses, you'd just have to look at each number and then see if it is in the range 0-254. It might be a good idea use a function for the actual comparison to avoid coding the same sequence four times.
For temp files, consider using mktemp Code:
tmp=$(mktemp pedropt.ip.XXXXXXX); |
yup . i know , i have to split the ip in 4 parts on the dots and check in their values are between 0 and 255 .
But that i will do it other time . |
All times are GMT -5. The time now is 02:50 AM. |