Create simple arg parse in python

argparse is a module to make user-friendly command-line interfaces. It’s probably the one of the most frequently used module when I create a script in python that needs to parse some arguments. Check this out.

$ vim test.py
#!/usr/bin/python
import argparse

def init_args():
  parser = argparse. ArgumentParser(description="This is the description")
  parser.add_argument("--arg1", required=True, type=str, help="This is arg1")
  parser.add_argument("--allow", required=False, action="store_true", help="Allow mode")
  return parser.parse_args()

def main(arg1):
  return "arg1: %s"%(arg1)

if __name__ == "__main__":
  args = init_args()
  main(args.arg1)

See what happens when we run it

$ ./test.py --arg1 "showme"
arg1: showme

Sometimes we want to create an argumen but only store it as a True variable. we can just create simple test like this.

$ vim test.py
#!/usr/bin/python
import argparse

def init_args():
  parser = argparse. ArgumentParser(description="This is the description")
  parser.add_argument("--arg1", required=False, action="store_true", help="Enable arg1")
  return parser.parse_args()

def main():
  return "arg1 is enabled"

if __name__ == "__main__":
  args = init_args()
  if args.arg1:
    main()
  else:
    print "arg1 parse is not enabled"

Try to run it

$ ./test.py --arg1
arg1 is enabled

Setup SSH key passphrase only ask once

Sometimes it’s annoying when you’re trying to work with your project and whenever you need to enter your key passphrase. So I wanna make this passphrase prompt ask only once.

vim ~/.bash_profile

and put this at the bottom of the file :

eval $(ssh-agent)
ssh-add

Or if you don’t want to be asked for passphrase at all, you can just generate new passphrase with no password.


Traceroute command line in ubuntu

This is how to install traceroute in ubuntu machine

apt-get install traceroute

And how to use it :

$ traceroute google.com
traceroute to google.com (74.125.68.100), 30 hops max, 60 byte packets
 1  192.168.100.1 (192.168.100.1)  2.281 ms  2.378 ms  2.398 ms
 2  61.5.80.1 (61.5.80.1)  9.254 ms  9.357 ms  9.378 ms
 3  121.subnet125-160-11.speedy.telkom.net.id (125.160.11.121)  8.980 ms  8.966 ms  8.941 ms
 4  61.94.171.69 (61.94.171.69)  9.611 ms  9.630 ms  9.598 ms
 5  180.240.193.238 (180.240.193.238)  23.731 ms 180.240.193.46 (180.240.193.46)  32.981 ms  33.343 ms
 6  180.240.193.237 (180.240.193.237)  29.036 ms 180.240.193.45 (180.240.193.45)  41.533 ms 180.240.193.237 (180.240.193.237)  24.494 ms
 7  180.240.204.29 (180.240.204.29)  20.850 ms 180.240.204.49 (180.240.204.49)  20.496 ms 180.240.204.29 (180.240.204.29)  20.846 ms
 8  72.14.223.88 (72.14.223.88)  21.115 ms  21.529 ms  24.440 ms

Setup read and write samba share in vagrant ubuntu

I use vagrant for daily development, now vagrant has this directory mounted when we setup vagrant at the first time. But sometimes I’d like to mount my own directory to my local machine.

This is the way I create samba share in my vagrant :

apt-get install samba samba-common

Create user for project

useradd -m user1 -d /srv/myproject

Open samba config file

vim /etc/samba/smb.conf

Put the new config below at the very bottom

[myproject]
browseable = yes
path = /srv/myproject
guest ok = yes
public = yes
read only = no
create mask = 0644
directory mask = 0755
force user = user1

Restart samba :

service samba restart

What’s next :

Mount samba share with command line


Mount samba share with command line

When you work with vagrant, you might want your project folder to be mounted on some directory on your local. You can do that easily with file manager, but here’s my favorite way to mount my samba share in vagrant to my directory on my local. So I can work on that.

Before you do this, you might want to get your current user id, so your project is accessible. And make sure the destination directory is already exists.

sudo mount -t cifs -o uid=1001,gid=1001 //192.168.56.11/kripikpasta /tmp/kripikpasta

Setup nginx, php, wordpress in ubuntu

So after all these years I’ve had using apache for run something that runs with php, but since I know about nginx, I’ve been using this webserver instead of apache.

This wordpress blog is running with nginx and php-fpm, because not like apache that you can run anything about php without install the additional package. With nginx, you’ll need php-fpm to run php behind nginx.

This is the steps that I always use for make my php run behind nginx, especially when I setup this wordpress blog. Since I’m using mysql for database, you might want to install php package for mysql as well.

apt-get install php5-common php5-fpm php5-mysql

create nginx configuration for php website

vim /etc/nginx/conf.d/myweb.conf

myweb.conf

server {
    listen 80;
    server_name www.pulpn.com pulpn.com;
    charset utf-8;
    gzip_vary on;
    access_log /var/log/nginx/myweb.access.log;
    error_log /var/log/nginx/myweb.error.log;
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    location / {
        root /var/www/pulpn;
        index index.php;
        try_files $uri $uri/ /index.php?q=$uri&$args;
        location ~* \.php {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass  unix:/var/run/php5-fpm.sock;
        }
    }
}

Restart your nginx :

service nginx restart

Or

/etc/init.d/nginx restart