Re: file with IP?

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Nick Rout
On Thu, 03 Nov 2005 09:15:20 +0000
Neil Bothwick <[hidden email]> wrote:

> On Thu, 03 Nov 2005 07:56:08 +0800, William Kenworthy wrote:
>
> > doesnt work well.  In my case only gives one lan IP, misses the other
> > Lan and the dhcp ADSL IP.  Not a lot of use :(
>
> No, but on a box with a single IP address, it makes life a lot easier.
>

on my box (1 ethernet and loopback up and one wireless down) it gives
127.0.0.1

Seems a bit hit and miss.
 
A machine with one IP address usually has no outside network
connection, only 127.0.0.1
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Neil Bothwick
On Fri, 4 Nov 2005 21:22:40 +1300, Nick Rout wrote:

> > No, but on a box with a single IP address, it makes life a lot easier.

> on my box (1 ethernet and loopback up and one wireless down) it gives
> 127.0.0.1
>
> Seems a bit hit and miss.

I've just tried it on four machines, one of them returned 127.0.0.1. it
turns out that one had

127.0.0.1 localhost trillian

in /etc/hosts. removing trillian, the box's name, made hostname -i give
the right answer.
 
> A machine with one IP address usually has no outside network
> connection, only 127.0.0.1

I meant one non-local IP address, surely the most common
configuration, this discussion would be pointless for a non-networked
machine.


--
Neil Bothwick

Only an idiot actually READS taglines.

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

bn-5
Given this hell, I can't see why my original suggestion of parsing
ifconfig has not been taken into account... a couple of lines of
python/perl/whatever should do the trick.

m.

--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Neil Bothwick
On Fri, 4 Nov 2005 16:12:07 +0100, brullo nulla wrote:

> Given this hell, I can't see why my original suggestion of parsing
> ifconfig has not been taken into account... a couple of lines of
> python/perl/whatever should do the trick.

Given the apparent vagaries of hostname -i, I'm inclined to agree,
although sed would suffice, to avoid anyone having to subject themselves
to perl :)


--
Neil Bothwick

Indecision is the key to flexibility.

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Jorge Almeida
In reply to this post by bn-5
On Fri, 4 Nov 2005, brullo nulla wrote:

> Given this hell, I can't see why my original suggestion of parsing
> ifconfig has not been taken into account... a couple of lines of
>
It has, at least by the OP (that would be me).

/sbin/ifconfig|grep -m 1 inet|sed -e 's/^\s*inet addr://'|sed -e 's/\s.*$//'
--
Jorge Almeida
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

bn-5
> > Given this hell, I can't see why my original suggestion of parsing
> > ifconfig has not been taken into account... a couple of lines of
> >
> It has, at least by the OP (that would be me).
>
> /sbin/ifconfig|grep -m 1 inet|sed -e 's/^\s*inet addr://'|sed -e 's/\s.*$//'

Wow. You evil geniuses of regular expressions! °_°

I'm not that good with sed, awk and the like... I'd have used a couple
lines of python, probably. You know, I belong to the new, lazy,
spoiled generations of desktop Linux users, that no more learn all the
old Unix magic formulas... :)

m.

--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Billy Holmes
brullo nulla wrote:
> Wow. You evil geniuses of regular expressions! °_°

can even shorten it with just one sed expression:

/sbin/ifconfig eth0 | sed -n 's/^.*inet addr:\([^ ]*\) .*$/\1/p'


--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Alexander Skwar-11
Billy Holmes schrieb:
> brullo nulla wrote:
>> Wow. You evil geniuses of regular expressions! °_°
>
> can even shorten it with just one sed expression:
>
> /sbin/ifconfig eth0 | sed -n 's/^.*inet addr:\([^ ]*\) .*$/\1/p'

Doesn't work, as it makes wrong assumptions. It assumes, that
there's string "inet addr:". For example in LANG=de_DE, that's
wrong:

alexander@blatt ~ $ /sbin/ifconfig ra0
ra0       Protokoll:Ethernet  Hardware Adresse 00:14:A5:00:AE:D2
          inet Adresse:192.168.1.11  Bcast:192.168.1.31  Maske:255.255.255.224
          UP BROADCAST NOTRAILERS RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12234 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6965 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:11 Sendewarteschlangenlänge:1000
          RX bytes:18131234 (17.2 Mb)  TX bytes:665533 (649.9 Kb)
          Interrupt:5

