Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, November 2, 2010

Python - Writing a traceroute using sockets

I recently learned, how to write a traceroute using socket programming in python. I am sharing it below. I don't take any credit for the code. I may have picked bits and pieces from various different resources on the Internet, as a part of my learning process. It's not very elegant but demonstrates the purpose. Perhaps, some day I can improve it and add few enhancements to it.

#!/usr/bin/python
import sys
import socket

def traceroute(dest_name):
    dest_addr = socket.gethostbyname(dest_name)
    port = 33434
    max_hops = 30
    print dest_name
    icmp = socket.getprotobyname('icmp')
    udp = socket.getprotobyname('udp')
    ttl = 1
    while True:
        print ttl,
        recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
        send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
        recv_socket.bind(("", port))
        send_socket.sendto("", (dest_name, port))
        curr_addr = None
        curr_name = None
        try:
            _, curr_addr = recv_socket.recvfrom(512)
            curr_addr = curr_addr[0]

            try:
                curr_name = socket.gethostbyaddr(curr_addr)[0]
            except socket.error:
                curr_name = curr_addr
        except socket.error:
            pass
        finally:
            send_socket.close()
            recv_socket.close()

        if curr_addr is not None:
            curr_host = "%s (%s)" % (curr_name, curr_addr)
        else:
            curr_host = "*"
        print "%d\t%s" % (ttl, curr_host)

        ttl += 1
        if curr_addr == dest_addr or ttl > max_hops:
            break

if __name__ == "__main__":
    traceroute(sys.argv[1])


Can be executed as -

>sudo ./traceroute.py xharpreetx.com

Friday, July 30, 2010

Compiling Ganglia - Errors and problems

Some errors and problems:


My install directory was /opt/ganglia.


Error -
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

Solution -
Add gcc bin path to PATH environment variable



Error -
checking for pcre.h... no
checking for pcre_compile in -lpcre... no
libpcre not found, specify --with-libpcre=no to build without PCRE support

Solution -
PCRE is not installed.
# wget ftp://ftp.sunfreeware.com/pub/freeware/SOURCES/pcre-8.10.tar.gz
# /usr/local/bin/tar -xzf pcre-8.10.tar.gz
# cd pcre-8.10
# ./configure --prefix=/opt/ganglia
# make && make install

Make sure you add, -I/opt/ganglia/include to CFLAGS environment variable
and -L/opt/ganglia/lib to LDFLAGS after this.



Problem -
Graphs not shown in ganglia or images not shown on ganglia front end.


Reason -
php-gd may not have been installed.
You can do so by ( as root) -
# yum install php-gd
Another reason could be front-end can't find RRD path. Make sure it is set in conf.php


Error -
checking for apr-1-config... no
configure: error: apr-1-config binary not found in path


Reason -
APR is not installed or path of apr-1-config is not available through the PATH environment variable.


Error -
libgcc_s.so.1: open failed: No such file or directory


Solution-
Execute the following:
# ldd gmond
...
libgcc_s.so.1 => (file not found)
...


So gmond can link to libgcc


Add the following to you LDFLAGS
# setenv LDFLAGS $LDFLAGS:-R/yourgccpath/gcc-4.0.3/lib


-R records the runtime search path.


Install again
# ./configure --prefix=/opt/ganglia --sysconfdir=/opt/ganglia/etc
# make && make install


Error -
After installation, while executing gmond
apr_pollset_create failed: Invalid argument

Solution -
In your config file gmond.conf, if there is no udp_recv_channel or tcp_accept_channel
defined, gmond fails to run with this error.
Set "deaf = yes " under "globals"

Error -
/usr/bin/ld: cannot find -lpython2.3
collect2: ld returned 1 exit status
make[4]: *** [modpython.la] Error 1

Solution -
I dont remember exactly how i resolved it.
Most likely it was the linker not being able to find python.
Probably by setting library flags this error was resolved.

Error -
false cru libgetopthelper.a getopt1.o getopt.o getopt_init.o
make[2]: *** [libgetopthelper.a] Error 1

Solution -
Add /usr/ccs/bin to PATH environment variable.

In case of dependency not found errors, you could configure options, like --with-libapr, --with-libconfuse to point to dependency paths.

Error -
Some gm_protocol_xdr.c related warnings and finally build fails with this error

ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status

