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
  • 375
  • 2
  • 4
  • 15
  • 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 Mar 04 '14 at 16:19
  • @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 Mar 04 '14 at 16:21
  • 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 Mar 04 '14 at 16:30
  • The script in my answer has not been tested on OSX. Please let me know if you get any errors. – terdon Mar 04 '14 at 16:57
  • 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 Mar 04 '14 at 17:16

2 Answers2

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
  • 52,568
  • 14
  • 124
  • 170
  • 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 Mar 04 '14 at 17:17
  • @AxelKennedal-TechTutor OK first, the `$` just indicates your [prompt](http://superuser.com/q/57575/151431), 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 Mar 04 '14 at 17:27
  • I know... And that's what I did @terdon – Axel Kennedal Mar 04 '14 at 17:31
  • @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 Mar 04 '14 at 20:52
  • 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 Mar 04 '14 at 22:06
  • @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 Mar 04 '14 at 22:08
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
  • 166
  • 4