Get Linux basic System information using Python language (i): Get Linux version, kernel, current time

Source: Internet
Author: User

I have been applying for a blog for some time, but I haven't written it yet.

Mainly because did not think of the need to write something, recently in the study of the Python language, according to the book to see the most basic things, found that they do not see, and the light to see the words today to understand the understanding, after two days of their own review of this part of the time to discover and forget, so I thought of learning while writing some small So that you can better remember the grammar.

I. Development environment and test environment

Python2.7.10, Pycharm, VM virtual machines, CentOS6.3

Second, the code implementation

Code structure:

commands.py-> os_info_in.py-> os_info.py |

linux_status.py |-->linux_status_main.py

Realize:

commands.py:

Implement the command to run Linux in Python code, get the printed result set, and return the result results

1 #Coding=utf-82 ImportOS3 classCommands:4     def __init__(Self,comm):5Self.commend=Comm6     defExcute_command (self):7result =Os.popen (self.commend)8Results =Result.readlines ()9         returnResults

os_info_in.py:

Get the OS version number of Linux, the kernel version number, and the current time by importing the commands.py module you just wrote.

Recording:

Eachline.strip () Remove the space before and after Eachline
FindAll (' description:\t# (. *) ', Afind) searches for content that matches regular expressions
1 #!/usr/bin/env python2 #Coding=utf-83 Importcommands4 ImportRe5 classos_info_in:6     defOs_version (self):#get the OS version number for Linux7L_command = Commands.commands ('lsb_release-a')8comm=L_command.excute_command ()9Allline = []TenOs_ver="' One          forEachlineinchComm: A Allline.append (Eachline.strip ()) -          forAfindinchAllline: -             ifRe.findall ('description:\t# (. *)', Afind): theOs_ver = Re.findall ('description:\t# (. *)', Afind) -         returnOs_ver[0] -     defOs_kernel (self):#get kernel version of Linux -L_command = Commands.commands ('Uname-r') +comm=L_command.excute_command () -Allline = [] +          forEachlineinchComm: A Allline.append (Eachline.strip ()) at         returnAllline[0] -     defOs_date (self):#Get system Time -L_command = Commands.commands ('Date') -comm=L_command.excute_command () -Allline = [] -          forEachlineinchComm: in Allline.append (Eachline.strip ()) -         returnALLLINE[0]

linux_status.py:

Save the acquired Linux status information


1 #!/usr/bin/env python2 #Coding=utf-83 #Save Linux status information4 #OS version number: Os_version5 #kernel version number: Os_kernal6 #System Current time: Os_date7 classLinux_status:8 Pass

os_info.py:

The Linux state information obtained by os_info_in.py is saved to the class linux_status that specifically stores the status information and returns

1 #Coding=utf-82 ImportLinux_status3 Importos_info_in4 classOs_info:5     def __init__(self):6self.linux_stat=linux_status.linux_status ()7self.os_infos_in=os_info_in.os_info_in ()8     defOs_info (self):9Self.linux_stat.os_version =self.os_infos_in.os_version ()TenSelf.linux_stat.os_kernal =Self.os_infos_in.os_kernel () OneSelf.linux_stat.os_date =self.os_infos_in.os_date () A         returnSelf.linux_stat

linux_status_main.py:

Test the main function (print the information obtained)

1 #!/usr/bin/env python2 #Coding=utf-83 ImportLinux_status4 ImportOs_info5 6 #Linux version, kernel, time7linux=linux_status.linux_status ()8os=Os_info.os_info ()9linux=Os.os_info ()Ten Print 'System:', Linux.os_version One Print 'Kernel:', Linux.os_kernal A Print 'Time:', Linux.os_date

iii. Display of results

Because just touch the Python language, the small program just finished writing feeling structure is very redundant, divided into a piece of relatively easy to understand, the feeling does not use to put the results into a class inside, stored in a dictionary can (at that time not very familiar with the dictionary), later write to get the file system state is used dictionary saved, Write it first, write it slowly ...

For the first time, I hope the great God to make a lot of suggestions ~~~~~ Hee ~ ~ ~ ~

Get Linux basic System information using Python language (i): Get Linux version, kernel, current time

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.