Eureka Moment Wiki

One eureka moment at the time

User Tools

Site Tools


development:python:os_thread

Python: Thread, for loop, script arguments, regex and OS execute

For loop 255 times, create a thread, execute a function and pass a system argument, execuring a system command and printing its output

#!/usr/bin/python
 
import threading
import time
import os
import sys 
import re
 
def ppp(addr):
    response = os.system('ping -c 1 ' + str(addr) + ' | grep from | awk \'{print "ip=" substr($4,1,length($4)-1),$7}\'')
 
if len(sys.argv) == 2:
    if str(sys.argv[1]).endswith('.'):
        ipbase=str(sys.argv[1])
    else:
        ipbase=str(sys.argv[1]) + '.'
 
    pattern = re.compile("^(([0-9]{1,2})|([0-1]{1}[0-9]{2})|([2]{1}[0-4]{1}[0-9]{1})|([2]{1}[5]{1}[0-5]{1}))\.(([0-9]{1,2})|([0-1]{1}[0-9]{2})|([2]{1}[0-4]{1}[0-9]{1})|([2]{1}[5]{1}[0-5]{1}))\.(([0-9]{1,2})|([0-1]{1}[0-9]{2})|([2]{1}[0-4]{1}[0-9]{1})|([2]{1}[5]{1}[0-5]{1}))\.$")
    if pattern.match(ipbase):
        for addrs in range(1,255):
            x = threading.Thread(target=ppp, args=(ipbase + str(addrs), ))
            x.start()
    else:
        print('./ips 192.168.0.')
else:
    print('./ips 192.168.0.')

Discussion

Enter your comment:
H J I M E
 
development/python/os_thread.txt · Last modified: 2019/07/12 10:07 by tplecko