To fix that, the locale should be shanged, eg. like this:

$ LC_ALL=C /sbin/ifconfig ra0

Let's make a different assumption, which is independent of
the locale - IP is after the 1st : in the 2nd line. One
solution:

alexander@blatt ~ $ /sbin/ifconfig ra0 | head -n 2 | tail -n 1 | sed 's|.*:\(.*\) .*:.*:.*|\1|'
192.168.1.11

Doing away with "tail -n 1", as sed can do that:

alexander@blatt ~ $ /sbin/ifconfig ra0 | head -n 2 | sed -e '$!d' -e 's|.*:\(.*\) .*:.*:.*|\1|'
192.168.1.11

"head" is also useless:

alexander@blatt ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' -e 's|.*:\(.*\) .*:.*:.*|\1|'
192.168.1.11

But I actually don't quite like the regexp - it's too long... With
perl, I'd use a "non-gready" .*, like so:

alexander@blatt ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' | perl -pe 's|.*?:(.*?) .*|$1|'
192.168.1.11

How to do it more ellegant?

Alexander Skwar
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Billy Holmes
Alexander Skwar wrote:
> But I actually don't quite like the regexp - it's too long... With
> perl, I'd use a "non-gready" .*, like so:

then don't use a regex at all:

/sbin/ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 1

--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Fredrik-3
ifconfig eth0 | awk 'NR==2 {print $2}' | sed -e s/addr://

will also work.(you may need to modify it so it fit your own cards)

//Fredrik


Billy Holmes wrote:

> Alexander Skwar wrote:
>
>> But I actually don't quite like the regexp - it's too long... With
>> perl, I'd use a "non-gready" .*, like so:
>
>
> then don't use a regex at all:
>
> /sbin/ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 1
>

--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Arturo 'Buanzo' Busleiman
In reply to this post by Alexander Skwar-11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

What about

ip addr show dev eth0 | awk '{print $2}' | grep -v \: | head -n 1 | cut -d'/' -f1


- --
Arturo "Buanzo" Busleiman - www.buanzo.com.ar
Consultor en Seguridad Informatica
KTP Consultores - info AT ktpconsultores.com.ar

Romper un sistema de seguridad los acerca tanto a ser hackers como el
encender autos puenteando los convierte en ingenieros automotrices.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDa6YFAlpOsGhXcE0RAsLgAJ9voJclEKGEAYrSUYT+fWnLVl6gVwCfa3hk
HpwUqV4lmjC/lt35HJFpSgU=
=StWO
-----END PGP SIGNATURE-----
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Alexander Skwar-11
Arturo 'Buanzo' Busleiman schrieb:

> What about
>
> ip addr show dev eth0 | awk '{print $2}' | grep -v \: | head -n 1 | cut -d'/' -f1

IMO, too many different commands to be elegant...

Further:

alexander@blatt ~ $ ip addr show dev ra0
bash: ip: command not found

But if awk is okay, I'd do:

alexander@blatt ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' | awk -F: '{print $2}' | sed 's| .*||'
192.168.1.11

Or if we make the assumption, that the network mask will
always start with "255.", we could do:

alexander@blatt ~ $ /sbin/ifconfig ra0 | awk -F: '/:255\./{print $2}' | sed 's| .*||'
192.168.1.11

Any awk "gurus" around? If so, how to do away with that sed command?

Alexander Skwar
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Alexander Skwar-11
In reply to this post by Billy Holmes
Billy Holmes schrieb:
> Alexander Skwar wrote:
>> But I actually don't quite like the regexp - it's too long... With
>> perl, I'd use a "non-gready" .*, like so:
>
> then don't use a regex at all:
>
> /sbin/ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d ' ' -f 1
>

Don't like that - there might be locales, where there's no "inet"
in the line. IMO better:

/sbin/ifconfig eth0 | grep :255\. | cut -d : -f 2 | cut -d ' ' -f 1

How about that:

alexander@blatt ~ $ echo `/sbin/ifconfig ra0` | cut -d : -f 8 | cut -d ' ' -f 1
192.168.1.11

