手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python 获取Linux本机信息

[日期:2012-12-12] 来源:Linux社区  作者:song49 [字体:大 中 小]

用Python写的获取linux本机信息,包括kernel、IP、Memory、Disk信息。

#####################################################

#Information on this program is used to get the Linux native.                  #

#You can enter the "kernel", "ip", "memory", "disk" keyword to get the results,#

#you can also enter "all".                                                    #

######################################################

翻译:本程序是用来获取linux本机信息的

你可以输入“kernel”,“ip”,“memory”,“disk”关键字获取响应的参数信息

也可以输入“all”,查看所有参数。

程序内容如下:

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. #2012/12/12 by SongShouJiong
  4. #Email:linuxsong49@163.com
  5. import os
  6. kernel_version = os.popen('''/bin/uname -a''').read().strip('\n')
  7. ip = os.popen('''/sbin/ifconfig | grep 'inet addr'|awk '{print $2}'|head -1 |cut -d ":" -f 2''').read().strip('\n')
  8. memory = os.popen('''free -m | head -2''').read().strip('\n')
  9. disk = os.popen('''df -hT''').read().strip('\n')
  10. print '''
  11. ################################################################################
  12. #Information on this program is used to get the Linux native. #
  13. #You can enter the "kernel", "IP", "memory", "disk" keyword to get the results,#
  14. #you can also enter "all". #
  15. ###############################################################################'''.strip('\n')
  16. a = str(raw_input('Please input to query parameter:'))
  17. if a == 'kernel':
  18. print "Kernel Version:",kernel_version
  19. elif a == 'ip':
  20. print "Local IP:",ip
  21. elif a == 'memory':
  22. print "Local Memory:" + ('\n') + memory
  23. elif a == 'disk':
  24. print "Local Disk:" + ('\n') + disk
  25. elif a == "all":
  26. print "Kernel Version:",kernel_version
  27. print "Local IP:",ip
  28. print "Local Memory:" + ('\n') + memory
  29. print "Local Disk:" + ('\n') + disk
  30. else:
  31. print "Didnt't you want to query parameter."

最近在学习python,所以就各种找需求去练习,写的也简单,各种堆命令,有什么不对的地方或者好的建议,还请指出。

linux
相关资讯       python 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款