Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python 获取Linux本机信息

用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(" ")
  7. ip = os.popen("""/sbin/ifconfig | grep "inet addr"|awk "{print $2}"|head -1 |cut -d ":" -f 2""").read().strip(" ")
  8. memory = os.popen("""free -m | head -2""").read().strip(" ")
  9. disk = os.popen("""df -hT""").read().strip(" ")
  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(" ")
  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:" + (" ") + memory
  23. elif a == "disk":
  24. print "Local Disk:" + (" ") + disk
  25. elif a == "all":
  26. print "Kernel Version:",kernel_version
  27. print "Local IP:",ip
  28. print "Local Memory:" + (" ") + memory
  29. print "Local Disk:" + (" ") + disk
  30. else:
  31. print "Didnt"t you want to query parameter."
最近在学习python,所以就各种找需求去练习,写的也简单,各种堆命令,有什么不对的地方或者好的建议,还请指出。