forked from device42/nix_bsd_mac_inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_aix.py
168 lines (152 loc) · 6.71 KB
/
module_aix.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import paramiko
class GetAixData:
def __init__(self, ip, ssh_port, timeout, usr, pwd, use_key_file, key_file,
get_serial_info, get_hardware_info, get_os_details,
get_cpu_info, get_memory_info, ignore_domain, upload_ipv6, debug):
self.machine_name = ip
self.port = int(ssh_port)
self.timeout = timeout
self.username = usr
self.password = pwd
self.ssh = paramiko.SSHClient()
self.use_key_file = use_key_file
self.key_file = key_file
self.get_serial_info = get_serial_info
self.get_hardware_info = get_hardware_info
self.get_os_details = get_os_details
self.get_cpu_info = get_cpu_info
self.get_memory_info = get_memory_info
self.ignore_domain = ignore_domain
self.upload_ipv6 = upload_ipv6
self.debug = debug
self.ssh = paramiko.SSHClient()
self.conn = None
self.sysdata = {}
self.alldata = []
self.name = None
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def main(self):
self.connect()
self.get_sys()
self.get_IP()
self.alldata.append(self.sysdata)
return self.alldata
def connect(self):
try:
if not self.use_key_file:
self.ssh.connect(str(self.machine_name), port=self.port,
username=self.username, password=self.password, timeout=self.timeout)
else:
self.ssh.connect(str(self.machine_name), port=self.port,
username=self.username, key_filename=self.key_file, timeout=self.timeout)
except paramiko.AuthenticationException:
print str(self.machine_name) + ': authentication failed'
return None
except Exception as err:
print str(self.machine_name) + ': ' + str(err)
return None
def get_sys(self):
if self.get_cpu_info:
cmd = 'lsconf | egrep -i "system model|machine serial|processor type|number of processors|' \
'processor clock speed|cpu type|kernel type|^memory size|disk drive|host name"; oslevel'
stdin, stdout, stderr = self.ssh.exec_command(cmd)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
osver = data_out[-1].strip()
self.sysdata.update({'osver': osver})
self.sysdata.update({'os': 'AIX'})
disknum = 0
for x in data_out:
if 'System Model' in x:
pass
if 'Machine Serial Number' in x:
serial = x.split()[-1].strip()
self.sysdata.update({'serial_no': serial})
if 'Number Of Processors' in x:
cpucount = x.split()[-1].strip()
self.sysdata.update({'cpucount': cpucount})
if 'Processor Clock Speed' in x:
cpupower = x.split()[-2].strip()
self.sysdata.update({'cpupower': cpupower})
if 'CPU Type' in x:
pass
if 'Kernel Type' in x:
pass
if 'Memory Size' in x:
memory = x.split()[-2].strip()
self.sysdata.update({'memory': memory})
if 'Disk Drive' in x:
disknum += 1
# hddsize = self.get_hdd_size(hddname)
# self.sysdata.update({'hddsize':hddsize})
if 'Host Name' in x:
devicename = x.split()[-1].strip()
self.name = devicename
self.sysdata.update({'name': self.name})
self.sysdata.update({'hddcount': disknum})
else:
print data_err
def get_MAC(self, nicname):
cmd = "entstat -d %s| grep -i 'hardware address'" % nicname
stdin, stdout, stderr = self.ssh.exec_command(cmd)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
mac = data_out[0].split()[2].strip()
return mac
else:
print 'Error: ', data_err
return None
def get_IP(self):
stdin, stdout, stderr = self.ssh.exec_command("/usr/sbin/ifconfig -a")
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
nics = []
header = ''
for rec in data_out:
if rec.startswith('\t'):
header += rec
else:
if header == '':
header += rec
else:
nics.append(list(header.split('\n')))
header = ''
header += rec
nics.append(list(header.split('\n')))
for nic in nics:
nicname = nic[0].split(':')[0]
if not nicname.startswith('lo'):
mac = self.get_MAC(nicname)
for rec in nic:
nicdata = {}
macdata = {}
if 'inet ' in rec or 'inet6 ' in rec:
ip = rec.split()[1]
if '/' in ip: # ipv6
ip = ip.split('/')[0]
name = self.name
nicdata.update({'ipaddress': ip})
nicdata.update({'macaddress': mac})
nicdata.update({'device': name})
nicdata.update({'tag': nicname})
self.alldata.append(nicdata)
if mac != '':
macdata.update({'macaddress': mac})
macdata.update({'port_name': nicname})
macdata.update({'device': name})
self.alldata.append(macdata)
else:
print 'Error: ', data_err
def get_hdd_size(self, hddname):
cmd = "bootinfo -s %s" % hddname
stdin, stdout, stderr = self.ssh.exec_command(cmd)
data_out = stdout.readlines()
data_err = stderr.readlines()
if not data_err:
size = int(data_out[0].strip()) / 1024
return str(size)
else:
print 'Error: ', data_err