Friday, April 26, 2013

Logical AND and OR in bash

In bash (bourne again shell), the logical operand AND and OR are being symbolized by && and ||. The usage example are as follow:

&& ( If first command succeed, continue with second command, else stop )

$ ls 
cat  lion  tiger

$ ls cat && echo "there is cat"
cat
there is cat
 
$ ls elephant && echo "there is elephant"
ls: cannot access elephant: No such file or directory


You can see from above that command `echo "there is elephant"` did not get executed because the command `ls elephant` did not successfully finish (non zero exit code)


|| ( If  the first command failed, execute second command, else stop )

$ ls 
cat  lion  tiger

$ ls cat || echo "there is no cat"
cat

$ ls elephant || echo "there is no elephant"
ls: cannot access elephant: No such file or directory
there is no elephant


Now you can see that, if the first command returned non zero exit code (failed), the second command will be executed.

To run the second command, while ignoring the first command's result, use ";" instead:

$ ls tiger; echo "there is tiger"
tiger
there is tiger

$ ls elephant; echo "there is no elephant"
ls: cannot access elephant: No such file or directory
there is no elephant


That's all folks.





Thursday, April 25, 2013

ssh through socks proxy

This technique is very useful if you have a firewall between you and your destination, and somehow the only way you could get in to the destination is by ssh'ing into a jumpbox and ssh again to the destination. In this scenario example, I'll call the machine we initiate this technique as A.local, the jumpbox as B.local and the destination server as C.local, and we will use a user called aladdin.

A.local -> B.local (jumpbox) -> C.local

To do this, please follow below steps:

Add below settings to your ssh config in A.local, the file is usually ~/.ssh/config
Host B.local 
DynamicForward localhost:1080 
Host C.local 
ProxyCommand /usr/bin/nc -x localhost:1080 %h %p

Initiate a socks proxy connection, and leave it open (-D is for dunamic application-level port forwarding and 1080 can be any port of your choice, 1080 is socks proxy default port for nc):
[A.local]$ ssh -D 1080 aladdin@B.local

Open another terminal, and run ssh as if you have direct connection to C.local
[A.local]$ ssh aladdin@C.local

Voila, your ssh session will go through as if you have direct connection to C.local.


If you just doesn't want to put it into your config, you can use it on the fly by using below command after you have initiate the socks proxy:

[A.local]$ ssh -o "ProxyCommand /usr/bin/nc -x localhost:1080 %h %p" aladdin@C.local

Or you can also put it as alias for easy usage:

[A.local]$ alias 

alias proxyssh='ssh -o "ProxyCommand /usr/bin/nc -x localhost:1080 %h %p"'




Using curl to check on site availability

The easy way to check if a site is available, is by using curl. For example, if you want to check whether www.google.com is available or not, jut run:

$ curl -I www.google.com

HTTP/1.1 200 OK

Date: Thu, 25 Apr 2013 06:03:06 GMT

Expires: -1

Cache-Control: private, max-age=0

Content-Type: text/html; charset=ISO-8859-1

Set-Cookie: PREF=ID=f4eacf44ddfe9913:FF=0:TM=1366869786:LM=1366869786:S=pD4jQT9xbgTOjuKG; expires=Sat, 25-Apr-2015 06:03:06 GMT; path=/; domain=.google.com

Set-Cookie: NID=67=KULBN37y3Mw7TIYNurxqV3L9OAm0gaj4VhRxz0_OsayoTS8C7nPN9QLCMovAzkVxhKfoop1EcHjWiBWjv7Vxl2C5iQ-Z8J0zcVtv4YfrJXs2ypRegbp2Y8MPcJjTyX1p; expires=Fri, 25-Oct-2013 06:03:06 GMT; path=/; domain=.google.com; HttpOnly

P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."

Server: gws

X-XSS-Protection: 1; mode=block

X-Frame-Options: SAMEORIGIN

Transfer-Encoding: chunked 
 
with -I for fetching the HTTP-header only. In this case the return code is 200, and it means the site is available.

Monday, February 11, 2013

Clear off swap

Today I have an issue where my swap is almost full. To check your swap usage:

$ free -m

                    total       used       free     shared    buffers     cached

Mem:          1024        157        866          0         11         48

-/+ buffers/cache:         97        926

Swap:          127         95         32

where -m is for the value to be listed in megabytes.

To clear the swap off, please use below commands (please run as root, or sudo):

# sync && /sbin/sysctl vm.drop_caches=3 && swapoff -a && swapon -a 


where sync is to force changed blocks to disk and update the super block; sysctl vm.drop_caches=3 is to free pagecache, dentries and inodes; swapoff is to turn off swap and swapon is to turn swap back on, obviously :)

p.s. Thanks to linuxnetadmin and linuxinsight for the tips.

And you will get free swap after that:

$ free -m

                    total       used       free     shared    buffers     cached

Mem:          1024        157        866          0         11         48

-/+ buffers/cache:         97        926

Swap:          127         0         127
 
 
That's all folks.

Friday, February 1, 2013

ldapvi - ldap client using text editor

Today I stumbled upon a good tool in managing ldap, which is ldapvi. The reason this tool champs over my previous ldap management tool, which is phpldapadmin, is, it is based on text editor, and can be used in terminal ~ cool :)

To install this tool, all you need is to use your package manager, in my case, yum:

$ sudo yum install ldapvi

Once you have finished installing, launch it with your username and ldap server:

$ sudo ldapvi --user cn=admin,ou=people,dc=local,dc=lan  --host ldap.local.lan

For first time use, you need to set your preferred text editor. Once logged in, you can change the ldap record as if you are working on a very long text file. Once you are done, save like how you normally save a text file, ldapvi will ask for confirmation, and you are done.