查看系統(tǒng)版本信息是一件家常便飯的事情,有時(shí)候需要將版本信息錄入到資產(chǎn)管理系統(tǒng)中,如果每次手動(dòng)的去查詢這些信息再錄入系統(tǒng)那么是一件令人呢頭疼的事情,如果采用腳本去完成這件事情,那么情況就有所不同了。
在Python的世界里,獲取Windows版本信息和Linux的版本信息都可以采用platform模塊,但platform模塊也不是萬(wàn)能的,有些特殊的信息(比如Windows的內(nèi)部版本號(hào))這個(gè)模塊拿不到,那么只能另辟蹊徑了。
在Linux系統(tǒng)中,可以簡(jiǎn)單的認(rèn)為一切都是文件,那么就算沒有現(xiàn)成的命令可用時(shí),可以用open()文件的方法通過對(duì)文件的讀寫控制它。而在Windows的大部分信息在注冊(cè)表中都能查到,因此可以從注冊(cè)表上下手。Windows注冊(cè)表是一個(gè)好東西,拿數(shù)據(jù)就像在Linux下一切都是文件一樣方便,如果想用Python訪問注冊(cè)表,除了權(quán)限外就是需要模塊了,在Python中_winreg是一個(gè)內(nèi)置模塊,通過這一模塊可以對(duì)注冊(cè)表進(jìn)行讀寫。
本腳本收集了一些獲取版本信息的常見方法,除了platform模塊外,還有其他的模塊可供使用,因?yàn)閜latform模塊不是內(nèi)置模塊,因此需要額外安裝。Windows下運(yùn)行腳本需要考慮權(quán)限問題和中文字符的問題,解決Python打印中文字符的問題是通過腳本中的get_system_encoding()函數(shù)實(shí)現(xiàn)的,這個(gè)函數(shù)取自Django,經(jīng)過測(cè)試這個(gè)函數(shù)還是非常好用的。
注:在PyCharm中,經(jīng)常遇到Run窗口打印出的中文顯示亂碼,代碼中沒有經(jīng)過正確轉(zhuǎn)碼是一方面,而IDE的編碼設(shè)置也是一方面。如果是在Windows下開發(fā),那么建議代碼用UTF-8編寫,IDE的編碼則設(shè)置為“GBK”,設(shè)置方法“File”-->"Settings"-->"Editor"-->"File Encoding"-->"IDE Encoding"選擇“<System Default (now GBK)>”, "Project Encoding"選擇UTF-8保證代碼的編碼一致性。

腳本如下:
#!/usr/bin/python# encoding: utf-8# -*- coding: utf8 -*-"""Created by PyCharm.File: LinuxBashShellScriptForOps:getSystemVersion.pyUser: GuodongCreate Date: 2016/12/16Create Time: 14:51 """import sysimport osimport platformimport subprocessimport codecsimport localedef get_system_encoding(): """ The encoding of the default system locale but falls back to the given fallback encoding if the encoding is unsupported by python or could not be determined. See tickets #10335 and #5846 """ try: encoding = locale.getdefaultlocale()[1] or 'ascii' codecs.lookup(encoding) except Exception: encoding = 'ascii' return encodingDEFAULT_LOCALE_ENCODING = get_system_encoding()mswindows = (sys.platform == "win32") # learning from 'subprocess' modulelinux = (sys.platform == "linux2")hidden_hostname = Trueif mswindows: uname = list(platform.uname()) if hidden_hostname: uname[1] = "hidden_hostname" print uname import _winreg try: reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE//Microsoft//Windows NT//CurrentVersion") if reg_key: ProductName = _winreg.QueryValueEx(reg_key, "ProductName")[0] or None EditionId = _winreg.QueryValueEx(reg_key, "EditionId")[0] or None ReleaseId = _winreg.QueryValueEx(reg_key, "ReleaseId")[0] or None CurrentBuild = _winreg.QueryValueEx(reg_key, "CurrentBuild")[0] or None BuildLabEx = _winreg.QueryValueEx(reg_key, "BuildLabEx")[0][:9] or None print (ProductName, EditionId, ReleaseId, CurrentBuild, BuildLabEx) except Exception as e: print e.message.decode(DEFAULT_LOCALE_ENCODING)if linux: uname = list(platform.uname()) if hidden_hostname: uname[1] = "hidden_hostname" print uname proc_obj = subprocess.Popen(r'uname -a', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) result = proc_obj.stdout.read().strip().decode(DEFAULT_LOCALE_ENCODING) if result: print result if os.path.isfile("/proc/version"): with open("/proc/version", 'r') as f: content = f.read().strip() if content != "": print content if os.path.isfile("/etc/issue"): with open("/etc/issue", 'r') as f: content = f.read().strip() if content != "": print content截圖如下:
(1)注冊(cè)表信息獲取位置:

(2)Windows環(huán)境下的輸出:

(3)Linux環(huán)境下的輸出:

新聞熱點(diǎn)
疑難解答
圖片精選