![]() |
|
IPfinder Bash Script. - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: IPfinder Bash Script. (/Thread-IPfinder-Bash-Script) Pages:
1
2
|
IPfinder Bash Script. - L0aD1nG - 08-26-2014 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
echoRunning this script will conclude on this results normally: Quote:Your Local Ip Is : <local 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! RE: IPfinder Bash Script. - Singularity_mybb_import13102 - 08-26-2014 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 ' ' -f1May 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
RE: IPfinder Bash Script. - L0aD1nG - 08-26-2014 (08-26-2014, 03:38 PM)Singularity Wrote: Simple, and fun I guess. I think it should work with eth0 too. I will check it out and if not update it. Thanks for the feedback! RE: IPfinder Bash Script. - Singularity_mybb_import13102 - 08-26-2014 (08-26-2014, 03:46 PM)L0aD1nG Wrote: I think it should work with eth0 too. 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 foundI 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. RE: IPfinder Bash Script. - L0aD1nG - 08-26-2014 (08-26-2014, 03:52 PM)Singularity Wrote:(08-26-2014, 03:46 PM)L0aD1nG Wrote: I think it should work with eth0 too. Tested too bro. Updating the thread for eth0 now, thanks a lot for noticing that. RE: IPfinder Bash Script. - Singularity_mybb_import13102 - 08-26-2014 (08-26-2014, 03:53 PM)L0aD1nG Wrote: Tested too bro. No problem, glad to help. Thanks for sharing this script with others. RE: IPfinder Bash Script. - L0aD1nG - 08-26-2014 Thread Updated This should work now on both eth0 and wlan0 now. If it doesn't let me know. RE: IPfinder Bash Script. - Singularity_mybb_import13102 - 08-26-2014 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. RE: IPfinder Bash Script. - The Real Slim Shady - 08-26-2014 (08-26-2014, 04:17 PM)L0aD1nG Wrote: Thread Updated 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) RE: IPfinder Bash Script. - Singularity_mybb_import13102 - 08-26-2014 This code should do the job efficiently: Code: echo -ne 'Local IP: '; ifconfig | grep -m1 'inet' | cut -d ':' -f 2 | cut -d ' ' -f 1Result: 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 |