Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged IP from sorted by
Terminal - Commands tagged IP - 73 results
curl ipinfo.io
2013-10-31 05:16:47
User: coderholic
0

Get your ip address, hostname, ASN and geolocation information. If you want just one field as a text response you can also get that,eg curl ipinfo.io/ip

cut -d ' ' -f 1 /var/log/apache2/access_logs | uniq -c | sort -n
2013-09-17 20:05:03
User: BorneBjoern
Functions: cut sort uniq
0

avoiding UUOC!

cut can handle files as well. No neet for a cat.

netstat -ntu | awk ' $5 ~ /^(::ffff:|[0-9|])/ { gsub("::ffff:","",$5); print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
2013-09-10 19:28:06
User: mrwulf
Functions: awk cut netstat sort uniq
1

Same as the rest, but handle IPv6 short IPs. Also, sort in the order that you're probably looking for.

cat /var/log/apache2/access_logs | cut -d' ' -f1 | sort | uniq -c | sort -n
2013-09-07 23:57:31
User: while0pass
Functions: cat cut sort uniq
1

The first sort is necessary for ips in a list to be actually unique.

tcpdump host <IP> -nXXv -s0 -w file.pcap
cat /var/log/apache2/access_logs | cut -d ' ' -f 1 | uniq -c | sort -n
2013-09-02 13:04:47
User: basvdburg
Functions: cat cut sort uniq
1

Show's per IP of how many requests they did to the Apache webserver

ifdata -pN eth0
ifconfig -a | awk '/Bcast/{print $2}' | cut -c 5-19
ifconfig -a | awk '/Bcast/{print $2}' | cut -c 5-19
ip -o -4 addr show | awk -F '[ /]+' '/global/ {print $4}'
2013-04-09 16:34:39
User: atoponce
Functions: awk
1

Why use many different utilities all piped together, when you only need two?

ip route get 8.8.8.8 2>/dev/null|grep -Eo 'src [0-9.]+'|grep -Eo '[0-9.]+'
2012-10-14 02:06:13
User: intangible
Functions: get grep route
2

Instead of hard-coding in a check to scrape info from ifconfig based on a specific interface, do it in a more portable way.

This works really well if you switch between wired, wireless, bluetooth or even VPN connections. You can get your current IP in a script (since it'll be something like tun0 instead of eth0 or wlan1).

This uses a well known public ip address 8.8.8.8, but it doesn't actually connect to it, it just shows you the route it would take.

telnet v4address.com
lsof -p `pidof pidgin` | awk '{ print $9 }'|egrep `hostname` | grep -o ">[^:]\+:" | tr -d ":>" | while read line; do host $line; done;
hostname -I
2012-07-18 19:43:48
User: bashfan
Functions: hostname
Tags: awk IP ip address
0

That's the easiest way to do it. -I (or capital i) display all network addresses of a host

ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p'
ip -f inet a | awk '/inet / { print $2 }'
2012-07-18 15:13:10
User: BorneBjoern
Functions: awk
Tags: awk IP ip address
2

gives u each configured IP in a seperate line.

ip a s eth0 | awk -F"[/ ]+" '/inet / {print $3}'
ip a s eth0 | awk '/inet / {print $2} | sed -e 's/\/..//g'
ip a s eth0 | grep "inet " | head -n 1 | awk '{print $2}' | cut -f1 -d'/'
2012-07-16 07:54:43
User: thelan
Functions: awk cut grep head
Tags: IP
-4

Get the first IPv4 address of an interface

netstat -tn | grep :80 | awk '{print $5}'| grep -v ':80' | cut -f1 -d: |cut -f1,2,3 -d. | sort | uniq -c| sort -n
2012-06-26 08:29:37
User: krishnan
Functions: awk cut grep netstat sort uniq
0

cut -f1,2 - IP range 16

cut -f1,2,3 - IP range 24

cut -f1,2,3,4 - IP range 24

lynx -dump http://www.ip2location.com/ | sed -n '/^ *Field Name *Value *$/,/^ *\[_\] *Mobile .*Carrier.*name/p'
lynx -dump ip.nu
curl -s -d "CSField=Name" -d "CSInput=BostonUCompSci" http://torstatus.blutmagie.de/index.php | grep -oP "ip=\K(\d+)(\.\d+){3}"
2012-03-09 16:52:27
User: JisSey
Functions: grep
0

Reciprocally, we could get the node name from a give Tor IP address =>

ip2node() { curl -s -d "QueryIP=$1" http://torstatus.blutmagie.de/tor_exit_query.php | grep -oP "Server name:.*'>\K\w+" ; }

ip2node 204.8.156.142

BostonUCompSci

ping1 IPaddr_or_hostname
2012-02-09 17:26:32
User: waibati
Tags: echo IP ping
0

export THISOS="`uname -s`"

if [ "$THISOS" = "SunOS" ]

then

export THISRELEASE="`uname -r`"

ping1() { ping -s $1 56 1 | egrep "^64"; }

elif [ "$THISOS" = "AIX" ]

then

export THISRELEASE="`uname -v`.`uname -r`"

ping1() { ping -w ${2:-1} $1 56 1 | egrep "^64"; }

elif [ "$THISOS" = "Linux" ]

then

export THISRELEASE="`uname -r`"

ping1() { ping -c 1 -w ${2:-1} $1 | egrep "^64"; }

fi

echo -n "IP Address or Machine Name: "; read IP; ping -c 1 -q $IP >/dev/null 2>&1 && echo -e "\e[00;32mOnline\e[00m" || echo -e "\e[00;31mOffline\e[00m"
2012-02-09 07:00:03
User: crlf
Functions: echo ping read
Tags: bash echo IP ping
1

I have used single packet, and in a silent mode with no display of ping stats. This is with color and UI improvement to the http://www.commandlinefu.com/commands/view/10220/check-if-a-machine-is-online. It is as per the enhancements suggested.