歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python 獲取Linux本機信息

Python 獲取Linux本機信息

日期:2017/3/1 10:05:29   编辑: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:[email protected]
  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,所以就各種找需求去練習,寫的也簡單,各種堆命令,有什麼不對的地方或者好的建議,還請指出。

Copyright © Linux教程網 All Rights Reserved