Login Register






IPfinder Bash Script. filter_list
Author
Message
IPfinder Bash Script. #1
Hello HackCommunity, I have a little bash script here that I am using long time now. I thought is worth sharing this. Its a script that will find and nicely print out your local && global ip address.Again this is mainly for terminal users. Ofc you can find them manually on terminal && on browser, but this for sure save you time and I guess you will find it more handy too.

Requirements : Linux OS

Here is the code:
Code:
#!/bin/bash ## Setting up the variables for check up eth_info=`ifconfig eth0 | grep "inet addr" | awk -F: ' { print $2 } ' | awk ' { print $1 } '` wlan_info=`ifconfig wlan0 | grep "inet addr" | awk -F: ' { print $2 } ' | awk ' { print $1 } '` ## Prints your local ip address echo -e "\nYour Local Ip Is : \c" if [ -z $eth_info ]; then echo $wlan_info else echo $eth_info fi echo ## Prints your local Ip address echo -e "Your Global Ip Is : \c" wget http://ipecho.net/plain -O - -q ; echo echo

Running this script will conclude on this results normally:
Quote:Your Local Ip Is : <local ip address>

Your Global Ip Is : <global ip address>


I wish this script will help Linux Buddies in any way!
Thanks a lot for reading this.
Sincerely,
L0aD1nG


EDIT : Thanks to @Singularity for his important note!
(This post was last modified: 08-26-2014, 04:32 PM by krozz.)
MASTERING OTHERS IS STRENGTH, MASTERING YOURSELF IS TRUE POWER.

[Image: qJweLN6.jpg]

Reply

RE: IPfinder Bash Script. #2
Simple, and fun I guess.
The first query for the local IP is flawed, not everyone is running on wifi and on the wlan0 interface. Some people may be directly connected and on an interface like eth0.
I would suggest using something such as:
Code:
ifconfig | grep -m1 'inet' | cut -d ':' -f2 | cut -d ' ' -f1
(You may be able to just drop the wlan0 from your code above)

May I also suggest you change the script up and use dig to query a DNS Server for gathering the external IP address?
It is more efficient Biggrin
(This post was last modified: 08-26-2014, 03:43 PM by MaltShizzle.)
[Image: obDhRdK.gif]

Reply

RE: IPfinder Bash Script. #3
(08-26-2014, 03:38 PM)Singularity Wrote: Simple, and fun I guess.
The first query for the local IP is flawed, not everyone is running on wifi and on the wlan0 interface. Some people may be directly connected and on an interface like eth0.
I would suggest using something such as:
Code:
ifconfig | grep -m1 'inet' | cut -d ':' -f2 | cut -d ' ' -f1
(You may be able to just drop the wlan0 from your code above)

May I also suggest you change the script up and use dig to query a DNS Server for gathering the external IP address?
It is more efficient Biggrin

I think it should work with eth0 too.
I will check it out and if not update it.
Thanks for the feedback!
MASTERING OTHERS IS STRENGTH, MASTERING YOURSELF IS TRUE POWER.

[Image: qJweLN6.jpg]

Reply

RE: IPfinder Bash Script. #4
(08-26-2014, 03:46 PM)L0aD1nG Wrote: I think it should work with eth0 too.
I will check it out and if not update it.
Thanks for the feedback!

I tested it on one my servers that is on an eth0 interface.
Since there is no wlan0 interface, it will spring the following error message as expected:

Code:
ninjex@playground:~# ifconfig wlan0 wlan0: error fetching interface information: Device not found

I also think it's sort of redundant to echo the loop back address (lo interface) but that's up for debate.
I still like the idea, and mainly hope others can learn from the code given.
(This post was last modified: 08-26-2014, 03:55 PM by MaltShizzle.)
[Image: obDhRdK.gif]

Reply

RE: IPfinder Bash Script. #5
(08-26-2014, 03:52 PM)Singularity Wrote:
(08-26-2014, 03:46 PM)L0aD1nG Wrote: I think it should work with eth0 too.
I will check it out and if not update it.
Thanks for the feedback!

I tested it one my server that is on a eth0 interface.
Since there is no wlan0 interface, it will spring the following error message:

Code:
ninjex@playground:~# ifconfig wlan0 wlan0: error fetching interface information: Device not found

Tested too bro.
Updating the thread for eth0 now, thanks a lot for noticing that.
MASTERING OTHERS IS STRENGTH, MASTERING YOURSELF IS TRUE POWER.

[Image: qJweLN6.jpg]

Reply

RE: IPfinder Bash Script. #6
(08-26-2014, 03:53 PM)L0aD1nG Wrote: Tested too bro.
Updating the thread for eth0 now, thanks a lot for noticing that.

No problem, glad to help. Thanks for sharing this script with others.
[Image: obDhRdK.gif]

Reply

RE: IPfinder Bash Script. #7
Thread Updated
This should work now on both eth0 and wlan0 now.
If it doesn't let me know.
MASTERING OTHERS IS STRENGTH, MASTERING YOURSELF IS TRUE POWER.

[Image: qJweLN6.jpg]

Reply

RE: IPfinder Bash Script. #8
You know there are more interfaces than just wlan0 / eth0, (for example, ural0 on an OpenBSD os) and these interface names could have been changed / tweaked? That's why I suggested the code I did the first time, as it does not matter what the name of the interface is, it should work on any interface, and regardless of the interface name being used.
(This post was last modified: 08-26-2014, 04:25 PM by MaltShizzle.)
[Image: obDhRdK.gif]

Reply

RE: IPfinder Bash Script. #9
(08-26-2014, 04:17 PM)L0aD1nG Wrote: Thread Updated
This should work now on both eth0 and wlan0 now.
If it doesn't let me know.

Only shows eth0 if you have eth0 and wlan0

also, if you have more than 1 ethernet or wireless adapter, you may not detect the correct one. (eg eth1/wlan1)

Reply

RE: IPfinder Bash Script. #10
This code should do the job efficiently:
Code:
echo -ne 'Local IP: '; ifconfig | grep -m1 'inet' | cut -d ':' -f 2 | cut -d ' ' -f 1

Result:
Code:
ninjex@playground:~# echo -ne 'Local IP: '; ifconfig | grep -m1 'inet' | cut -d ':' -f 2 | cut -d ' ' -f 1 Local IP: 94.23.62.140
(This post was last modified: 08-26-2014, 04:30 PM by MaltShizzle.)
[Image: obDhRdK.gif]

Reply