Friday, October 5, 2007

Python Scripting Tutorial, Part one


Python is a great tool to work with. I like the fact that I can accomplish all sorts of tasks, from socket programming to web development to GUI) in just one language. Python has an incredible standard library, it is very readable and I found it to be very fast. I prefer it over BASH because it seems I just can make the damn thing work.

In this series, we'll first start on with some very simple scripts and then move on, developing on those and adding more features. This is a hands-on tutorial, because you have to write the code and try to understand it. As I always comment my code excessively when writing tutorials, I won't explain the same thing twice.

In this part you will learn how to:
  • find out if a program is running by searching for its pid
  • get simple command line arguments
  • learn what a list is and how to access it

As you may already know, I have moved my sshd port to 8722 on my servers (they are all in sync). Also, I use only public/private keys for authentification using ssh-agent and ssh-add. I found it very cumbersome that everytime I want to ssh to see if ssh-agent is already running, then if it's not, ssh-add the key, and type that long command. Here is the command that I actually type to get into one of my hosts:

ssh rsavu@host1 -p 8722

The problem is I don't have any short hosts like host1 in /etc/hosts and I just want to make a script that automates this task (okay, admit I need it just for the sake of argument).

What should the script do? Well, if it receives an argument (user@host is necessary) it should connect to that host using the port 8722. If it doesn't receive any arguments it should print a menu with known hosts.

What to improve on in the next parts

  • check if the format of the first argument is user@host
  • read hosts from /etc/hosts
  • read all other options from a configuration file that can be thrown through a command line argument or be a predefined one
  • suppressing any warnings
The script

That thing above is called a shabang line for anyone that did not know that
It instructs the shell what interpreter should be used for the following
lines of code

These are the libraries we are going to work with.
import sys
import os
The main function
def main(argv):
# First let's find out if ssh-agent is working
pid = os.system("pidof -s ssh-agent")
if pid == 256:
# Let's see how many arguments we have
argc = len(argv)
# Test if we don't have any argument
if argc == 1:
# This is a list
knownHosts = ["user@host1", "user@host2"]
# Request some input from the user and transform it in an integer
option = int(raw_input("Choose an option: "))
remoteHost = knownHosts[option-1]
elif argc == 2:
remoteHost = argv[1]
cmd = 'ssh ' + remoteHost + ' -p 8722'
print cmd

Prints a table with know hosts
def printTable(knownHosts):
i = 1
for host in knownHosts:
# In the next line the statement `i` makes us able concat a string and int
print `i` + ". " + host
i = i+1

This basically means that if this file is ran independently and not included
in any other file it should call function main with that argument.
More on this another time
if __name__ == '__main__':

No comments: