Use the Python language to compile and obtain basic Linux system information (1): Obtain the Linux version, kernel, current time, And pythonlinux

Source: Internet
Author: User

Use the Python language to compile and obtain basic Linux system information (1): Obtain the Linux version, kernel, current time, And pythonlinux

I have been applying for a blog for a while, but I haven't written any more .....

I did not think of anything to write. I recently learned the Python language and read the most basic things in the book. I found that I could not read them at all, in addition, I thought I understood it all today. I forgot it when I reviewed this part of content in two days, so I thought that I could learn and write some small programs so that I could better remember the syntax.

I. Development Environment and test environment

Python2.7.10, pycharm, vmvm, CentOS6.3

Ii. Code Implementation

Code structure:

Commands. py-> OS _info_in.py-> OS _info.py |

Linux_status.py | --> linux_status_main.py

Implementation:

Commands. py:

Run the Linux Command in Python code to obtain the printed result set and return the result results.

 

1 # coding=UTF-82 import os3 class commands:4     def __init__(self,comm):5         self.commend=comm6     def excute_command(self):7         result = os.popen(self.commend)8         results = result.readlines()9         return results

 

OS _info_in.py:

Import the commands. py module you just compiled to obtain the Linux OS version number, kernel version number, and current time.

Record:

Remove spaces before and after eachLine. strip ()
Findall ('description: \ t # (. *) ', afind) searches for the content that matches the Regular Expression
1 #! /Usr/bin/env python 2 # coding = UTF-8 3 import commands 4 import re 5 class OS _info_in: 6 def OS _version (self): # Get linux OS Version 7 L_command = commands. commands ('lsb _ release-a') 8 comm = L_command.excute_command () 9 allLine = [] 10 OS _ver = ''11 for eachLine in comm: 12 allLine. append (eachLine. strip () 13 for afind in allLine: 14 if re. findall ('description: \ t #(. *) ', afind): 15 OS _ver = re. findall ('description: \ t #(. *) ', afind) 16 return OS _ver [0] 17 def OS _kernel (self): # Get linux kernel version 18 L_command = commands. commands ('uname-R') 19 comm = L_command.excute_command () 20 allLine = [] 21 for eachLine in comm: 22 allLine. append (eachLine. strip () 23 return allLine [0] 24 def OS _date (self): # obtain the system time 25 L_command = commands. commands ('date') 26 comm = L_command.excute_command () 27 allLine = [] 28 for eachLine in comm: 29 allLine. append (eachLine. strip () 30 return allLine [0]

Linux_status.py:

Save the obtained linux status information


1 #! /Usr/bin/env python2 # coding = UTF-83 # Save linux status information 4 # OS Version: OS _version5 # kernel version: OS _kernal6 # system current time: OS _date7 class linux_status: 8 pass

 

OS _info.py:

The linux status information obtained through OS _info_in.py is saved to the linux_status class for storing the status information and returned

 1 # coding=UTF-8 2 import linux_status 3 import os_info_in 4 class os_info: 5     def __init__(self): 6         self.linux_stat=linux_status.linux_status() 7         self.os_infos_in= os_info_in.os_info_in() 8     def os_info(self): 9         self.linux_stat.os_version = self.os_infos_in.os_version()10         self.linux_stat.os_kernal = self.os_infos_in.os_kernel()11         self.linux_stat.os_date = self.os_infos_in.os_date()12         return self.linux_stat

 

Linux_status_main.py:

Test the main function (print the obtained information)

1 #! /Usr/bin/env python 2 # coding = UTF-8 3 import linux_status 4 import OS _info 5 6 # linux version, kernel, time 7 linux = linux_status.linux_status () 8 OS = OS _info. OS _info () 9 linux = OS. OS _info () 10 print 'System: ', linux. OS _version11 print 'kernel: ', linux. OS _kernal12 print 'time: ', linux. OS _date

 

Iii. Result Display

 

 

 

 

Since I was just familiar with the Python language, I feel that the structure of the applet is redundant and it is easy to understand it by dividing it into one piece. I don't feel that I need to store the results in one class, you can store it in a dictionary (not very familiar with the dictionary at the time). You can save it in a dictionary to obtain the file system status in the future. Write it here and write it slowly...

For the first time, I hope you can give more comments ~~~~~ Xi ~~~~

 

Related Article

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.