========= CLI Tools ========= ------------------------------- netcat, tar, rsync, iperf, tee ------------------------------- .. class:: right Morgan Goose April 2011 .. class:: handout This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Netcat ======= "Netcat is a featured networking utility which reads and writes data across network connections, using the TCP/IP protocol. It is designed to be a reliable "back-end" tool that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities." - netcat_ .. _netcat: http://netcat.sourceforge.net/ Simple use ========== Think of it as a network pipe akin to | and here it is sending a file across the network. .. code-block:: bash #Server1 $ cat some.tgz | nc 80 #Server2 $ nc Server1 80 > some.tgz Proxy ====== This starts a nc server on port 12345 and all the connections get redirected to google.com:80, as well as sending te response back to the web browser. .. code-block:: bash $ nc -l 12345 -c 'nc www.google.com 80' tar === In computing, tar (derived from tape archive and commonly referred to as "tarball") is both a file format (in the form of a type of archive bitstream) and the name of a program used to handle such files. - wikipedia_ .. _wikipedia: http://en.wikipedia.org/wiki/Tar_%28file_format%29 .. raw:: html
This is a tar pipe =================== Since tar takes pushes to stdout and can recieve stdin .. code:: bash (cd src && tar -cf - .) | (cd dest && tar -xpf -) We can say change to src dir, and tar it up through a pipe to untar it in a dest dir. Or put simply "copy the src directory to dst, preserving permissions and other special stuff." - `extra cheese`_ .. _extra cheese: http://blog.extracheese.org/2010/05/the-tar-pipe.html Why that's cool ================ We can go back to *netcat* and use it to send data over a network (ssh can be used as well, but I mentioned nc) .. code:: bash #Server2 (receiving) $ netcat -l -p 7000 | tar x #Server1 (sending) $ tar cf - * | netcat Server2 7000 *note* I used the **-l** flag here to explicitly have a server listen for incoming, instead of pulling from the implicit serve of like the first example Side note on pipes and netcat ============================= You can also use this method to image/clone disk with remotely .. code:: bash #Server2 (receiving) $ nc -l -p 7000 | dd of=/dev/hda #Server1 (sending) $ dd if=/dev/hda | nc Server2 7000 -q 10 To be even more leet ===================== Pipe it through gzip to compress it before transfer .. code:: bash #Server2 (receiving) $ nc -l -p 7000 | gzip -dfc | dd of=/dev/had #Server1 (sending) $ dd if=/dev/hda | gzip -cf | nc Server2 7000 -q 10 rsync ===== .. code:: coolest flag: --link-dest=DIR Unchanged files are hard linked from DIR to the destination directory. The files must be identical in all preserved attributes in order for the files to be linked together. Beginning in version 2.6.4, multiple --link-dest directories may be provided, which will cause rsync to search the list in the order specified for an exact match. This option works best when copying into an empty destination hierarchy, as rsync treats existing files as definitive. iperf ===== A commonly used network testing tool that can create TCP and UDP data streams and measure the throughput of a network that is carrying them. .. raw:: html
Looks like this =============== .. code:: Client side: #iperf -c 10.1.1.1 ------------------------------------------------------------ Client connecting to 10.1.1.1, TCP port 5001 TCP window size: 16384 Byte (default) ------------------------------------------------------------ [ 3] local 10.6.2.5 port 33453 connected with 10.1.1.1 port 5001 [ 3] 0.0-10.2 sec 1.26 MBytes 1.05 Mbits/sec Server side: #iperf -s ------------------------------------------------------------ Server listening on TCP port 5001 TCP window size: 8.00 KByte (default) ------------------------------------------------------------ [852] local 10.1.1.1 port 5001 connected with 10.6.2.5 port 33453 [ ID] Interval Transfer Bandwidth [852] 0.0-10.6 sec 1.26 MBytes 1.03 Mbits/sec tee === normally used to split the output of a program so that it can be seen on the display and also be saved in a file. .. raw:: html
Good for logging only stdout, but seeing both stdout and stderr in the terminal, while making a sort of logfile of the program run.