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 Network from sorted by
Terminal - Commands tagged Network - 115 results
for i in {1..6}; do printf "%0.2X:" $[ $RANDOM % 0x100 ]; done | sed 's/:$/\n/'
2010-12-07 19:26:58
User: forcefsck
Functions: printf sed
Tags: Network bash mac
2

Shorter and more straightforward.

Also in perl:

perl -e 'print join(":", map { sprintf "%0.2X",rand(256) }(1..6))."\n"'
h=0123456789ABCDEF;for c in {1..12};do echo -n ${h:$(($RANDOM%16)):1};if [[ $((c%2)) = 0 && $c != 12 ]];then echo -n :;fi;done;echo
2010-12-05 01:35:03
User: dabom
Functions: echo
Tags: Network bash mac
2

Doubt it actually generates valid mac addresses but this version doesn't need any external commands so it runs much faster.

Much shorter as well.

for i in {0..1200}; do for i in {1..12} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' | sed 's/.\(.*\)/\1/' ; echo; done
2010-12-04 16:44:11
User: Raymii
Functions: echo sed
2

First set the variable $hexchars:

hexchars="0123456789ABCDEF"

Change the number in the first for loop if you need less then 1200 mac addresses

socat TCP-LISTEN:locport,fork TCP:XXX.XXX.XXX.XXX:YYY,sourceport=srcport
2010-11-22 13:24:33
User: cyrusza
0

It connects to XXX.XXX.XXX.XXX port YYY, using a source port of "srcport" and binds the tunnel on local port "locport". Then you can connect to localhost:locport.

With this command it's possible to connect to servers using a specific source port (useful when a firewall check the source port). Because of the connections starting from the same source port, this works well only for the first connection (for example, works well with SSH and bad with HTTP because of multiple requests).

* It requires socat

curl ifconfig.me
while :; do ping -W1 -c1 -n 8.8.8.8 > /dev/null || tput bel > /dev/console; sleep 1; done
2010-09-24 06:34:12
User: hackerb9
Functions: ping sleep tput
0

This is like ping -a, but it does the opposite. It alerts you if the network is down, not up. Note that the beep will be from the speaker on the server, not from your terminal.

Once a second, this script checks if the Internet is accessible and beeps if it is not. I define the Net as being "UP", if I can ping Google's public DNS server (8.8.8.8), but of course you could pick a different static IP address. I redirect the beep to /dev/console so that I can run this in the background from /etc/rc.local. Of course, doing that requires that the script is run by a UID or GID that has write permissions to /dev/console (usually only root).

Question: I am not sure if the -W1 flag works under BSD. I have only tested this under GNU/Linux using ping from iputils. If anybody knows how portable -W is, please post a comment.

route -n | grep "^0\." | awk '{print "Gateway to the World: "$2", via "$NF""}'
ip route show | awk '$3 ~ /^[1-9]+/ {print $3;}'
2010-08-08 23:57:23
User: kerim
Functions: awk route
-1

Find your default gateway and print it directly output.

http://www.bayner.com/

kerim@bayner.com

route -n | awk '$2 ~/[1-9]+/ {print $2;}'
2010-08-08 23:51:07
User: kerim
Functions: awk route
-2

Find your default gateway and print it directly output

http://www.bilgisayarmatematik.com/

kerim@bayner.com

wget -qO- icanhazip.com
2010-06-24 03:49:14
Functions: wget
11

Curl is not installed by default on many common distros anymore. wget always is :)

wget -qO- ifconfig.me/ip
echo -e "GET /ip HTTP/1.0\nUser-Agent: netcat\nHOST: ifconfig.me\n\n" | nc ifconfig.me 80 | sed -n '/^[0-9]/p'
2010-06-16 19:08:05
User: putnamhill
Functions: echo sed
-2

Here's a version that uses netcat (although I'd much rather use curl!).

lynx --dump icanhazip.com
curl icanhazip.com
2010-06-14 18:47:11
User: pykler
-2

With a lolcat favicon if you access it from your browser

for i in 192.168.1.{1..254} ; do if ping -c1 -w1 $i &>/dev/null; then echo $i alive; fi; done
nmap -sP 192.168.1.0/24
2010-06-05 14:48:37
User: sdadh01
21

Works on any machine with nmap installed. Previous version does not work on machines without "seq".

Also works on subnets of any size.

for ip in `seq 1 255`; do ping -c 1 192.168.1.$ip ; done | grep ttl
/sbin/ifconfig|grep -B 1 inet |head -1 | awk '{print $5}'
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
181

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/

trickle -d 60 wget http://very.big/file
2010-03-29 06:55:30
Functions: wget
7

Trickle is a voluntary, cooperative bandwidth shaper. it works entirely in userland and is very easy to use.

The most simple application is to limit the bandwidth usage of programs.

nm-tool 2>/dev/null|sed -n '/Type:[ ]*802.11 WiFi/,/IPv4 Settings/{ /State:[ ]*connected/,/IPv4 Settings/{ s/^[ ]*//;/^\*.*Infra/ { s/^*//;s/:.*//;p }}}'
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.

sudo nmap -F -O 192.168.1.1-255 | grep "Running: " > /tmp/os; echo "$(cat /tmp/os | grep Linux | wc -l) Linux device(s)"; echo "$(cat /tmp/os | grep Windows | wc -l) Window(s) devices"
2010-01-10 03:09:56
User: matthewbauer
Functions: echo grep sudo
3

Shows how many Windows and Linux devices are on your network.

May add support for others, but that's all that are on my network right now.

ipcalc $(ifconfig eth0 | grep "inet addr:" | cut -d':' -f2,4 | sed 's/.+Bcast:/\//g') | awk '/Network/ { print $2 } '