Active Topics

 



Notices


Reply
Thread Tools
Posts: 1,680 | Thanked: 3,685 times | Joined on Jan 2011
#1
I know that you, like me have yearned for a totally awesome way to get your current IP address in a script that doesnt totally suck balls. Here I present my script to retrieve your current IP, if you have no IP (disconnected) it will display the word disconnected.

Why is this better than the IP script in DCEW?

this is better because it displays your INTERNAL IP irrespective of what interface is in use(wlan, gprs, usb)

It is a total mad bro style one liner.

It can be used as a DCEW, a QBW or a function in another script.



If you can think of a better 1-liner to get your current IP, then post it up, I dare you.



Code:
ifconfig $(route 2&>/dev/null | awk '{ field = $NF }; END{ print field }') >/dev/null && ifconfig $(route | awk '{ field = $NF }; END{ print field }') | egrep -o 'dr:[^ ]+'|sed 's/dr://g' || echo "127.0.0.1"
__________________
N900: One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die.
 

The Following 3 Users Say Thank You to vi_ For This Useful Post:
Posts: 114 | Thanked: 45 times | Joined on Oct 2009 @ Turin, Italy
#2
I didn't test it on the phone, but on my computer it works well

Code:
ifconfig ${iface} | sed -ne "s/.*inet addr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/p"
If you want to get the address of a specific interface, you should simply define the ${iface} variable.

NB: it works ONLY with IPv4 address.
 

The Following User Says Thank You to HoX For This Useful Post:
Posts: 1,680 | Thanked: 3,685 times | Joined on Jan 2011
#3
Originally Posted by HoX View Post
I didn't test it on the phone, but on my computer it works well

Code:
ifconfig ${iface} | sed -ne "s/.*inet addr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/p"
If you want to get the address of a specific interface, you should simply define the ${iface} variable.

NB: it works ONLY with IPv4 address.
IPv4, yeah forgot about that however you could just change the regex to include ipv6.

Your script doesnt work the same way. it returns the loopback address as well as the connected address. I tried this type of approach first however ONLY wanted to receive the current IP, in 1 line with NO loopback. This allows it to work in DCEW.
__________________
N900: One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die.
 
Posts: 114 | Thanked: 45 times | Joined on Oct 2009 @ Turin, Italy
#4
I'm sorry, but I don't know what "DCEW" means.
However, if you don't want the loopback device, you could "grep-it-out":
Code:
ifconfig ${iface} | sed -ne "s/.*inet addr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/p" | grep -v "127.0.0.1"
Otherwise, to get all the interfaces IP, but the loopback device:
Code:
ifconfig | sed -ne "s/^\([A-Za-z0-9]\+\).*/\1/p" | while read iface  ; do [ ${iface} != "lo" ] && ifconfig ${iface} ; done | sed -ne "s/.*inet addr:\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/p"
 
JonWW's Avatar
Posts: 623 | Thanked: 289 times | Joined on Jan 2010 @ UK
#5
Originally Posted by vi_ View Post
If you can think of a better 1-liner to get your current IP, then post it up, I dare you.
You dare us do you!

Ok I dare; five commands all piped:
Code:
ifconfig `ifconfig|grep -o wlan0`|grep -m1 't a'|sed 's/.*r://;s/ .*//'
and if you want your 'Disconnected' message, then bung this sixth command on the end::
Code:
|sed 's/127.0.0.1/Disconnected/'
and if you are not root (ie QBW):
Code:
echo "ifconfig `echo ifconfig|root|grep -o wlan0`|grep -m1 't a'|sed 's/.*r://;s/ .*//'"|root
Having waste four hours of my life in front of the tele last night trying to sort this, I dare you back to shorten it!

Oh vi_, yours throws a route usage error for me if run in XTerm! And fails if not connected.
Code:
Nokia-N900:~# ifconfig $(route 2&>/dev/null | awk '{ field = $NF }; END{ print field }') >/dev/null && ifconfig $(route | awk '{ field = $NF }; END{ print field }') | egrep -o 'dr:[^ ]+'|sed 's/dr://g' || echo "127.0.0.1"
BusyBox v1.10.2 (Debian 3:1.10.2.legal-1osso30+0m5) multi-call binary

Usage: route [{add|del|delete}]

ifconfig: Iface: error fetching interface information: Device not found
HoX DCEW = Desktop Command Execution Widget
 

The Following 3 Users Say Thank You to JonWW For This Useful Post:
Posts: 114 | Thanked: 45 times | Joined on Oct 2009 @ Turin, Italy
#6
Originally Posted by JonWW View Post
[/CODE]HoX DCEW = Desktop Command Execution Widget
Thank you and congratulation for the script.
 

The Following User Says Thank You to HoX For This Useful Post:
JonWW's Avatar
Posts: 623 | Thanked: 289 times | Joined on Jan 2010 @ UK
#7
Originally Posted by HoX View Post
Thank you and congratulation for the script.
I had to think 'outside the box' for this one! but it was fun.
 
Reply

Tags
hox rulez, one liner, whos your daddy


 
Forum Jump


All times are GMT. The time now is 01:54.