• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python执行shell获取硬件参数写入mysql的方法

python执行shell获取硬件参数写入mysql的方法

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python执行shell命令,python 调用shell,python 执行shell,python shell,python shell是什么等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python执行shell获取硬件参数写入mysql的方法。分享给大家供大家参考。具体分析如下:

最近要获取服务器各种参数,包括cpu、内存、磁盘、型号等信息。试用了Hyperic HQ、Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy。

于是乎想到用python执行shell获取这些信息,python执行shell脚本有以下三种方法:

1. os.system()
#返回结果0或者1,不能得到命令的输出</div>
2. os.popen()
print output.read()
#打印出的是命令输出,但是得不到执行的返回值</div>
3. commands.getstatusoutput()
print status, output
#打印出返回值和命令输出</div>
可以根据需要选取其中一种方法,以下是python执行shell获取硬件参数写入mysql,并定期更新的程序:

'''
Created on Dec 10, 2014

@author: liufei
'''
#coding=utf-8
import time, sched, os, string
from datetime import datetime
import MySQLdb
 
s = sched.scheduler(time.time,time.sleep)

def event_func():
    try:
        #主机名
        name = os.popen(""" hostname """).read()
        #cpu数目
        cpu_num = os.popen(""" cat /proc/cpuinfo | grep processor | wc -l """).read()
        #内存大小
        mem = os.popen(""" free | grep Mem | awk '{print $2}' """).read()
        #机器品牌
        brand = os.popen(""" dmidecode | grep 'Vendor' | head -1 | awk -F: '{print $2}' """).read()
        #型号
        model = os.popen(""" dmidecode | grep 'Product Name' | head -1 | awk -F: '{print $2}' """).read()
        #磁盘大小
        storage = os.popen(""" fdisk -l | grep 'Disk /dev/sd' | awk 'BEGIN{sum=0}{sum=sum+$3}END{print sum}' """).read()
        #mac地址
        mac = os.popen(""" ifconfig -a | grep HWaddr | head -1 | awk '{print $5}' """).read()
       
        name = name.replace("\n","").lstrip()
        cpu_num =  cpu_num.replace("\n","").lstrip()
        memory_gb = round(string.atof(mem.replace("\n","").lstrip())/1000.0/1000.0, 1)
        brand = brand.replace("\n","").lstrip()
        model = model.replace("\n","").lstrip()
        storage_gb = storage.replace("\n","").lstrip()
        mac = mac.replace("\n","").lstrip()
       
        print name
        print cpu_num
        print memory_gb
        print storage_gb
        print brand
        print model
        print mac
   
        conn=MySQLdb.connect(host='xx.xx.xx.xx',user='USERNAME',passwd='PASSWORD',db='DBNAME',port=3306)
        cur=conn.cursor()
        cur.execute('select mac from servers where mac=%s',mac)
        data = cur.fetchone()

        if data is None:
            value = [name, brand, model, memory_gb, storage_gb, cpu_num, mac, datetime.now(), datetime.now()]
            cur.execute("insert into servers(name, brand, model, memory_gb, storage_gb, cpu_num, mac,  created_at, updated_at) values(%s, %s, %s, %s, %s, %s, %s, %s, %s)",value)           
        else:
            value1 = [name, brand, model, memory_gb, storage_gb, cpu_num, datetime.now(), mac]
            cur.execute("update servers set name=%s,brand=%s,model=%s,memory_gb=%s,storage_gb=%s,cpu_num=%s, updated_at=%s where mac=%s",value1)
          
        conn.commit()
        cur.close()
        conn.close()
       
    except MySQLdb.Error,e:
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
   
def perform(inc):
    s.enter(inc,0,perform,(inc,))
    event_func()
   
def mymain(inc=10):
    s.enter(0,0,perform,(inc,))
    s.run()
 
if __name__ == "__main__":
    mymain()
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python中执行shell的两种方法总结
  • python中执行shell的两种方法总结
  • 日常整理python执行系统命令的常见方法(全)
  • 日常整理python执行系统命令的常见方法(全)
  • python执行shell获取硬件参数写入mysql的方法
  • python中执行shell命令的几个方法小结

相关文章

  • Python中的引用和拷贝浅析
  • 让python 3支持mysqldb的解决方法
  • Python重新引入被覆盖的自带function
  • 在Python中使用正则表达式的方法
  • Python中处理unchecked未捕获异常实例
  • python 判断一个进程是否存在
  • Python字符串特性及常用字符串方法的简单笔记
  • 在Python中使用lambda高效操作列表的教程
  • Python的collections模块中namedtuple结构使用示例
  • 介绍Python中的__future__模块

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python 实现一个颜色色值转换的小工具
    • python中__call__方法示例分析
    • 在Gnumeric下使用Python脚本操作表格的教程
    • 复制粘贴功能的Python程序
    • Python3基础之输入和输出实例分析
    • python在Windows8下获取本机ip地址的方法
    • python文件读写并使用mysql批量插入示例分享(python操作mysql)
    • python pdb调试方法分享
    • python Django批量导入不重复数据
    • Python程序设计入门(2)变量类型简介

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有