【发布时间】:2017-08-20 04:03:23
【问题描述】:
我正在开发一个 ping LAN 设备并将 ping 所用的 avg_time 写入 MySQL 数据库的项目。
# Python script to update contents of MySQL database
import MySQLdb
import subprocess
import re
#Connecting to MySQL database
db = MySQLdb.connect("localhost","tempLocal","thunderbolt","dbavgspeed")
#initializing cursor
cur = db.cursor()
error = "Request"
while 1:
# 0.1 is the interval between pings and 4 is the number of times ping operation is performed and -t 1 sets the timeout to 1 sec
command1 = ['ping','-n','-i','0.1','-t','1','-c','400','192.168.1.1']
p1 = subprocess.Popen(command1,stdout=subprocess.PIPE)
#text is the string which stores the output from the terminal
text1 = p1.stdout.read()
if error in text1:
status1 = 0
cur.execute("""UPDATE tbavgspeed SET status = %s WHERE 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 WHERE id = %s""",(avgTime1,status1,1))
db.commit()
#terminates the connection
db.close()
- 脚本使用简单的 ping 命令运行良好,但如何通过需要 sudo 的 python 脚本使用 Flood ping (ping -f)
- 有没有更好的方法来计算路由器的当前数据传输率
【问题讨论】:
-
尝试在超级用户模式下运行你的python脚本:
sudo python ...