Friday, December 17, 2010

Linux Search & Replace Text in Directory Structure

Introduction

I have to perform a find and replace of text in files for an entire directory structure.  I do not do this often, so I have to look it up every time.  Unfortunately, it takes more time to look up than I would expect so here are some example.

Example #1
Find all files named "Root" and replace the text "FIND_ME" with the text "REPLACE_WITH"

find . -name Root -exec sed -i 's/FIND_ME/REPLACE_WITH/g' '{}' \;

Thursday, December 2, 2010

TcpDump

Introduction
A project I was working on required me to verify network traffic activity over specific ports.  I have not had much experience doing this but I knew Linux tcpdump would be my tool of choice.  Unfortunately, I was working in a Windows environment but it was easy enough to find a port of tcpdump for Windows.

Download
I found MicroOlap had an easy to use port of tcpdump available for download.
http://www.microolap.com/products/network/tcpdump/

Installation
Installation is simple, just unzip it.

Find Your Network Interface (Device)
Your computer may have more than one network interface.  A laptop will have an Ethernet card for a network cable but it will also have a wireless Ethernet card.  You need to know which one you are using and tell tcpdump to listen for network traffic on the device.  To get a list of devices on your computer, use the -D switch.

C:\Apps\tcpdump>tcpdump -D

*******************************************************************
**                                                               **
**        Tcpdump v4.0.0 (October 27, 2008) for Windows          **
**          Win 98/ME/NT4/2000/XP/2003/Vista/2008/Win7           **
**                                                               **
**      built with MicroOLAP Packet Sniffer SDK v4.2 and         **
**   MicroOLAP WinPCap to Packet Sniffer SDK migration module.   **
**                                                               **
**     Copyright (c) 1997 - 2009 MicroOLAP Technologies LTD,     **
**                  Khalturin A.P. & Naumov D.A.                 **
**                   http://www.microolap.com                    **
**                                                               **
**                         Trial license.                        **
**                                                               **
*******************************************************************

1.\Device\PssdkLoopback (PSSDK Loopback Ethernet Emulation Adapter)
2.\Device\{8C8A81C7-D190-4A6D-88D9-7C83CF11BD1F} (VMware Virtual Ethernet Adapter for VMnet1)
3.\Device\{9AA64137-DCCD-4292-802D-7568D75004AA} (DW1520 Wireless-N WLAN Half-Mini Card)
4.\Device\{B58EA48A-BD5C-400D-94FD-02659C8B659A} (Intel(R) 82577LM Gigabit Network Connection)
5.\Device\{F61893D4-5284-48ED-960D-27A777C4D1DC} (Check Point Virtual Network Adapter For SecureClient)
6.\Device\{952066E8-9363-4AB5-8E8F-20F64C7DB32F} (VMware Virtual Ethernet Adapter for VMnet8)

Listen for Incoming Traffic on a Specific Port
After determining your interface, run the following command to listen for incoming traffic on a specific port.

C:\Apps\tcpdump>tcpdump -i 5 -nnvvXSs 1514 port %PORT_NUMBER%

-i 5  Specifies to which interface to use, which in this example is #5 above.
%PORT_NUMBER% Is replaced with the port number to listen for incoming traffic.

There is a lot more to tcpdump, but that is enough for now.

Friday, November 12, 2010

Running Vuze (Azureus) BitTorrent Client From Command Line (SSH Terminal)

Introduction
Finding command line BitTorrent clients has been a challenge.  My challenge is finding one which passes the 5 minute rule.  If I want a GUI based BitTorrent client I can download and install uTorrent, click on the torrent's link, and be downloading within 5 minutes.  For command line clients, I have run into python errors, C library version issues, and other problems which are not resolved in 5 minutes and not worth the effort to.
      
Vuze (Azureus) is a major Java based GUI BitTorrent client however a little searching online revealed it can also be run from a command line.  Getting it up and running and being able to monitor the torrent download was not too painful.  It required searching to solve some problems so it did not pass the 5 minute rule but it was not too bad.  I document my research here so it is all in one place.

Download Vuze (Azureus) and supporting libraries
You need to download 3 files.  Put them all in the same directory

