abhi's logbook

Adventures in coding and coding for adventures.

Introduction

Unix is like a war chest full of different tools to accomplish
different things. They can work together to get your job done.
They can be faster than the equivalent GUI tools.

The goal of this document is to show some of the power that Unix
posesses and hopefully to transfer some of that knowledge so others
can be just as productive with this wonderful system which has
largely stood the legacy of time.

Commands

Command Description
comm Compares two files. It allows you to see what is available in one file, the second file or both files. It is useful to see information on unions of files.

Setting up a new machine

I’m sure I can do this a better way using chef-solo or something but I
like to keep things simple.

passwd
/etc/sudoers
adduser abhi
apt-get update
apt-get upgrade
apt-get install zsh tmux emacs vim sudo git

git clone https://github.com/abhiyerra/dotfiles
cd dotfiles
sh dotfiler.sh

I really like Dropbox. It’s a convienient way to sync all your
computers and makes it quite easy to get started. However, the thing
that bugs me the most is keeping my dotfiles in sync as I usualyl
forget to sync between computers when I change
them. Thanksfully, Dropbox makes this easier.

  1. Create a directory in your Dropbox folder.
  2. Move your dotfiles from $HOME to that folder. Change the prefix . with _.
  3. Add this file (I named it dotfiler.sh, but you can name it whatever you want)

    #!/bin/sh

    for file in *
    do
    ln -s -n -f pwd/$file $HOME/${file/#
    /.}
    done

Now whenever you have a new machine just run the script in your
dotfiles directory. This should keep all your dotfiles in sync
whatever computer you are using and make setting up a new environment
less painful.

If you also added a .profile and .bashrc or .zshrc file make
sure to take your platform into account i.e., Linux, Mac OS X. You can
also run the script whenever you add a new file.

Zsh

cd -

Move back to the previous directory you were in.

[email protected]:~/Dropbox $ cd ~/.emacs.d/plugins/org-mode
[email protected]:~/.emacs.d/plugins/org-mode $ cd -
~/Dropbox
[email protected]:~/Dropbox $

!$

Gives the last argument from previously run command.

[email protected]:~/Dropbox $ mv junk ~/.emacs.d/plugins/org-mode
[email protected]: $ cd !$
[email protected]:~/.emacs.d/plugins/org-mode $

Network Utilities

lsof

Process running on a port

$ lsof -i :3000
COMMAND  PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    7634 ayerra   12u  IPv4 0x3a21119dc10aed27      0t0  TCP *:hbci (LISTEN)

ngrep

A super useful tool to sniff network traffic. Allows you to see what
is going in and out of your network device.

[email protected]:~ $ sudo ngrep
Password:
interface: en0 (192.168.1.0/255.255.255.0)
#
T 89.16.176.16:6667 -> 192.168.1.104:63781 [AP]
  :[email protected] PRIVMSG #emacs :+takes me back to list..
##
T 89.16.176.16:6667 -> 192.168.1.104:63781 [AP]
  :[email protected] PRIVMSG #emacs :+looks like it's in some monitored list but I can't figure where to look..
##
U 192.168.1.104:17500 -> 255.255.255.255:17500
  {"host_int": 89902014, "version": [1, 8], "displayname": "89902014", "port": 17500, "namespaces": [53605312, 4575490, 8623931, 69308427, 7943213, 98397390]}
#
U 192.168.1.104:17500 -> 192.168.1.255:17500
  {"host_int": 89902014, "version": [1, 8], "displayname": "89902014", "port": 17500, "namespaces": [53605312, 4575490, 8623931, 69308427, 7943213, 98397390]}
#
T 89.16.176.16:6667 -> 192.168.1.104:63781 [AP]
  :[email protected]/rking PRIVMSG #ruby :+Hehe, noobs unite!..

Here you see the packets from my connection to IRC being listed.

ssh

Connect without a Password

cd ~/.ssh
# Will ask for a ssh key name
ssh-keygen -t dsa
# Copy to remote host
scp ~/.ssh/<nameoffile>.pub remote:~/.ssh/authorized_keys2
ssh-add <nameoffile>

iptables

Block a port

iptables -A INPUT -j DROP -p tcp --destination-port 80 -i eth0