Search This Blog

Tuesday, March 14, 2017

Print files recursively in python

import os

""" Print files recursively in a tree"""
path_dir = raw_input("Enter the directory path: ")
for root, subdirs, files in os.walk(path_dir):
    level=" "*len(root.split("\\"))
    print "%s|_ %s" %(level,root)
    if subdirs:
        pass
    if files:
        for file in files:
            print "%s|___ %s" %(level, file)
    else:
        pass



Output:

C:\Program Files (x86)>C:\Python27\python.exe D:/Deepti/Work/Python/practice/recursive_dir.py
Enter the directory path: D:\Deepti\Work\Python\practice\Dir
      |_ D:\Deepti\Work\Python\practice\Dir
       |_ D:\Deepti\Work\Python\practice\Dir\dir1
       |___ text11.txt
       |___ text12.txt
        |_ D:\Deepti\Work\Python\practice\Dir\dir1\dir11
        |___ text111.txt
        |___ text112.txt
        |_ D:\Deepti\Work\Python\practice\Dir\dir1\dir12
        |___ fiel122.txt

Monday, October 5, 2015

Python Signals and IPC

What does this script do?

Creates a tcp server which listens on a port. Implements signals to ensure it automatically shutsdown after a pre-configured duration which is given through command line

Python version: 2.6

[root@host-172-17-95-7 module2]# cat signals_exercise.py
import signal, sys
from socket import *
from optparse import OptionParser

HOST = ""
PORT = 37845
ADDR = (HOST,PORT)
BUFSIZE = 4096

# Create a raw socket
serv = socket(AF_INET, SOCK_STREAM)

def option_parser():
 parser = OptionParser()
 parser.add_option('-t','--time',help="Time for server to server",action='store',type=int,\
 dest='time_to_serv')
 (options, args) = parser.parse_args()
 return options,args


def signal_handler(signum,frm):
 print "Got signal: %s" %signum
 print "Shutting down the server"
 serv.shutdown(SHUT_WR)
 print "Closing the server"
 serv.close()
 sys.exit()

def bind_server():
 print "Binding tcpServer"
 serv.bind((ADDR))
 serv.listen(100) # Listen time is greater than time_to_serv

def trigger_alarm(time_to_serv):
 print "Triggering signal"
 signal.signal(signal.SIGALRM,signal_handler)
 signal.alarm(time_to_serv)
 print "Done!"

if __name__ == '__main__':
 options, args = option_parser()
 bind_server()
 trigger_alarm(options.time_to_serv)

 while True:
 pass
[root@host-172-17-95-7 module2]# python signals_exercise.py -t 5
Binding tcpServer
Triggering signal
Done!
Got signal: 14
Shutting down the server
Closing the server
[root@host-172-17-95-7 module2]#
[root@host-172-17-95-7 module2]# netstat -nap | grep 37845
[root@host-172-17-95-7 module2]# netstat -nap | grep python

Monday, September 28, 2015

Threading and Queues

from scapy.all import *
import threading
import Queue
import time

