Obtain linux local information using python [perfect]

Source: Internet
Author: User

Obtain information about the linux host, including kernel, IP, Memory, and Disk, written in python.
 

######################################## ########################################
# 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 ".#
######################################## #######################################
This program is used to obtain information about the linux host.
You can enter the "kernel", "ip", "memory", and "disk" keywords to obtain response parameter information.
You can also enter "all" to view all parameters.

The program content is as follows:

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

I have been studying python recently, so I want to find various requirements to practice. It is also simple to write, and there are various heap commands. What are wrong or good suggestions? Please also point out.

This article comes from "My future is not a dream ." Blog, please be sure to keep this source http://song49.blog.51cto.com/4480450/1086861

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.