Sunday, April 28, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2480  / 3 Years ago, tue, june 1, 2021, 2:16:51

I'm writing a shell script which needs information on the state of the network connection - the basic stuff like ip address, subnet, netmask and so on.



I could just use ifconfig and sed/grep for the information, but I was hoping there was a cleaner way to do this - possibly using the ip command, or similar utilities.



I'm looking for something which could give the following results:



$ (magic command) ip
192.168.1.1
$ (magic command) netmask
255.255.255.0
$ (magic command) subnet
192.168.0.0


and possibly similar things for network gateway/dns servers.



Does such a command exist? Perhaps there's an --unformatted flag for ip or ifconfig? Am I too hopeful/naive?



Thanks :)


More From » command-line

 Answers
6

You can also use ifdata to get information without parsing output of ip with grep, awk or other tools:



[~]$ ifdata -pa eth0
192.168.246.161
[~]$ ifdata -pn eth0
255.255.240.0
[~]$ ifdata
Usage: ifdata [options] iface
-e Reports interface existence via return code
-p Print out the whole config of iface
-pe Print out yes or no according to existence
-pa Print out the address
-pn Print netmask
-pN Print network address
-pb Print broadcast
-pm Print mtu
-ph Print out the hardware address
-pf Print flags
-si Print all statistics on input
-sip Print # of in packets
-sib Print # of in bytes
-sie Print # of in errors
-sid Print # of in drops
-sif Print # of in fifo overruns
-sic Print # of in compress
-sim Print # of in multicast
-so Print all statistics on output
-sop Print # of out packets
-sob Print # of out bytes
-soe Print # of out errors
-sod Print # of out drops
-sof Print # of out fifo overruns
-sox Print # of out collisions
-soc Print # of out carrier loss
-som Print # of out multicast
-bips Print # of incoming bytes per second
-bops Print # of outgoing bytes per second

[#29653] Wednesday, June 2, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anatta

Total Points: 326
Total Questions: 128
Total Answers: 96

Location: Jordan
Member since Sun, Jun 26, 2022
2 Years ago
;