class WorkerThread(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        print "In worker thread"
        while True:
            counter = self.queue.get()
            print "Ordered to sleep for %d seconds" %counter
            time.sleep(counter)
            print "Completed the job. Slept for %d seconds" %counter
            self.queue.task_done()

queue = Queue.Queue()

for i in range(10):
    print "creating worker thread %d" %i
    worker = WorkerThread(queue)
    worker.setDaemon(True)
    worker.start()
    print "Worker thread %d created" %i


for j in range(10):
    queue.put(j)

queue.join()

print "All Tasks Over!"


OUTPUT:
======
/usr/bin/python /home/dvaidyula/PycharmProjects/spse/scapy-2.3.1/tcp_connect_scan.py
WARNING: No route found for IPv6 destination :: (no default route?)
creating worker thread 0
In worker thread
Worker thread 0 created
creating worker thread 1
In worker threadWorker thread 1 created

 creating worker thread 2
In worker threadWorker thread 2 created
creating worker thread 3

In worker threadWorker thread 3 created

creating worker thread 4
In worker thread
 Worker thread 4 created
creating worker thread 5
In worker threadWorker thread 5 created
creating worker thread 6
Worker thread 6 created
creating worker thread 7
In worker thread

In worker thread
 Worker thread 7 created
creating worker thread 8
In worker threadWorker thread 8 created
creating worker thread 9

In worker threadWorker thread 9 created

Ordered to sleep for 0 seconds
 Ordered to sleep for 1 seconds
Ordered to sleep for 2 seconds
Completed the job. Slept for 0 seconds
Ordered to sleep for 3 seconds
Ordered to sleep for 4 seconds
Ordered to sleep for 5 seconds
 Ordered to sleep for 7 secondsOrdered to sleep for 6 seconds

Ordered to sleep for 8 seconds
Ordered to sleep for 9 seconds
Completed the job. Slept for 1 seconds
Completed the job. Slept for 2 seconds
Completed the job. Slept for 3 seconds
Completed the job. Slept for 4 seconds
Completed the job. Slept for 5 seconds
Completed the job. Slept for 6 seconds
Completed the job. Slept for 7 seconds
Completed the job. Slept for 8 seconds
Completed the job. Slept for 9 seconds
All Tasks Over!

Process finished with exit code 0

Python threads

import thread, time

def worker_thread(id):
    print "Thread ID %d is now alive" %id
    count = 1
    while True:
        print "Thread with ID %d has counter value %d" %(id, count)
        time.sleep(2)
        count += 1


for i in range(5):
    thread.start_new_thread(worker_thread, (i,))


print "Main Thread Going For An Infinite Loop"
while True:
    pass
       

OUTPUT in windows:
===================

Main Thread Going For An Infinite LoopThread ID 4 is now aliveThread ID 2 is now aliveThread ID 0 is now aliveThread ID 3 is now aliveThread ID 1 is now alive

Thread with ID 4 has counter value 1Thread with ID 2 has counter value 1Thread with ID 0 has counter value 1Thread with ID 3 has counter value 1Thread with ID 1 has counter value 1

Thread with ID 3 has counter value 2Thread with ID 2 has counter value 2Thread with ID 4 has counter value 2Thread with ID 0 has counter value 2Thread with ID 1 has counter value 2

Thread with ID 3 has counter value 3Thread with ID 0 has counter value 3Thread with ID 2 has counter value 3Thread with ID 4 has counter value 3Thread with ID 1 has counter value 3

Thread with ID 0 has counter value 4Thread with ID 3 has counter value 4Thread with ID 2 has counter value 4Thread with ID 4 has counter value 4Thread with ID 1 has counter value 4

Scapy and Python

http://www.secdev.org/projects/scapy/doc/usage.html

Scapy is a Python interpreter that enables you to create, forge, or decode packets on the network, to capture packets and analyze them, to dissect the packets, etc. It also allows you to inject packets into the network. It supports a wide number of network protocols and it can handle and manipulate wireless communication packets.

Scapy can be used to perform the jobs done by many network tools, such as nmap, hping, arpscan, and tshark (the command line of wireshark).

The concept behind Scapy is that it is cable of sending and receiving packets and it can sniff packets. The packets to be sent can be created easily using the built-in options and the received packets can be dissected. Sniffing of packets helps in understanding what communication is taking place on the network.

Building a packet:

[root@deeptiWorkSpace scapy-2.3.1]# ./run_scapy
INFO: Can't import python gnuplot wrapper . Won't be able to plot.
INFO: Can't import PyX. Won't be able to use psdump() or pdfdump().
WARNING: No route found for IPv6 destination :: (no default route?)
Welcome to Scapy (2.3.1)
>>> a=IP(ttl=10)
>>> a
<IP  ttl=10 |>
>>> a.src
'127.0.0.1'
>>> a.dst="10.10.12.39"
>>> a
<IP  ttl=10 dst=10.10.12.39 |>
>>> a.src
'10.10.16.199'
>>> a.ttl
10
>>> del(a.ttl)
>>> a
<IP  dst=10.10.12.39 |>
>>> a.ttl
64

Stacking layers
The / operator has been used as a composition operator between two layers. When doing so, the lower layer can have one or more of its defaults fields overloaded according to the upper layer. (You still can give the value you want). A string can be used as a raw layer.

>>> IP()
<IP  |>
>>> IP()/TCP()
<IP  frag=0 proto=tcp |<TCP  |>>
>>> Ether()/IP()/TCP()
<Ether  type=IPv4 |<IP  frag=0 proto=tcp |<TCP  |>>>
>>> IP()/TCP()/"GET / HTTP/1.0\r\n\r\n"
<IP  frag=0 proto=tcp |<TCP  |<Raw  load='GET / HTTP/1.0\r\n\r\n' |>>>
>>> Ether()/IP()/IP()/UDP()
<Ether  type=IPv4 |<IP  frag=0 proto=ipencap |<IP  frag=0 proto=udp |<UDP  |>>>>
>>> IP(proto=55)/TCP()
<IP  frag=0 proto=mobile |<TCP  |>>

Each packet can be build or dissected (note: in Python _ (underscore) is the latest result):
>>> str(IP())
'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01'
>>> IP(_)
<IP  version=4L ihl=5L tos=0x0 len=20 id=1 flags= frag=0L ttl=64 proto=ip chksum=0x7ce7 src=127.0.0.1 dst=127.0.0.1 |>

>>> a=Ether()/IP(dst="www.slashdot.org")/TCP()/"GET /index.html HTTP/1.0 \n\n"
>>> print a
▒g寫l▒B▒K▒EC@Ґ

▒▒"▒0PP ▒GET /index.html HTTP/1.0

>>> hexdump(a)
0000   D0 67 E5 AF AB 6C 9E 42  EA 4B FC 5F 08 00 45 00   .g...l.B.K._..E.
0010   00 43 00 01 00 00 40 06  D2 90 0A 0A 10 C7 D8 22   .C....@........"
0020   B5 30 00 14 00 50 00 00  00 00 00 00 00 00 50 02   .0...P........P.
0030   20 00 15 8E 00 00 47 45  54 20 2F 69 6E 64 65 78    .....GET /index
0040   2E 68 74 6D 6C 20 48 54  54 50 2F 31 2E 30 20 0A   .html HTTP/1.0 .
0050   0A                                                 .
>>>


>>> Ether(str(a))
<Ether  dst=d0:67:e5:af:ab:6c src=9e:42:ea:4b:fc:5f type=IPv4 |<IP  version=4L ihl=5L tos=0x0 len=67 id=1 flags= frag=0L ttl=64 proto=tcp chksum=0xd290 src=10.10.16.199 dst=216.34.181.48 options=[] |<TCP  sport=ftp_data dport=http seq=0 ack=0 dataofs=5L reserved=0L flags=S window=8192 chksum=0x158e urgptr=0 options=[] |<Raw  load='GET /index.html HTTP/1.0 \n\n' |>>>>
>>> Ether(str(a))
<Ether  dst=d0:67:e5:af:ab:6c src=9e:42:ea:4b:fc:5f type=IPv4 |<IP  version=4L ihl=5L tos=0x0 len=67 id=1 flags= frag=0L ttl=64 proto=tcp chksum=0xd290 src=10.10.16.199 dst=216.34.181.48 options=[] |<TCP  sport=ftp_data dport=http seq=0 ack=0 dataofs=5L reserved=0L flags=S window=8192 chksum=0x158e urgptr=0 options=[] |<Raw  load='GET /index.html HTTP/1.0 \n\n' |>>>>
>>> Ether(str(a)).hide_defaults()  # Hides all the default values
>>>

Reading PCAP files
==================

You can read packets from a pcap file and write them to a pcap file.

>>> a=rdpcap("/spare/captures/isakmp.cap")
>>> a
<isakmp.cap: UDP:721 TCP:0 ICMP:0 Other:0>

Sending packets
Now that we know how to manipulate packets. Let’s see how to send them. The send() function will send packets at layer 3. That is to say it will handle routing and layer 2 for you. The sendp() function will work at layer 2. It’s up to you to choose the right interface and the right link layer protocol.
>>> send(IP(dst="10.10.16.198")/ICMP())
.
Sent 1 packets.
>>> sendp(Ether()/IP(dst="10.10.16.198",ttl=(1,4)), iface="eth1")
....
Sent 4 packets.

***********************
===== TCP REPLAY =====
***********************
>>> sendp(rdpcap("/tmp/pcapfile")) # tcpreplay
...........
Sent 11 packets.


>>> sendp(rdpcap("/home/dvaidyula/udp_du_bb1_1500.pcap"))
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Sent 1501 packets.


Process Creation

import os

def child_process () :
    print "I am the child process and my PID is: %d" %os.getpid()
    print "The child is exiting"


def parent_process():
    print "I am the parent process and my PID is: %d" %os.getpid()
    print "Calling child process"
    childpid = os.fork()

    if childpid == 0:
        #We are inside child
        child_process()
    else:
        #We are inside parent process
        print "We are inside parent process"
        print "The child pid i %d" %childpid

    while True:
        pass

    print "I am still in parent and my PID is: %d" %os.getpid()

parent_process()
   

fork Demo - Python

[dvaidyula@qa-test temp]$ cat fork_demo.py
import os

def parent_process():
    print "I am the parent process with pid %s" %os.getpid()
    childId = os.fork()
    print "I am in the parent loop still and pid is %s" %os.getpid()
    print "ChildID is %s" %childId

    if childId ==  0 :
        child_process()
    else:
        print "We are inside child process and pid is %s" %childId

def child_process():
    print "I am the child process and pid is %s" %os.getpid()
    print "The child is exiting"

parent_process()
[dvaidyula@qa-test temp]$
[dvaidyula@qa-test temp]$ py fork_demo.py
I am the parent process with pid 5317
I am in the parent loop still and pid is 5318
ChildID is 0
I am the child process and pid is 5318
The child is exiting
I am in the parent loop still and pid is 5317
ChildID is 5318
We are inside child process and pid is 5318

### EXECVP ####
[dvaidyula@qa-test temp]$ python
Python 2.6.6 (r266:84292, Jan  2 2013, 10:42:24)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import os
>>> os.execvp("ping",["ping","127.0.0.1"])
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.007 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.005 ms

--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.005/0.011/0.023/0.009 ms

[dvaidyula@qa-test temp]$  # Note that once we kill, it is exiting the python prompt / parent process