Solution -
Uninstall the dependency 'confuse' and install it by setting CFLAGS="-O3 -fPIC"
> cd ../confuse-2.7/
> make uninstall
> make clean
> env CFLAGS="-O3 -fPIC" ./configure --prefix=/opt/ganglia
> make && make install

Do a configure, make and make install and you should be good.
 

Tuesday, June 16, 2009

Asp.Net and Python-cgi communication

Technorati Tags: ,,

I recently had my first shot at writing cgi scripts in python. It was an altogether different experience having come from a .Net and Turbogears background. I also got my hands on a Linux server. Infact, it was the first time I got to use vi, ls, cat, cp etc, since college. It is really amazing how much you can do with just the shell commands on Linux.


The Linux machine had an apache server which ran some cgi scripts to do some basic tasks like creating a new user record in the database, deleting, editing etc. I think that was the reason they did not go beyond cgi. These tasks were manually done by an admin after logging into the application. Now, these scripts were required to be made callable from an asp.net web application.


ASP.NET application <===========> Python cgi scripts
CreateDemo.aspx _____________ create_demo.cgi
(collects user info
for demo record) _____________ (requires authentication)

So, we decided to create another script, let us say 'authenticate.cgi', in which we used urllib python library to by pass the authentication on create_demo.cgi and then CreateDemo.aspx would call authenticate.cgi. Here is the new flow-

CreateDemo.aspx --> authenticate.cgi --> create_demo.cgi

This write-up addresses two key areas:
1. How to communicate with a python cgi script from an asp.net web page?

//Request url and request method

HttpWebRequest httpRequest = HttpWebRequest)WebRequest.Create("http://192.1.1.1/no_auth/authenticate.cgi?name=harpreet”);

httpRequest.KeepAlive = false;

httpRequest.Method = "GET";

//Read the web response from the URI

string strResponse = string.Empty;

using (WebResponse webResponse = (WebResponse)httpRequest.GetResponse())

{

System.Text.Encoding enc = System.Text.Encoding.GetEncoding(1252);

using (StreamReader srResponseStream = new StreamReader(webResponse.GetResponseStream(), enc))

{

//Read the response into a string or you could use XML also

strResponse = srResponseStream.ReadToEnd();

}

}

2. How to by pass basic authentication on cgi script using urllib?

#!/usr/bin/env /usr/local/bin/python

import urllib2,urllib

import sys,os,cgi,cgitb

import re,datetime

import base64

from urlparse import urlparse

def handler():

form = cgi.FieldStorage()

if form.list:

data = {}

for field in flds:

data[field]=form.getvalue(field,'')

theurl = 'http://localhost/admin/create_demo.cgi’

# you'll need to supply a protected page with your username and password

username = 'scott'

password = 'tiger' # a very bad password

req = urllib2.Request(theurl)

try:

handle = urllib2.urlopen(req, urllib.urlencode(data))

except IOError, e:

# do whatever you want to handle the exception here, 'pass' in this example

pass

else:

# If we don't fail then the page isn't protected

print "This page is not protected by any authentication."

sys.exit(1)

if not hasattr(e, 'code') or e.code != 401:

# we got an error - but not a 401 error

print "This page is not protected by any authentication."

print 'Some other reason for failure'

sys.exit(1)

authheader = e.headers['www-authenticate']

# this gets the www-authenticate line from the headers

# which would contain the authentication scheme and realm

authobj = re.compile(r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"]([^'"]+)['"]''',re.IGNORECASE)

# this RE is used to extract scheme and realm

matchobj = authobj.match(authheader)

# if the authheader isn't matched by the regular expression

# then there is probably some error in the header

if not matchobj:

print 'The authentication header is badly formed.'

print authheader

sys.exit(1)

scheme = matchobj.group(1)

realm = matchobj.group(2)

# check schem (this example works for basic authorization only

if scheme.lower() != 'basic':

print 'This example only works for BASIC Authorization.'

sys.exit(1)

base64string = base64.encodestring('%s:%s' % (username, password))[:-1]

authheader = "Basic %s" % base64string

req.add_header("Authorization", authheader)

try:

handle = urllib2.urlopen(req,urllib.urlencode(data))

except IOError, e:

print "The username or password is not valid."

sys.exit(1)

thepage = handle.read()

print "Content-type: text/html; charset=utf-8"

print

print thepage

handler()

Security was not really a major concern for this cgi, but an IP based security was added to limit unwanted access.

* The code snippets are not really tested but, will hopefully work :-)