Pages

Showing posts with label Linux Tips. Show all posts
Showing posts with label Linux Tips. Show all posts

Monday, 6 September 2010

Linux Command netcat(nc)

What is 'netcat' (nc) ?
         Netcat is a generic utility capable to doing anything involving TCP/UDP connections.  It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning.
 
Syntax:
       netcat [flags] [hostname] [port]
               or
       nc [flags] [hostname] [port]

       Flags:
               -v ----> Include a verbose o/p to understand what netcat is doing
               -z ----> Check if the specified port [port] is open on server [hostname]

What can 'netcat' do?
It can be used a tool for any of the following
  1. Port Scanning.
  2. File Transfer
  3. Chat
1) Port Scanning:
          When '-z' flag is specified, netcat reports whether the specified ports are open or not.
Example:
hprem@hprem-lnx:~$ netcat -v -z hprem-lds 20-30
netcat: connect to hprem-lds port 20 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 21 (tcp) failed: Connection refused
Connection to hprem-lds 22 port [tcp/ssh] succeeded!
netcat: connect to hprem-lds port 23 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 24 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 25 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 26 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 27 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 28 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 29 (tcp) failed: Connection refused
netcat: connect to hprem-lds port 30 (tcp) failed: Connection refused

2)Chat

         To start a chat session between two hosts. For that start netcat in listen mode on some random port On host1 .In the following examples it is assumed that the machine that creates the session(listening connection) has the IP address 192.168.1.4 . So, create the chat server on this machine and set it to listen to 9600  TCP port::
       netcat -l 9600

On the other host, start netcat On the same port:
       netcat 192.168.1.4 9600

Anything that is typed on host2 would get echoed on host1 and vice-versa


3)File Transfer
       To transfer file between two hosts, start netcat in listen mode on the host that should accept the file, on some random available port and redirect the o/p to a file
       On host1:
       netcat -l 9600 > filename

On the other server, start netcat and as it to read data from the file that has to be copied
       On host2:
       netcat 192.168.1.4 9600 < filename
   netcat does not show any info about the progress of the data transfer. This is inconvenient when dealing with large files. In such cases, a pipe-monitoring utility like pv can be used to show a progress indicator.

Linux Command BC(Basic Calculator)

BC(Basic Calculator)
What is 'bc'
  • 'bc' (basic calculator) is a command line calculator. It reads "expressions to be evaluated" from stdin and sends output to stdout. So you can pipe the output of any linux command to 'bc' for further calculations and you can pipe the output to any other linux command.
  • Since 'bc' reads input from stdin, the common usage is of the form
         echo "expr" | bc     (or)        bc <<< "expr"
Why 'bc':

  • You dont have to load a GUI calculator to do simple calculations.
  • Precision can be set to any value (most of the graphical calculators has a precision upto 10 digits). Precision is limitted only by the processing power of your computer. You can find the value of pi upto 1000 decimal places within 1.5 mins with a normal computer.
  • Available by default with all flavours of linux by default
1.Example for Addition, Subtraction, Multiplication, Division, Power, square root


sunitha@sunitha-lappy:~$ echo "10+2" | bc
12
sunitha@sunitha-lappy:~$ echo "10-2" | bc
8
sunitha@sunitha-lappy:~$ echo "10*2" | bc
20
sunitha@sunitha-lappy:~$ echo "10/2" | bc
5
This can be written simple as
sunitha@sunitha-lappy:~$ bc <<< "10+2"
12
sunitha@sunitha-lappy:~$ bc <<< "10-2"
8
sunitha@sunitha-lappy:~$ bc <<< "10*2"
20
sunitha@sunitha-lappy:~$ bc <<< "10/2"
5

The scale (number of digits after decimal point), by default is set to 2
sunitha@sunitha-lappy:~$ bc <<< "scale=2;10/2"
5.00
sunitha@sunitha-lappy:~$ bc <<< "2^3"
8
sunitha@sunitha-lappy:~$ bc <<< "scale=20;sqrt(10)"
3.16227766016837933199


2.Grouping - using parenthesis

sunitha@sunitha-lappy:~$ bc <<< "10+(2*3)"
16

3.Relational Expressions - <, >, >=, <=, ==, !=

sunitha@sunitha-lappy:~$ bc <<< "5<3"
0
sunitha@sunitha-lappy:~$ bc <<< "5>3"
1
sunitha@sunitha-lappy:~$ bc <<< "5>=3"
1
sunitha@sunitha-lappy:~$ bc <<< "5<=3"
0
sunitha@sunitha-lappy:~$ bc <<< "5==3"
0
sunitha@sunitha-lappy:~$ bc <<< "5!=3"
1

4.Bitwise Operations:
Base - obase, ibase
Base conversion -- Hexadecimal to Decimal
sunitha@sunitha-lappy:~$ bc <<< "obase=16;124"
7C
Base conversion -- Decimal to Hexadecimal
sunitha@sunitha-lappy:~$ bc <<< "ibase=16;7C"
124

Hexadecimal calculation
sunitha@sunitha-lappy:~$ bc <<< "obase=16;ibase=16;7C+F"
8B

sunitha@sunitha-lappy:~$ bc <<< "ibase=16;obase=10;7C+F"
8B

Binary calculation:
sunitha@sunitha-lappy:~$ bc <<< "obase=2;28"
11100
sunitha@sunitha-lappy:~$ bc <<< "obase=2;ibase=2;11100+10011"
101111

5.MATH LIBRARY
       If bc is invoked with the -l option, math library gets preloaded, making a lot more math functions available to us.
Note: Default scale is set to 20 when bc is started with -l option

(a) sine,cos,tan and arctangent
Note: pi can be calculated using the formula, pi=4*arctangent(1)
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);pi"
3.14159265358979323844
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);s(pi/2)"
1.00000000000000000000
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);c(pi/2)"
.00000000000000000001

(b)exponential and logarithmic functions
sunitha@sunitha-lappy:~$ bc -l <<< "l(e(10))"
9.99999999999999999999

Some Cool tips
* Arbitrary precision (scale)
bc has a default scale of 0, which is quite annoyoing as bc <<< 3/2 gives 1 instead of 1.5.  bc -l would solve this issue

sunitha@sunitha-laptop:~$ bc <<< 3/2 -l
1.50000000000000000000
 
  The default scale is 20. But if you need any arbitrary precision, say you want the default scale to be 5 whenever you invoke bc, follow this

In your .bashrc in your home directory add "export BC_ENV_ARGS=~/.bc"
Then create the file named ".bc" in your home directory, with the following one line
        scale=5
Now, bc <<< 5/3 gives 1.66666

* Alarm
To start audacious (the default music player that comes with ubuntu) after 8 hrs (to server as an alarm)
sleep `bc <<< 8*60*60`; audacious -p