Vuze_<version>.jar
Select to download the "Jar" from the download page or try downloading directly.

log4j.jar 
Search and download log4j.jar or try downloading directly.

commons-cli.jar
Search and download commons-cli.jar or try downloading directly.
Running Vuze (Azureus) from the command line
To run Vuze (Azureus) from the command line, execute the following command (Linux assumed)

$ screen java -jar Vuze_4510.jar --ui=console

Yes, you will want to use the
screen command to run it.  Why you want to use screen will become apparent soon.
Console commands
The available console commands are easy enough.  Type "help" to get a list of available commands:
> -----
help
> -----
Available console commands (use help <command> for more details):

  [] xml   [#] hack   [a] add   [c] check   [q] queue   [r] remove
  [s] start   [h] stop   [] host   [] publish   [] forcestart   [tl] tlog
  [l] log   [m] move   [] share   [+] set   [sh] show   [u] ui   [] logout
  [] quit   [?] help   [] alias   [] prio   [] plugin
> -----


Type "help <command>" to get help for a specific command.  To get help for the "add" command, type "help add"
> -----
help add
add [addoptions] [.torrent path|url]            a       Add a download from the given .torrent file path or url. Example: 'add /path/to/the.torrent' or 'add http://www.url.com/to/the.torrent'
> -----
add [addoptions] [.torrent path|url]            a       Add a download from the given .torrent file path or url. Example: 'add /path/to/the.torrent' or 'add http://www.url.com/to/the.torrent'
    -r,--recurse               recurse sub-directories.
    -f,--find                  only find files, don't add.
    -h,--help                  display help about this command
    -l,--list                  list previous find results
    -o,--output <outputDir>    override default download directory
> -----

Adding a Torrent
Adding a torrent is easy.  Use the "add" command combined with either the URL or file system path to the torrent.
> -----
add http://url/of/the/file.torrent


> -----
add /path/of/the/file.torrent

Checking Progress of a Torrent
To see if a torrent is finished, use the "show torrents" command

> -----
show torrents
> -----
 1 [>] 078.2%   Something being downloaded (3.27 GB) ETA: 21h 26m
                Speed: 9.7 kB/s / 4.8 kB/s      Amount: 2.56 GB ( 1.76 MB discarded ) / 515.76 MB       Connections: 4(8) / 2(9)

 2 [>] 071.7%   Another thing being downloaded (3.49 GB) ETA: 12h 36m
                Speed: 23.8 kB/s / 5.6 kB/s     Amount: 2.51 GB ( 1.45 MB discarded ) / 270.24 MB       Connections: 5(8) / 2(8)

Total Speed (down/up): 33.5 kB/s / 10.5 kB/s
Transferred Volume (down/up/discarded): 5.08 GB / 786.01 MB / 3.21 MB
Total Connected Peers (seeds/peers): 9 / 4
> -----

Logging out of Your SSH Session and Coming Back
After you added torrent(s), you want Vuze (Azureus) to continue running in the background until it is finished but you also want to be able to come back to it to check on the progress.  So how do you do this?

Well, because the
screen command was used to start the application, to leave and let Vuze (Azureus) to continue running in the background all you need to do is close your console window or exit your Putty screen.  Basically just end your connection without exiting or logging out.
To come back and check on the progress of your torrent download is just as easy.  SSH to you account then execute the following command
$ screen -ls
There is a screen on:
        31432.pts-0.server      (Attached)
1 Socket in /var/run/screen/S-michael.
You should see a screen listed.  Now execute the following command  to return to the screen.
$ screen –x 31432

And there you have it, you are back at the Vuze (Azureus) console interface!

References
http://wiki.vuze.com/w/ConsoleUI
http://wiki.vuze.com/w/Commandline_options
http://azureus.sourceforge.net/download.php
http://sourceforge.net/projects/azureus/files/vuze/Vuze_4510/Vuze_4510.jar/download
http://svn.vuze.com/public/client/trunk/uis/lib/log4j.jar
http://svn.vuze.com/public/client/trunk/uis/lib/commons-cli.jar