#/usr/bin/python #coding:utf8 import MySQLdb,subprocess link = MySQLdb.Connection(host="192.168.100.202",user="asset",passwd="*******",db="Auto_Noc",charset="utf8") link.set_character_set("utf8") def getasset(): Asset_tuple = ("ifconfig eth0 |grep Bcast|awk '{printf $2}'|awk -F: '{printf $2}'",'hostname',"ethtool -i eth0|sed -n '1p'","ethtool -i eth0|sed -n '2p'","cat /proc/cpuinfo |grep model|sed -n '2p'|awk -F: '{printf $2}'","free |sed -n '2p'|awk '{printf $2}'","fdisk -l|grep Disk|head -n 1|awk '{printf $3}'") #default value application_name ="No Add" Asset_number=0 room = "MD37" cabinets="G01" parent_device="Is Null" pro_info="Is Null" person_in_charge="Cwtea" host_name ="Server1" network_interface="eth1" cpu_info="inter" mem_info="4G" network_info="Test" hard_disk_info= "100G" create_time = "2013-08-1 12:03:20" ip_addr = (subprocess.Popen(((Asset_tuple[0])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") host_name = (subprocess.Popen(((Asset_tuple[1])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") network_info1 = (subprocess.Popen(((Asset_tuple[2])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") network_info2 = (subprocess.Popen(((Asset_tuple[3])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") network_info = network_info1+ " " +network_info2 cpu_info = (subprocess.Popen(((Asset_tuple[4])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") mem_info = (subprocess.Popen(((Asset_tuple[5])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") hard_disk_info = (subprocess.Popen(((Asset_tuple[6])),shell=True,stdout=subprocess.PIPE)).stdout.read().replace("\n","") #Insert Database cursor = link.cursor() #ip_addr,host_name,network_interface,cpu_info,mem_info,network_info,hard_disk_info,create_time sql = "INSERT INTO Asset_management(Asset_number,application_name,ip_addr,room,cabinets,host_name,parent_device,network_interface,cpu_info,mem_info,network_info,hard_disk_info,pro_info,person_in_charge,create_time) values ('%d','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (Asset_number,application_name,ip_addr,room,cabinets,host_name,parent_device,network_interface,cpu_info,mem_info,network_info,hard_disk_info,pro_info,person_in_charge,create_time) cursor.execute(sql) link.commit() print "Work is Ok" print network_info