Category: Python

Python

Line oriented command interpreter in Python

The Cmd class provides a simple framework for writing line-oriented command interpreters. A Cmd instance or subclass instance is a line-oriented interpreter framework. There is no good reason to instantiate Cmd itself; rather, it’s useful as a superclass of an interpreter class you define yourself in order to inherit Cmd‘s methods and encapsulate action methods. …

C, C++, Python

Integrate python with C/C++ components

ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python. ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries. You load libraries by accessing …

Python

Banner Grabbing with Python

A simple banner grabber in Python: import optparse from socket import *   def banner(targetHost, targetPort): try: connsocket = socket(AF_INET, SOCK_STREAM) connsocket.connect((targetHost, targetPort)) connsocket.send(’Hi therern’) results = connsocket.recv(100) print ” + str(results) connsocket.close() except: print ‘error’ def main(): options = {} parser = optparse.OptionParser(’usage %prog -H <target host> -P <target port>’) parser.add_option(’-H’, ‘–host’, dest=’target_host’, type=’string’, …

Python

TCP Full Connect Scan with Python

A simple tcp full connect port scanner in Python: import optparse from socket import *   def connect(targetHost, targetPort): try: connsocket = socket(AF_INET, SOCK_STREAM) connsocket.connect((targetHost, targetPort)) connsocket.close() print ‘%d/tcp port openn’%targetPort except: print ‘%d/tcp port closedn’%targetPort   def scanner(targetHost, targetPorts): try: ip = gethostbyname(targetHost) except: print "unable to resolve ‘%s’: Unknown hostn"%targetHost return   try: …

Python

Obtain devices list with Python and pcapy

Install pcapy sudo apt-get install python-pcapysudo apt-get install python-pcapy Code import pcapy   devs = pcapy.findalldevs();   for dev in devs: print devimport pcapy devs = pcapy.findalldevs(); for dev in devs: print dev Run sudo python mysniffer.pysudo python mysniffer.py Output eth0 usbmon1 usbmon2 usbmon3 usbmon4 usbmon5 any loeth0 usbmon1 usbmon2 usbmon3 usbmon4 usbmon5 any lo …

Python

ARP scanning using python and scapy

Run the following scipt with administrative rights to discover all alive hosts on the network! #!/usr/bin/env python   import sys from scapy.all import *   if len(sys.argv) != 2: print "Usage: python arp-scan.py 192.168.1.0/24" sys.exit(1)   try: alive,dead=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=sys.argv[1]), timeout=2, verbose=0) print "MAC – IP" for i in range(0,len(alive)): print alive[i][1].hwsrc + " – " + …

Python

Traceroute system call in python

#!/usr/bin/env python   from subprocess import Popen, PIPE import sys   def tracer(host=None): p = Popen([’traceroute’, host], stdout=PIPE)   while True: try: line = p.stdout.readline() if not line: break print (line.rstrip()) except: break   def main(argv=None): tracer(sys.argv[1])   if __name__ == "__main__": sys.exit(main())#!/usr/bin/env python from subprocess import Popen, PIPE import sys def tracer(host=None): p = …

Python

How to mark directory on disk as a Python package

To mark a directory as a Python package directory you have to put a file named __init__.py in this directory. for example, myproject/libraries/__init__.py myproject/libraries/mymodule.py myproject/main.py you can import mymodule.py in main.py as: import libraries.mymodule or from libraries import mymodule The __init__.py file is usually empty.

Python

Simple python traceroute

Python traceroute with sockets #!/usr/bin/python import sys import socket   def traceroute(hostname, port, max_hops): destination = socket.gethostbyname(hostname) print "target %s" % hostname   icmp = socket.getprotobyname(’icmp’) udp = socket.getprotobyname(’udp’) ttl = 1   while True: recvsock = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) sendsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp) sendsock.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl) recvsock.bind(("", port)) sendsock.sendto("", (hostname, port)) currentaddr = …