Results 1 to 3 of 3
I'm writting script to logon to pppd network.
After connecting I get new ip from provider
I want to assign ip_address to a variable
the closest I got is :
...
- 07-01-2008 #1Just Joined!
- Join Date
- Jan 2007
- Posts
- 4
isolating ip address
I'm writting script to logon to pppd network.
After connecting I get new ip from provider
I want to assign ip_address to a variable
the closest I got is :
/sbin/ifconfig pppd | grep "inet addr"
inet addr:192.168.10.188 Bcast:192.168.10.255 Mask:255.255.255.0
I need to trim it to show only the ip #
Is there a way of doing this in script?
- 07-01-2008 #2
Pipe your output through sed. And I'm sure someone will come along in this forum and give you an exact answer.
But you'll learn more in the long run if you google this:
Hope this helps.Code:sed tutorial
I was going to refer you to the man page, but that would be sheer torture for you. :)--
Bill
Old age and treachery will overcome youth and skill.
- 07-02-2008 #3
This doesn't use sed, but egrep...
I'm using "cut" to cut everything off after the first 26 bytes ( or characters ). That is because "inet addr:" is 10 bytes, and the IP address will need a maximum of 16 bytes. It's then sent to egrep which is using the "-o" option, which is the "only show match" option. Normally grep would show the whole line that the match was found on, but with this, it only shows the exact match according to the regular expression defined. It's all assigned to ip_addr using command substitution. However, with this version, you should also be able to use it in any other command substitution variable type without worrying about whitespace.Code:ip_addr=`/sbin/ifconfig pppd | cut -b 1-26 | egrep -o \([0-9]\{1,3}[-:\.]*\)\{4,}`
The regular expression is simple enough. It matches any digit from 0-9, at least once and no more than three times, followed by a hyphen, colon, decimal point ( note the escape sequence to keep it from matching the next character ) or any other character such as white space. This entire sequence is matched 4 times, or in other words, for every "xxx." pattern, where the "." is optional.
This regular expression was modified from is original intention to match and extract MAC address, however. It can be used in the same way with egrep and stdin, like so
The differences should be obvious. It needs to match hexadecimal, hyphens or colons, and 6 times instead of four.Code:egrep -oi \([0-9a-f]\{2,2}[-:]*\)\{6,}
I've actually preferred grep and egrep for stuff like this for a while because the "sed" man page file is quite intimdiating, kudos to wje_lf for mentioning the tutorial version.
P.S.
I tested this with
To make sure it would work with the formatting you provided. The "-i" isn't necessary of course because it's just setting it to being case insensitive ( it is necessary for MAC though ). If you for some reason get a different formatting, it'll serve you best to learn about sed as well.Code:echo "inet addr:192.168.010.188 Bcast:192.168.10.255 Mask:255.255.255.0" | cut -b 1-26 | egrep -io \([0-9]\{1,3}[-:\.]*\)\{4,}


Reply With Quote