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 address from sorted by
Terminal - Commands tagged ip address - 103 results
finger @www.linuxbanks.cn | grep -oE '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | head -n1
2010-05-05 14:58:55
User: zhangweiwu
Functions: finger grep head
Tags: ip address
-4

This is useful when you got a reserved IP address like 192.168.0.100 and want to find out what IP address is used to access the Internet. You have to know a server with 'efingerd -n' configured, like www.linuxbanks.cn as above.

Other method to find out this information are for example access www.tell-my-ip.com and grep the output. The finger method have the advantage that it is easy to deploy a service like www.tell-my-ip.com, as you only need to get efingerd installed.

curl ifconfig.me/all/json
2010-04-21 20:47:17
User: truemilk
1

Request all information about my IP address in json format

curl ifconfig.me/all/xml
2010-04-21 20:45:17
User: truemilk
-1

Request all information about my IP address in xml format

curl ifconfig.me
2010-04-21 13:10:33
User: aajjk
194

curl ifconfig.me/ip -> IP Adress

curl ifconfig.me/host -> Remote Host

curl ifconfig.me/ua ->User Agent

curl ifconfig.me/port -> Port

thonks to http://ifconfig.me/

/bin/grep - ipranges.txt | while read line; do ipcalc $line ; done | grep -v deag
2010-04-20 21:13:00
User: tf8
Functions: grep read
7

Taking file with ip ranges, each on it's own line like:

cat ipranges.txt

213.87.86.160-213.87.86.193

213.87.87.0-213.87.88.255

91.135.210.0-91.135.210.255

command returns deaggregated ip ranges using ipcalc deaggregate feature like that:

213.87.86.160/27

213.87.86.192/31

213.87.87.0/24

213.87.88.0/24

91.135.210.0/24

Useful for configuring nginx geo module

curl icanhazip.com
nmap -sP 192.168.1.0/24; arp -n | grep "192.168.1.[0-9]* *ether"
2010-04-12 14:36:15
User: gavinmc
Functions: arp grep
14

You send a unicast ICMP packet to each host. Many firewalls will drop that ICMP. However, in order to send the ICMP, you'll have first done an ARP request and the remote machine is unlikely to ignore that, so the computer will be in your ARP table.

curl -s ip.appspot.com
2010-04-04 01:22:59
User: tuxilicious
8

should be very consistent cause it's google :-)

curl -s icanhazip.com
dig +short myip.opendns.com @resolver1.opendns.com
2010-04-02 21:53:26
User: dooblem
Functions: dig
27

Instead of opening your browser, googling "whatismyip"...

Also useful for scripts.

dig can be found in the dnsutils package.

ip addr | awk '/inet / {sub(/\/.*/, "", $2); print $2}'
2010-03-04 14:21:18
User: markdrago
Functions: awk
0

ifconfig is dead, long-live iproute2

this also uses only awk to do the grepping and removal of CIDR notation

tail -f /var/log/apache2/access.log | awk -W interactive '!x[$1]++ {print $1}'
2010-01-12 15:23:03
User: pykler
Functions: awk tail
1

Prints the unique IP Addresses as they arrive from an Apache `access.log` file.

The '-W interactive' tells awk to start writing to stdout immediately and not buffer the output.

This command builds on the uniq lines without sorting command (http://www.commandlinefu.com/commands/view/4389/remove-duplicate-entries-in-a-file-without-sorting.)

ip addr add 192.168.10.1/24 dev eth0
2010-01-12 13:07:36
User: wincus
2

Allows to add more than one ip address to one network device.

python -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.connect(('google.com', 80)); print s.getsockname()[0]"
2009-12-29 19:17:03
User: whiteinge
Functions: python
-11

Requires Python; doesn't require a specific server to return your IP.

x=IO::Interface::Simple; perl -e 'use '$x';' &>/dev/null || cpan -i "$x"; perl -e 'use '$x'; my $ip='$x'->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>
2009-12-13 02:23:40
User: sputnick
Functions: perl
1

Thanks to comment if that works or not...

If you have already typed that snippet or you know you already have IO::Interface::Simple perl module, you can type only the last command :

perl -e 'use IO::Interface::Simple; my $ip=IO::Interface::Simple->new($ARGV[0]); print $ip->address,$/;' <INTERFACE>

( The first perl command will install the module if it's not there already... )

MyIps(){ echo -e "local:\n$(ifconfig $1 | grep -oP 'inet (add?r:)?\K(\d{1,3}\.){3}\d{1,3}')\n\npublic:\n$(curl -s sputnick-area.net/ip)"; }
2009-12-06 22:52:31
User: sputnick
Functions: echo
1

Like the tiltle said, you can use an argument too ( the interface )

MyIps eth0

will show only the IP of this interface and the public IP

( tested with Linux )

You can add that function in ~/.bashrc, then

. ~/.bashrc

Now you are ready to call this function in all your terms...

perl -e '$_=`ifconfig eth0`;/\d+.\d+.\d+.\d+ /; print $&,"\n";'
2009-12-05 14:24:48
Functions: perl
0

If you are interested in interfaces other than eth0 you will need to change eth0 to your interface name.

You could use this mammoth to nab the ip4 addresses of all your interfaces

perl -e '@_=`ifconfig -a`; sort(@_); foreach(@_) { /(inet addr\:)(\d+.\d+.\d+.\d+ )/; $_=$2; @uniq=grep($_ ne $prev && (($prev) = $_), @_);} print join "\n",@uniq,"\n"; '

it seems silly to have all this code when the following will work fine

ifconfig -a | grep "inet " | awk -F":" ' { print $2 } ' | cut -d " " -f1

ifconfig eth0 | grep "inet:" | cut -d ":" -f2 | cut -d " " -f1
ifconfig -a| awk 'BEGIN{FS="[ :]+"} /Bcast/{print $4}'
ifconfig -a|grep Bcast:|cut -d\: -f2|awk '{print $1}'
/usr/sbin/ifconfig -a|awk -F" " 'NR==4{print $2}'
curl ip.appspot.com
2009-10-31 21:11:10
User: ktoso
22

Yeah I know it's been up here a million times, but this service is a really clean and nice one. Nothing but your IP address on it. Actually I was to write something like this, and noticed this on appspot... ;)

echo 127.0.0.1 | egrep -e '^(([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-4])\.){3}([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-4])$'
2009-09-17 17:40:48
User: arcege
Functions: echo egrep
-1

Handles everything except octets with 255. Ran through ip generator with variable octet lengths.

perl -wlne 'print $1 if /(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))/' iplist
2009-09-17 16:14:52
User: salparadise
Functions: perl
-1

if you want to only print the IP address from a file.

In this case the file will be called "iplist" with a line like "ip address 1.1.1.1"

it will only print the "1.1.1.1" portion

echo 254.003.032.3 | grep -P '^((25[0-4]|2[0-4]\d|[01]?[\d]?[1-9])\.){3}(25[0-4]|2[0-4]\d|[01]?[\d]?[1-9])$'
2009-09-17 12:59:44
User: foob4r
Functions: echo grep
0

This obey that you don't match any broadcast or network addresses and stay between 1.1.1.1 - 254.254.254.254