Archive

Archive for March, 2010

What's In Your .gitconfig?

March 18th, 2010 1 comment

git-logo_medI have been using git for over 2 years now. My current .gitconfig is below. What’s in your .gitconfig?

[user]
    name = vinod (mercury)
    email = myname@mydomain.com
    editor = vim
[diff]
    tool = vimdiff
[color]
    branch = auto
    diff = auto
    status = auto
    interactive = auto
    ui = true
    pager = true
[color "branch"]
    current = yellow reverse
    local = yellow
    remote = green
[color "diff"]
    meta = yellow bold
    frag = magenta bold
    old = red bold
    new = green bold
[color "status"]
    added = yellow
    changed = green
    untracked = cyan
[alias]
    st = status
    ci = commit
    br = branch
    df = diff
    lg = log -p
    co = checkout
    tree = log --graph --pretty=oneline --decorate
    pom = push origin master
    amend = commit --amend -C HEAD
[core]
    pager = less -FRSX
    whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
[apply]
  whitespace = fix

Happy Giting :-)

Categories: Uncategorized Tags:

Paramiko: SSH and SFTP With Python

March 17th, 2010 2 comments

pythonParamiko is a module for python 2.2 (or higher) that implements the SSH2 protocol for secure (encrypted and authenticated) connections to remote machines.

Emphasis is on using SSH2 as an alternative to SSL for making secure connections between python scripts. All major ciphers and hash methods are supported. SFTP client and server mode are both supported too.

Installing paramiko

First, we need to install paramiko, if you don’t have it already.

On Ubuntu/Debian:

$ sudo apt-get install python-paramiko

On Gentoo Linux:

$ emerge paramiko

Or install from source:

$ wget http://www.lag.net/paramiko/download/paramiko-1.7.6.tar.gz
$ tar xzf paramiko-1.7.6.tar.gz
$ cd paramiko-1.7.6
$ python setup.py build
$ su -c "python setup.py install"

Working with paramiko

SSHClient is the main class provided by the paramkio module. It provides the basic interface you are going to want to use to instantiate server connections. The above code creates a new SSHClient object, and then calls ”connect()” to connect us to the local SSH server.

Here’s a simple example:

import paramiko
ssh = paramiko.SSHClient()
ssh.connect('192.168.1.2', username='vinod', password='screct')

Another way is to use an SSH key:

import paramiko
import os
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey)

Running Simple Commands

Lets run some simple commands on a remote machine.

import paramiko
 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('beastie', username='vinod', password='secret')
stdin, stdout, stderr = ssh.exec_command('df -h')
print stdout.readlines()
ssh.close()

“paramiko.AutoAddPolicy()” which will auto-accept unknown keys.

Using sudo in running commands:

import paramiko
 
cmd    = "sudo /etc/rc.d/apache2 restart"
 
ssh    = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('beastie', username='vinod', password='secret')
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write('secret\n')
stdin.flush()
print stdout.readlines()
ssh.close()

Secure File Transfer Using SFTPClient

SFTPClient is used to open an sftp session across an open ssh Transport and do remote file operations.

An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called Channels, across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).

First we will create a Transport

import paramiko
import os
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
username = 'vinod'
transport.connect(username = username, pkey = mykey)

Now we can start the SFTP client:

sftp = paramiko.SFTPClient.from_transport(transport)

Now lets pull a file across from the remote to the local system:

remotepath='/var/log/system.log'
localpath='/tmp/system.log'
sftp.get(remotepath, localpath)

Now lets push a file to remote system:

remotepath='/var/www/images/file.png'
localpath='/tmp/file.png'
sftp.put(remotepath, localpath)

Finally, close the SFTP connection and the transport:

sftp.close()
transport.close()

Happy SSHing :-)

Categories: PYTHON Tags: ,

Keep Track Of Configuration Changes Using etckeeper

March 16th, 2010 3 comments

The official etckeeper website says:

etckeeper is a collection of tools to let /etc be stored in a git, mercurial, darcs, or bzr repository. It hooks into apt (and other package managers including yum and pacman-g2) to automatically commit changes made to /etc during package upgrades. It tracks file metadata that revison control systems do not normally support, but that is important for /etc, such as the permissions of /etc/shadow. It’s quite modular and configurable, while also being simple to use if you understand the basics of working with revision control.

Install etckeeper:

Debian/Ubuntu users can install etckeeper and git using apt.

$ sudo apt-get install etckeeper git-core

Configure etckeeper:

Open /etc/etckeeper/etckeeper.conf in your favorite text editor. The first option that you need to look at is VCS, which is the version control system you want to use. By default it’s set to git, but you can change it to hg or bzr depending on your preference.

If you want to specify some git commit options look ‘GIT_COMMIT_OPTIONS’

Another option that you may want to look is AVOID_COMMIT_BEFORE_INSTALL. By default, etckeeper will automatically commit any pending changes when you install packages. You can disable it by setting AVOID_COMMIT_BEFORE_INSTALL to 1.

Also set AVOID_DAILY_AUTOCOMMITS to 1 for avoiding daily auto commit.

Using etckeeper:

Initialize etckeeper:

$ sudo etckeeper init
Initialized empty Git repository in /etc/.git/
$ sudo etckeeper commit -m "Initial import"

This will create a  git repository for /etc, add all files below /etc (except etckeeper ignore list) to that repository and commit (save) the current contents .

Whenever you make a coherent change to your configuration files, you can commit it by calling etckeeper commit:

$ sudo vi /etc/mysql/my.cnf
$ sudo etckeeper commit -m "mysql conf change"

You can view your commits(saves) using simple git commands:

$ cd /etc/
$ sudo git log

or use a git front-end to see the commits:

$ cd /etc/
$ sudo gitk

etckeeper

Do try it out!

Categories: HOW-TOS, LINUX Tags: , ,

Switch to our mobile site