Concise IP information in OS X Terminal

1

Is there a command for the OS X Terminal that shows you only the most vital IP configuration information about your machine? I know there is "ifconfig" but that brings up a bunch of (in most cases) unnecessary information. I just want to know my current IP, subnet mask, default gateway and DNS.

UPDATE


Output of netstat -rn

Axels-MacBook-Air:~ axelkennedal$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
default            10.164.192.1       UGSc           75        0     en0
10.164.192/19      link#4             UCS             3        0     en0
10.164.192.1       c0:62:6b:e2:7a:c0  UHLWIir        76       20     en0   1150
10.164.206.216     127.0.0.1          UHS             0       25     lo0
10.164.223.255     ff:ff:ff:ff:ff:ff  UHLWbI          0       14     en0
127                127.0.0.1          UCS             0        0     lo0
127.0.0.1          127.0.0.1          UH              4      644     lo0
169.254            link#4             UCS             0        0     en0

Internet6:
Destination                             Gateway                         Flags         Netif Expire
::1                                     ::1                             UHL             lo0
fe80::%lo0/64                           fe80::1%lo0                     UcI             lo0
fe80::1%lo0                             link#1                          UHLI            lo0
fe80::%en0/64                           link#4                          UCI             en0
fe80::7ed1:c3ff:fef1:9b1f%en0           7c:d1:c3:f1:9b:1f               UHLI            lo0
ff01::%lo0/32                           ::1                             UmCI            lo0
ff01::%en0/32                           link#4                          UmCI            en0
ff02::%lo0/32                           ::1                             UmCI            lo0
ff02::%en0/32                           link#4                          UmCI            en0

Axel Kennedal

Posted 2014-03-04T15:58:21.733

Reputation: 333

Could you show us the output of ifconfig on your machine? The details are different on different OSs. Does ifconfig show your DNS and default gateway? – terdon – 2014-03-04T16:19:33.700

@terdon Sure! http://gyazo.com/ecd50ef22bc0393a87fff8136386fa4a The thing is though that as you can tell by the name "ifconfig" is "InterFaceconfig" meaning it only states addressing for interfaces on the machine running the terminal.

– Axel Kennedal – 2014-03-04T16:21:45.883

What machines would you want it to state addresses for? Of course it returns information for the machine it's running on. Anyway, thanks for the output but please just copy paste it into your question. I need to be able to copy it to my terminal to test and I can't use an screenshot. – terdon – 2014-03-04T16:30:05.190

The script in my answer has not been tested on OSX. Please let me know if you get any errors. – terdon – 2014-03-04T16:57:17.980

I only want info concerning my own machine, I just said that to clarify that it didnt show info about other devices (like the DNS server) @terdon – Axel Kennedal – 2014-03-04T17:16:22.707

Answers

0

As far as I know, there is no single command that will give you all the info you want. You will need to run a few different ones. The easiest approach is probably to create a little script that does this for you. I am writing this on Linux using the ipconfig command which is not available on Linux so this will likely have some errors, please let me know and I'll try and work them out.

#!/bin/bash

## Get the ip
ip=$(ipconfig getifaddr en0)

## Get the DNS server(s), this assumes Wi-Fi
dns=$(networksetup -getdnsservers Wi-Fi)

## Get the gateway
gateway=$(netstat -rn | awk 'NR==3{print $2}')

## And the netmask
mask=$(netstat -rn | awk 'NR==4{print $3}')

## Pretty print
cat<<EOF
IP      : $ip
Gateway : $gateway
Netmask : $mask
DNS     : $dns
EOF

Save that script as netinfo.sh or whatever in a directory that is in your $PATH (/usr/local/bin for example), make it executable (chmod a+x /usr/local/bin/netinfo.sh) and then run it:

$ netinfo.sh

terdon

Posted 2014-03-04T15:58:21.733

Reputation: 45 216

I tried it, but unfortunately I get an error: Axels-MacBook-Air:~ axelkennedal$ chmod a+x netinfo.sh Axels-MacBook-Air:~ axelkennedal$ netinfo.sh -bash: netinfo.sh: command not found I think the "chmod" part is where the error is..? I've never done something like this though @terdon – Axel Kennedal – 2014-03-04T17:17:09.460

@AxelKennedal-TechTutor OK first, the $ just indicates your prompt, you should not actually write it. Just save the script in the /usr/local/bin directory and then run chmod a+x /usr/local/bin/netinfo.sh.

– terdon – 2014-03-04T17:27:54.450

I know... And that's what I did @terdon – Axel Kennedal – 2014-03-04T17:31:19.653

@AxelKennedal-TechTutor not sure what is going wrong. Try running the script like this bash netinfo.sh in the directory where you saved the script. – terdon – 2014-03-04T20:52:41.897

That actually works! Nice job man! The only thing is that the gateway is not displayed at all and the netmask is displayed as "Flags" @terdon – Axel Kennedal – 2014-03-04T22:06:47.047

@AxelKennedal-TechTutor because I don't have a mac, I can't test this properly, please [edit] your question and add the output of netstat -rn so I can update this with the correct parsing. – terdon – 2014-03-04T22:08:00.537

0

Hope this helps. I don't think there's one command to show that info.

ifconfig en1 | grep inet && scutil --dns | grep nameserver && netstat -nr | grep default

levy

Posted 2014-03-04T15:58:21.733

Reputation: 146