写一个python脚本,用来充mysql数据vkuzhong查询主机,并且查看主机是否存货,并且更新数据记录。
参考:
python mysql:
#!/usr/bin/env python
from threading import Thread
import subprocess
from Queue import Queue
import re
import MySQLdb
sum=0
#ips=["iquadcore-01","amd64","aaaaa","xue"]
def Sql(count):
try:
connection=MySQLdb.connect(host="172.20.33.19",user="root",passwd="reqiq iu",db="hostlist")
except:
print "Could not connect to MySQL server."
try:
cursor= connection.cursor()
cursor.execute("select hostname from servers")
print "Rows selected: ", cursor.rowcount
for row in cursor.fetchall():
print row[0]
count=count+1
print "Thread %s : Pinging %s" % (count,row[0])
ret= subprocess.call("ping -c 1 %s" %(row[0]),
shell=True,
stdout=open('/dev/null','w'),
stderr=subprocess.STDOUT)
if ret==0:
print "%s: is alive" %(row[0])
sql="update servers set status='OnLine' where hostname=%s"
param=(row[0])
cursor.execute(sql,param)
#place valid ip address in next queue
else:
print "%s: did not respond" %(row[0])
cursor.close()
except:
print "Could not selete to MySQL server."
sql_queue=Sql(sum)
阅读(276) | 评论(0) | 转发(0) |