Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32167
  • 博文数量: 21
  • 博客积分: 707
  • 博客等级: 上士
  • 技术积分: 237
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-15 12:48
文章分类

全部博文(21)

文章存档

2011年(21)

分类:

2011-01-13 17:36:02

写一个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) |
给主人留下些什么吧!~~