Netcat
From Bobs Projects
Netcat is a swiss-army knife for network debugging.
Setting up a one-shot webserver on port 8080 to present the content of a file
(from Wikipedia)
{ echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c <some.file)\r\n\r\n"; cat some.file; } | nc -l 8080
The file can then be accessed via a web browser under http://servername:8080/. Netcat only serves the file once to the first client that connects and then exits, it also provides the content length for browsers that expect it. (This should work fine in a LAN, but probably may fail with any kind of firewall between.).
Copying block devices between systems
(see Using DD Over Netcat vs SSH).
On receiver end:
nc -l -p 19000 | bzip2 -d | dd bs=16M of=/dev/sdb
(some netcats may not need the -p flag before the listening port number...)
On sender:
dd bs=16M if=/dev/sda | bzip2 -c | nc receiver.example.net 19000
Before doing this, on the sending end it might help to:
dd if=/dev/zero bs=16M of=tmp1 rm tmp1
to fill all empty space on disk with zeros.
Whilst dd is in progress, you can send it a USR1 signal to get it to dump its byte count so far:
kill -s USR1 <pid of dd>
Logging connection attempts on a port
touch keep_watching while [ -r keep_watching ] ; do nc -lvp 5432 < /dev/null 2>> connects.txt ; date >> connects.txt ; done
How it works:
- runs in an indefinite loop, stopped by the removal of the "keep_watching" "file"
- netcat listens on a port 5432 (PostgreSQL in this case) and prints connection into to stderr (-v flag)
- incoming connections cause netcat to send /dev/null (nothing) and close connection, appending connection info to "connects.txt"
- date runs to "timestamp" the connection attempt
- do it all again.