-
Notifications
You must be signed in to change notification settings - Fork 0
/
osinfo.py
executable file
·58 lines (56 loc) · 2.19 KB
/
osinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
import re
import socket
import os.path
import subprocess
class OSinfo:
def __init__(self):
try:
self.FQDN=socket.gethostbyaddr(socket.gethostname())[0]
except:
self.FQDN = socket.gethostname()
self.hostName = self.FQDN.split('.')[0]
self.OS = "Unknown"
self.OSversion = "Unknown"
if os.path.isfile('/etc/os-release'):
for line in open('/etc/os-release', 'r'):
m = re.match('^VERSION_ID\s*=\s*\"(.*)\"$', line)
if m:
self.OSversion = m.group(1)
m = re.match('^NAME\s*=\s*(.*)$', line)
if m:
self.OS = m.group(1)
elif os.path.isfile('/etc/SuSE-release'):
for line in open('/etc/SuSE-release', 'r'):
m = re.match('^VERSION = (.*)$', line)
if m:
self.OSversion = m.group(1)
m = re.match('^(.*suse.*?)\s*\d*', line, re.IGNORECASE)
if m:
self.OS = m.group(1)
elif os.path.isfile('/etc/redhat-release'):
for line in open('/etc/redhat-release', 'r'):
m = re.match('^(.*?) Linux .* release (\d+\.\d+)', line, re.IGNORECASE)
if m:
self.OS = m.group(1)
self.OSversion = m.group(2)
for line in open('/proc/sys/kernel/osrelease'):
self.kernel = line.strip()
try:
raw = subprocess.Popen('onload --version', shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
vstring = raw.stdout.readline()
self.TOE = vstring.strip()
except:
self.TOE = None
def __str__(self):
OSstring = "Node {0} (FQDN: {1}) running {2} version {3} with kernel {4}.".format(
self.hostName, self.FQDN, self.OS, self.OSversion, self.kernel)
if self.TOE:
OSstring = OSstring + " TOE is {0}".format(self.TOE)
else:
OSstring = OSstring + " No TOE installed."
return OSstring
if __name__ == '__main__':
MyOS = OSinfo()
print(MyOS)