Package pyhai :: Package plugins :: Package windows :: Module network
[hide private]
[frames] | no frames]

Source Code for Module pyhai.plugins.windows.network

 1  """ 
 2  @copyright: 2011 Mark LaPerriere 
 3   
 4  @license: 
 5      Licensed under the Apache License, Version 2.0 (the "License"); 
 6      you may not use this file except in compliance with the License. 
 7      You may obtain a copy of the License at 
 8   
 9      U{http://www.apache.org/licenses/LICENSE-2.0} 
10   
11      Unless required by applicable law or agreed to in writing, software 
12      distributed under the License is distributed on an "AS IS" BASIS, 
13      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14      See the License for the specific language governing permissions and 
15      limitations under the License. 
16   
17  @summary: 
18      A network auditing plugin that should work on most Windows systems 
19   
20  @author: Mark LaPerriere 
21  @contact: pyhai@mindmind.com 
22  @organization: Mind Squared Design / www.mindmind.com 
23  @version: 0.1.3 
24  @date: Jan 19, 2012 
25  """ 
26  from pyhai.plugins import AuditorPlugin 
27  import logging 
28   
29  # set some default logging behavior 
30  _logger = logging.getLogger(__name__) 
31  _logger.addHandler(logging.NullHandler()) 
32   
33  try: 
34      from win32com.client import GetObject 
35  except: 
36      _logger.exception('%s failed to import required Windows specific modules: %s', __name__, ', '.join('win32com.client.GetObject',)) 
37      raise 
38   
39 -class NetworkPlugin(AuditorPlugin):
40 - def run(self, *args, **kwargs):
41 network_info = {} 42 43 wmi = GetObject(r'winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2') 44 network_adaptor_configurations = wmi.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE') 45 network_adaptors = wmi.ExecQuery('SELECT * FROM Win32_NetworkAdapter') 46 47 for nic_config in network_adaptor_configurations: 48 if not nic_config.IPEnabled: 49 continue 50 51 config_index = str(nic_config.Index) 52 if config_index not in network_info: 53 network_info[config_index] = {} 54 55 network_info[config_index]['dns_servers'] = [str(host) for host in self._get_or_list(nic_config.DNSServerSearchOrder)] 56 network_info[config_index]['dhcp_enabled'] = nic_config.DHCPEnabled 57 network_info[config_index]['dhcp_server'] = nic_config.DHCPServer 58 network_info[config_index]['ip_address'] = [str(ip) for ip in self._get_or_list(nic_config.IPAddress)] 59 network_info[config_index]['subnet_mask'] = [str(sm) for sm in self._get_or_list(nic_config.IPSubnet)] 60 network_info[config_index]['default_gateway'] = [str(dg) for dg in self._get_or_list(nic_config.DefaultIPGateway)] 61 network_info[config_index]['mac_address'] = nic_config.MACAddress 62 network_info[config_index]['wins_servers'] = [str(nic_config.WINSPrimaryServer), str(nic_config.WINSSecondaryServer)] 63 64 for nic in network_adaptors: 65 nic_index = str(nic.Index) 66 if nic_index not in network_info: 67 continue 68 69 network_info[nic_index]['name'] = nic.Name 70 network_info[nic_index]['label'] = nic.NetConnectionID 71 network_info[nic_index]['manufacturer'] = nic.Manufacturer 72 network_info[nic_index]['device_id'] = nic.PNPDeviceID 73 network_info[nic_index]['guid'] = nic.GUID 74 network_info[nic_index]['speed'] = nic.Speed 75 76 return network_info
77
78 - def _get_or_list(self, obj):
79 try: 80 if obj: 81 return obj 82 else: 83 return list() 84 except: 85 return list()
86