linerboom.blogg.se

Bash script to get ip address
Bash script to get ip address







bash script to get ip address

So it's better to use the command ip in these situations.įor example: $ ip addr show wlan0 | grep -Po 'inet \K+' Solutions (using ip)Īs was also mentioned in the comments, ifconfig can be troublesome to use on systems that have multiple IP addresses assigned to a network device, given it only returns the first one. I have a text file named abd shown below. This solution makes use of grep's ability in newer versions to make use of PCRE (Perl regular expressions), along with it's -o switch to return just what matches the regular expression.

bash script to get ip address

demonstrated that there is an even more compact version using grep: $ ifconfig wlan0|grep -Po 't addr:\K+' There are countless other ways to do this, these are just a couple of examples to get you started. Vice versa this seems to be simple but I need it exactly the other way around, as I just have one IP address as input which is assigned to one of many interfaces of my system. In Linux, the standard tool for displaying and configuring network interfaces is ip. The Perl example is about as compact as it can get. You can determine your system private IP address by querying the network stack with commands such as ip, ifconfig or hostname. You could also do it using perl: $ ifconfig wlan0 | perl -nle'/t addr:(\S+)/&print$1' You could for example use this awk script to parse out the IP address of your wireless LAN NIC (wlan0): $ ifconfig wlan0 | grep "inet " | awk -F'+' '' The commands below explicitly ignore this, but could be adapted quite easily to grab this information instead. If your system is configured to support IPv6 you'll see both the "dotted quad" as well as the IPv6 IP addresses in the ifconfig output.įor example: inet6 addr: fe80::226:c7ff:fe85:a720/64 Scope:Link For example if it's a wired NIC card, then it's likely eth0.Īlso these examples are returning the IPv4 address. Indeed, there is often separate hardware for Internet access.

bash script to get ip address

Adjust this bit in the examples for your particular situation. To choose the best way of getting the external address of a machine via a Bash shell script, we should know the following: devices in the network. The examples below assume that the network interface is a wireless card named wlan0.









Bash script to get ip address