[cli] Follow changes in a growing file

Tags

, , , ,

I can think of three options two follow changes in a file (say, file.log) which is being updated by another program:

  1. Using tail: $ tail -f file.log
  2. Using less: $ less +F file.log
  3. Using watch: $ watch -d -n 10 "tail file.log"

(-d tells it to highlight changes and -n 10 to show changes at 10s interval)

In all the cases, you may exit from the ‘follow mode’ by pressing  <C-c>.

[cli] Tweet from the shell

Tags

, , , , , ,

I just added the following alias in the ~/.bash_aliases:

alias tweet='echo "What are you doing now?"; read mesg; curl -u kousikonline -d status="$mesg" http://twitter.com/statuses/update.xml'

where kousikonline is my Twitter user name. After sourcing the ~/.bashrc you should be able to tweet from the shell! (You’ll be asked for your Twitter password, of course!)

Reference: here.

[cli] Go the the previous directory

Tags

, , ,

First the “bad” way of doing it:
I would create an alias bk to go to the previous directory, which looks like the following:

In BASH:
alias bk='cd "$OLDPWD"'

In CSH/TCSH:
alias cd 'set old=$cwd;if ("\!:0-$" == cd) cd;if ("\!:0-$" != cd) cd \!$'
alias bk 'set backdir=$cwd;chdir $old;set old=$backdir;unset backdir'

(In my C-Shell, somehow the OLDPWD is undefined; that’s why I had to find a way around. Does anyone know how to set it?)

But there is a far better and more elegant way of doing it (which I just picked up from Commandline-fu:
$ cd -
Yeah, just the hyphen will take you to the previous directory! This works if the $OLDPWD variable is set (which is not set when you log in).

How about changing the aliases now?
alias bk='if [ -n "${OLDPWD+x}" ]; then cd -; else cd ~; fi'
in BASH, and
alias bk 'cd -'
in CSH or TCSH. Now they look a lot smarter!

[security] Introduction to encryption of files using GPG

Tags

, , , , , , , ,

Here’s a quick guide to create and encrypt files on the fly using GNU Privacy Guard (GnuPG or gpg, in short).

A. Keypair generation and key management

First, get GnuPG from here and install it.
Second, create your first private gpg key:

$ gpg --gen-key

It’ll then prompt you to choose a few options: first opt for 1 (DSA + RSA), then choose the maximum possible keylength (why not?). Then enter the validity period of the key — if you want to share the public key part, you should choose a finite validity period. However, for your own purpose you may choose a non-expiring key. Next enter required personal information: (a) real name, (b) comment (just a label for the key) and (c) email. Then verify the information and you’ll be asked to enter a passphrase. Choose a long but memorable passphrase.  It’ll generate a public and private key-pair in the ~/.gnupg directory.

You may want to check the installed keys on your keyring:

$ gpg --list-keys

If you want to get more information about a key, use

$ gpg -v --fingerprint <description>

The full form of <description> is ‘Real Name (comment) <your_email>’; but you may enter any part of it above since its purpose is just to identify the key. Probably the email is the most useful description for “uniquely” identifying a key. If you want to put more than one word in <description> in the above command, you must put quotes (`’) around the whole description string.

To delete a key, private or public (see below), using

$ gpg --delete-key <description>

 

.

B. Encryption for personal use

1. Encrypt a file on your computer, say filename.txt, using your private key:

$ gpg --encrypt --recipient <description> filename.txt

Here again <description> may be all or a part of the full description of the key. You may use terse forms of --encrypt (viz. -e ) and --recipient (viz. -r ) . You may save the output to a file of your choice (insead of the default, filename.txt.gpg) using --output or -o option. You may also want to delete the original unencrypted file, filename.txt.

2. In order to decrypt file.txt.gpg to new_file.txt, use

$ gpg --output new_file.txt --decrypt file.txt.gpg

It’ll ask for your passphrase.

.

C. Encryption for public sharing

1. Create an ASCII version of your public key already generated:

$ gpg --armor --output kousik_pubkey.txt --export <my_description>

You may freely distribute kousik_pubkey.txt to anyone who you want to share files with. Next time if your friend wants to send some files securely to you, s/he should encrypt it using your public key (of course, you should send him/her that first!) using the following technique.

2. The friend first will import my public key to his/her keyring:

$ gpg --import kousik_pubkey.txt

S/he should verify if it is in his/her keyring using
$ gpg --list-keys
As a security measure, s/he should also check if the fingerprint is OK (see above in key management part)

3. Then s/he’ll encrypt the  file, say securefile.txt, using my public key

$ gpg --encrypt --recipient <my_description> securefile.txt

and send me the encrypted securefile.txt.gpg as an email attachment.

4. I’ll decrypt the file in the usual manner, as if I encrypted it on my own computer (see above).

.

N.B. This may also be used in the same way as OpenSSL:

$ gpg --cipher-algo aes256 -c -o filename.txt.gpg filename.txt

Refereces: here and here, as well as on the GnuPG HowTo page.

[security] Encrypt files in the command line using OpenSSL

Tags

, , , , , , , , , , , , ,

In order to make our life easier, first let’s create two aliases to encrypt (sslenc) and decrypt (ssldec) files in the command line using OpenSSL, respectively:

$ alias sslenc='openssl aes-256-cbc -salt -a'
$ alias ssldec='openssl aes-256-cbc -d -a'

In order to encrypt a file, say filename.txt, you should do something like this:

$ sslenc -in filename.txt -out filename.txt.enc && rm -f filename.txt

which first encrypts the input file to filename.txt.enc (“enc” suffix  is there just to tell you that it is encrypted) and then the rm command removes the input file. To decrypt it into new_filename.txt use the following:

$ ssldec -in filename.txt.enc -out new_filename.txt

.

Now back to beginning: let’s explain the various flags in the aliases given in the top:

  • aes-256-cbc: a command to encrypt using the Advanced Encryption Standard cipher with key size of 256 bit, which makes use of the Cipher-Block Chaining mode. Currently aes-256-cbc is the the standard cipher choice of the US government. You may change this command (i.e., “aes-256-cbc”) by “enc -aes-256-cbc” which means the same thing, but in a longhand: encrypt (enc) using the cipher, aes-256-cbc.
  • -salt: adds strength to the encryption and should always be used. (see wikipedia page for salt).
  • -a: indicates that the encrypted output will be base64 encoded, this allows you to view it in a text editor or paste it in an email (optional).
  • -d: for file decryption.

You may also be interested in the wikipedia article which talks about the key size.

Reference: link.

Design a site like this with WordPress.com
Get started