Monday, February 4, 2013

attach a file in an email

Mail (or, mailx) is a convenient utility for sending and receiving emails on unix/linux systems.Here is a tip on how to send an email with an attachment.

To send an email with message stored in file MailBody and a picture in file Pic.jpg as an attachment, we type:

(cat MailBody; uuencode Pic.jpg Pic.jpg)|mail -s "Subject of this mail" someone@some.where.com

Sunday, February 3, 2013

Netcat tricks

Netcat is a utility which reads and writes data across network connections, using TCP or UDP. It is known as a "Swiss-army knife for TCP/IP". Here are some useful netcat tricks.



Simple Client

To connect to the SMTP server (srv), we type:

nc srv 25



One-shot Server

To provide a uptime service on a unix/linux system, we type:

nc -l -p Port# -e /usr/bin/uptime

where Port# is the service port number, uptime the utility reporting system uptime.



Simple Server

To provide a fixed web page (index.html) service on a unix/linux, we type:

while true; do {echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c < index.html)\r\n\r\n"; cat index.html; } | nc -l -p Port#; done

To implement a log server that logs the first 500 bytes data from any client, we type:

while true; do nc -l -p Port# | head -c 500 >> LogFile; date >> LogFile; done

where LogFile is the name of our log file, each log record is followed by a time stamp.

To set up a backdoor shell, we type:

nc -l -p Port# -e /bin/sh



File Transfer

To push a file (FileName) from host A to host B (hostB) , we type

nc -l -p Port# > FileName

on host B, and

nc -q 10 hostB Port# < FileName

on host A.


To pull a file (FileName) from host A (hostA) back to host B, we type

nc -q 10 -l -p Port# < FileName

on host A, and

nc hostA Port# > FileName

on host B.



Chat Service

To set up chat server (srv), we type:

nc -l -p Port#

on srv, and on chat client, we type:

nc srv Port#



Port Scanner

Netcat can be a port scanner. It does not have as many features as  nmap, but if we just want to see what ports numbered between 1 to 1024 are open on host with IP address 192.168.0.1, we type:

nc -z -v -n -w 1 192.168.0.1  1-1024

where -z specifies scanning, -v verbose, -n no DNS, -w 1 timeout after 1 second.



UDP mode

The option -u puts netcat to operate in UDP mode.

Wednesday, January 30, 2013

Using diff and patch

Diff is a file comparison utility that computes the differences between two files. The output of diff is called a "diff" or a "patch," since it can be applied to another utility named patch to update an old file to a new version. Here is a simple how-to on using diff and patch.

To compute the patch between OldFile and NewFile, we type:


diff -u OldFile NewFile > PatchforOld

where -u specifies that the differences is represented in unified format. The diff (patch) is stored in file PatchforOld. To apply the patch to OldFile, change to the directory where it is located and type:

patch < PatchforOld

or

patch -i PatchforOld

It is not necessary to specify the target file for a patch, which is included in the patch file generated in unified format. If something went wrong (for example, patch was  created  with  the  old  and  new  files swapped), you are able to reverse your patch (and revert to OldFile) by issuing:

patch -R -i PatchforOld


To compute the patch between two directories OldDir and NewDir, we type:

diff -urN OldDir NewDir > PatchforOldD

where -r means recursively compare any subdirectories, -N treats absent files as empty.

Tuesday, January 29, 2013

an advanced usage of screen

The following script is an advance usage of command screen, which allows us to connect to a list of machines with few keystrokes (if key-based ssh logins is enabled). This is quite handy if we need to manage a long list of servers from our desktop.

#!/bin/bash
screen -d -m -S srvA   ssh serverA.domain.name

screen -d -m -S srvB   ssh serverB.domain.name
screen -d -m -S srvZ   ssh serverZ.domain.name

where options
-d -m        creates a new screen session in  detached mode
-S srvA  names identify of the session with the name srvA


To work on srvB, you just attach to its screen by typing:

screen -r srvB

Key-based ssh logins

Key-based authentication is a much more secure mode of authentication usable with (Open)SSH. It may provide a way for ssh automatic login. Here is the procedure to set up a client:

1. generate a key pair
The following command will generate RSA keys with a bit length of 4096 for use by SSH protocol version 2:

ssh-keygen -t rsa -b 4096


2. transfer client (public) key to server
The public key is normally stored in the file $HOME/.ssh/authorized_keys. This can be done by issuing:

ssh-copy-id -i server

where -i specifies that the key to be copied is stored in its default location ($HOME/.ssh/id_rsa.pub, in our example). In fact, it can be done by any your preferred method to move file from client to server.


For ssh automatic login, we need the help from ssh-agent, which allows us to store (using ssh-add, for example) our private key identities to the authentication agent.

Sunday, January 27, 2013

FreeBSD upgrade

I just finished upgrading host pdp from FreeBSD 9.0 to 9.1. There are few commands to get job done but lots of effort involved.

Four steps to upgrade from 9.0 to 9.1:
  1. to gather information necessary for the upgrade
    freebsd-update -r 9.1-RELEASE upgrade
  2. committing the upgrade
    freebsd-update install
  3. rebooting the system
    shutdown -r now
  4. committing the second phase installation
    freebsd-update install
Most effort was paid to upgrade installed packages/applications. A brute-force rebuild of all installed packages was done with:

portupgrade -af

which takes long hours. With the help from screen, it ran smoothly, thought taking more than 1 day.

Thursday, January 24, 2013

screen -- a terminal based window manager

Screen is a window manager that multiplexes a physical terminal between several processes. It allows users to run several interactive shell processes within one physical terminal. A subtle application of screen is that it enables processes running despite a dropped connection.

The command screen creates a single window with a  shell and then gets out of our way so that we can use the shell as we do normally.

Everything we type is sent to the process running in the current window, except for one keystroke that is used to initiate a command to the window manager. By default, each command begins with a control-a (Ctrl-a), and is  followed by one other keystroke. The most important screen commands that we needs are:
  • Ctrl-a c -- create a new window and switch to that window
  • Ctrl-a w -- show a list of windows
  • Ctrl-a n -- switch to the next window
  • Ctrl-a p -- switch to the previous window
  • Ctrl-a 0 -- switch to window number 0
  • Ctrl-a 9 -- switch to window number 9
  • Ctrl-a d -- detach screen from this terminal

To detach a screen session and return to your normal terminal, type:

Ctrl-a d

All processes (in the screen session) continue to run when screen is detached from the user's terminal.


To get a list of your current screen sessions, (in your normal terminal) type:

screen -ls


In case there is only one screen session, you may reattach to it by typing:

screen -r


In case there are more screen sessions running, you should specify which session to reattach by typing:

screen -r [[pid.]tty[.host]]

where [[pid.]tty[.host]] is the session information obtained by command screen -ls