ping - Using sudo in python script -
i working on project pings lan devices , writes avg_time taken ping in mysql db.
# python script update contents of mysql database import mysqldb import subprocess import re #connecting mysql database db = mysqldb.connect("localhost","templocal","thunderbolt","dbavgspeed") #initializing cursor cur = db.cursor() error = "request" while 1: # 0.1 interval between pings , 4 number of times ping operation performed , -t 1 sets timeout 1 sec command1 = ['ping','-n','-i','0.1','-t','1','-c','400','192.168.1.1'] p1 = subprocess.popen(command1,stdout=subprocess.pipe) #text string stores output terminal text1 = p1.stdout.read() if error in text1: status1 = 0 cur.execute("""update tbavgspeed set status = %s id = %s""",(status1,1)) db.commit() else: status1 = 1 find11 = re.search("stddev(.+?)ms", text1) allvalues1 = find11.group(1) find12 = re.search("/(.+?)/", allvalues1) avgtime1 = find12.group(1) cur.execute("""update tbavgspeed set avg_time = %s, status = %s id = %s""",(avgtime1,status1,1)) db.commit() #terminates connection db.close()
- the script working fine simple ping command, how use flood ping (ping -f) through python script requires sudo
- is there better way calculate current data transfer rate router
you run python script sudo
. example:
import os os.system('ping -f google.com')
without sudo
:
$ python >>> import os >>> os.system('ping -f google.com') ping: -f flag: operation not permitted 19712
with sudo
:
$ sudo python >>> import os >>> os.system('ping -f google.com') ping google.com (172.217.6.78): 56 data bytes .^c --- google.com ping statistics --- 409 packets transmitted, 408 packets received, 0.2% packet loss round-trip min/avg/max/stddev = 3.334/3.645/9.459/0.362 ms 0
Comments
Post a Comment