Alexander Skwar
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Arturo 'Buanzo' Busleiman
In reply to this post by Alexander Skwar-11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alexander Skwar wrote:
> Arturo 'Buanzo' Busleiman schrieb:
>
>
>>What about
>>
>>ip addr show dev eth0 | awk '{print $2}' | grep -v \: | head -n 1 | cut -d'/' -f1
>
>
> IMO, too many different commands to be elegant...

But simple. The idea of writing scripts is that any non-guru will understand it easily.

For a **very** simple unix-scripter

awk means: print fields
grep: print [non] matching
head: from start....
tail: from end....
cut: name says it all

- --
Arturo "Buanzo" Busleiman - www.buanzo.com.ar
Consultor en Seguridad Informatica
KTP Consultores - info AT ktpconsultores.com.ar

Romper un sistema de seguridad los acerca tanto a ser hackers como el
encender autos puenteando los convierte en ingenieros automotrices.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDa6ryAlpOsGhXcE0RApZNAJ45EF9Wpx7zd6OFdnVJDvprvJUCdACffH3e
dNJFp0K6Ya1gVY3qHOfEOGU=
=DrbN
-----END PGP SIGNATURE-----
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Billy Holmes
In reply to this post by Alexander Skwar-11
Alexander Skwar wrote:
> Don't like that - there might be locales, where there's no "inet"
> in the line. IMO better:
> /sbin/ifconfig eth0 | grep :255\. | cut -d : -f 2 | cut -d ' ' -f 1

broadcasts don't always begin or end with 255, odd yes, but if we're
spliting hairs then anything is possible.

The only option is to assume that the address line appears on the 2nd
line, and it is delimited by a colon, and then strip out the fields past
the trailing spaces.

/sbin/ifconfig eth0 | head -n 2 | tail -n 1 | cut -d : -f 2 | cut -d ' '
-f 1

and.. of course, this only works in linux... and for IPv4 Addresses.
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Arturo 'Buanzo' Busleiman
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Billy Holmes wrote:
> The only option is to assume that the address line appears on the 2nd
> line, and it is delimited by a colon, and then strip out the fields past
> the trailing spaces.

I'm writing a small helper proggie to get the main ip_address:mask for the given interface...

I'll post the url here later.

- --
Arturo "Buanzo" Busleiman - www.buanzo.com.ar
Consultor en Seguridad Informatica
KTP Consultores - info AT ktpconsultores.com.ar

Romper un sistema de seguridad los acerca tanto a ser hackers como el
encender autos puenteando los convierte en ingenieros automotrices.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDa75wAlpOsGhXcE0RAoPiAJoCJsZu+O9W/+JTQkOTvcQbrFiNGwCfYmGH
11mZ2LtdUdqZJZyGbLFlRPA=
=HGMm
-----END PGP SIGNATURE-----
--
[hidden email] mailing list

Reply | Threaded
Open this post in threaded view
|

Re: file with IP?

Alexander Skwar-11
In reply to this post by Billy Holmes
Billy Holmes schrieb:
> Alexander Skwar wrote:
>> Don't like that - there might be locales, where there's no "inet"
>> in the line. IMO better:
>> /sbin/ifconfig eth0 | grep :255\. | cut -d : -f 2 | cut -d ' ' -f 1
>
> broadcasts don't always begin or end with 255, odd yes, but if we're
> spliting hairs then anything is possible.

Well, I wasn't thinking about the *broadcast* adress (which
in my address indeed has no 255 in it, as it is 192.168.1.31).
I was referring to the network mask. Sure, even that doesn't
*HAVE* to start with 255 - but I don't know about any network
that "spans" multiple "class A" networks.

But you're right, there's no requirement that the network
mask MUST start with 255. It just happens to always and
ever be that way - not so with the "inet addr" text. That
happens to often be wrong (locale de_DE, fr_FR and certainly
many others).

> The only option is to assume that the address line appears on the 2nd
> line, and it is delimited by a colon, and then strip out the fields past
> the trailing spaces.

Yep. Or that it is the 1st quad-dotted adress, or the 1st
sequence of "numbers, maybe followed by dot" after the
first colon, which would be a regexp approach.

> /sbin/ifconfig eth0 | head -n 2 | tail -n 1 | cut -d : -f 2 | cut -d ' '
> -f 1

Nice.

> and.. of course, this only works in linux... and for IPv4 Addresses.

Yep.

--
Alexander Skwar
--
[hidden email] mailing list