def __init__(self, server, config): threading.Thread.__init__(self) self.server = server self.config = config self._logger = tools.create_logger(__name__, self.config) self.config_server = ressources.ConfigServer(0)
def __init__(self, server, config, lock): threading.Thread.__init__(self) self.server = server self.config = config self.kb_patches = server.kb_patches self._logger = tools.create_logger(__name__, self.config) self._lock = lock self.config_server = ressources.ConfigServer(4)
def __init__(self, server, config, lock): threading.Thread.__init__(self) self.server = server self.config = config self.packages = server.packages self.active = False self._logger = tools.create_logger(__name__, self.config) self._lock = lock self.config_server = ressources.ConfigServer(3)
def __init__(self, server, config, lock): threading.Thread.__init__(self) self.server = server self.config = config self.packages = server.packages self.active = False self._logger = tools.create_logger(__name__, self.config) self._lock = lock self.config_server = ressources.ConfigServer(1)
def Import_cisco_switch_file(filename, config): """ Create a Server object from an extraction script result archive """ import time logger = tools.create_logger(__name__, config) switch_to_import = Switch() startTime = time.time() if not os.path.isfile(filename): logger.error("Cisco switch import error, file not foud : " + str(filename)) return False else: switch_to_import.name = filename.split('/')[-1] switch_to_import.manufacturer = "Cisco" # Open the file and store lines in a list file_switch = open(filename, 'rb') file_content_lines = file_switch.readlines() file_switch.seek(0, 0) file_content_exclamation = file_switch.read().split('!\n') file_switch.close() nf.import_cisco_hostname(switch_to_import, file_content_lines, logger) nf.import_cisco_osversion(switch_to_import, file_content_lines, logger) nf.import_cisco_vlan(switch_to_import, file_content_exclamation, logger) nf.import_cisco_interfaces_and_switchport(switch_to_import, file_content_exclamation, logger) nf.import_cisco_route(switch_to_import, file_content_lines, logger) nf.import_cisco_catalyst_acl_table(switch_to_import, file_content_lines, logger) print switch_to_import print switch_to_import.acl_table print switch_to_import.vlan print switch_to_import.interfaces print switch_to_import.switchport print switch_to_import.routes # import_osname(server_to_import, xtract_dir, logger) endTime = time.time() logger.info( "Cisco switch successfully imported. Time : {0:.2} secs\n".format( endTime - startTime)) return switch_to_import
def Import_Debian_server_archive(filename, config): """ Create a Server object from an extraction script result archive """ import time logger = tools.create_logger(__name__, config) server_to_import = Server() startTime = time.time() [tmp_path, xtract_dir] = Linux.extract_archive(server_to_import, logger, filename) logger.info("\nServer hostname : " + str(server_to_import.name)) server_to_import.ostype = "Debian" logger.info("OS type : " + str(server_to_import.ostype)) Linux.import_version_script(server_to_import, xtract_dir, logger) import_osname(server_to_import, xtract_dir, logger) import_osversion(server_to_import, xtract_dir, logger) import_kernel_release(server_to_import, xtract_dir, logger) Linux.import_accounts(server_to_import, xtract_dir, logger) Linux.import_interfaces(server_to_import, xtract_dir, logger) Linux.import_connections(server_to_import, xtract_dir, logger) Linux.import_processes(server_to_import, xtract_dir, logger) Linux.import_files(server_to_import, xtract_dir, logger, config) Linux.import_nsswith(server_to_import, xtract_dir, logger) Linux.import_ssh_config(server_to_import, xtract_dir, logger) Linux.import_sudoers(server_to_import, xtract_dir, logger) Linux.import_fstab(server_to_import, xtract_dir, logger) Linux.import_crontab(server_to_import, xtract_dir, logger, "/var/spool/cron/crontabs") Linux.import_ip_hostname_local(server_to_import, xtract_dir, logger) import_packages(server_to_import, xtract_dir, logger) import_pam(server_to_import, xtract_dir, logger) Linux.remove_extracted_archive(tmp_path, logger) endTime = time.time() logger.info( "Server successfully imported. Time : {0:.2} secs\n".format(endTime - startTime)) return server_to_import
def __init__(self, server, config): self.server = server self.config = config self.lock = threading.Lock() self._logger = tools.create_logger(__name__, self.config) if self.server.ostype == "Debian": self.analyze_debian_server() elif self.server.ostype == "CentOS": self.analyze_centos_server() elif self.server.ostype == "Red Hat": self.analyze_redhat_server() elif self.server.ostype == "Windows Server": self.analyze_windows_server() else: self._logger.error("Analyze not supported for this kinf of server : " + str(self.server.ostype))
def Import_cisco_switch_file(filename, config): """ Create a Server object from an extraction script result archive """ import time logger = tools.create_logger(__name__, config) switch_to_import = Switch() startTime = time.time() if not os.path.isfile(filename): logger.error("Cisco switch import error, file not foud : " + str(filename)) return False else: switch_to_import.name = filename.split('/')[-1] switch_to_import.manufacturer = "Cisco" # Open the file and store lines in a list file_switch = open(filename, 'rb') file_content_lines = file_switch.readlines() file_switch.seek(0, 0) file_content_exclamation = file_switch.read().split('!\n') file_switch.close() nf.import_cisco_hostname(switch_to_import, file_content_lines, logger) nf.import_cisco_osversion(switch_to_import, file_content_lines, logger) nf.import_cisco_vlan(switch_to_import, file_content_exclamation, logger) nf.import_cisco_interfaces_and_switchport(switch_to_import, file_content_exclamation, logger) nf.import_cisco_route(switch_to_import, file_content_lines, logger) nf.import_cisco_catalyst_acl_table(switch_to_import, file_content_lines, logger) print switch_to_import print switch_to_import.acl_table print switch_to_import.vlan print switch_to_import.interfaces print switch_to_import.switchport print switch_to_import.routes # import_osname(server_to_import, xtract_dir, logger) endTime = time.time() logger.info("Cisco switch successfully imported. Time : {0:.2} secs\n".format(endTime - startTime)) return switch_to_import
def Import_Windows_server_archive(filename, config): """ Create a Server object from an extraction script result archive """ import time logger = tools.create_logger(__name__, config) server_to_import = ServerWindows() startTime = time.time() # [tmp_path, xtract_dir] = Linux.extract_archive(server_to_import, logger, filename) [tmp_path, xtract_dir] = extract_archive(logger, filename) server_to_import.registering_path = config.logs_path server_to_import.ostype = "Windows Server" logger.info("OS type : " + str(server_to_import.ostype)) import_hostname(server_to_import, xtract_dir, logger) logger.info("\nServer hostname : " + str(server_to_import.name)) import_architecture(server_to_import, xtract_dir, logger) import_os_name(server_to_import, xtract_dir, logger) import_os_and_kernel_version(server_to_import, xtract_dir, logger) logger.info("Architecture : " + str(server_to_import.osarchitecture)) logger.info("OS name : " + str(server_to_import.osname)) logger.info("OS version : " + str(server_to_import.osversion)) logger.info("Kernel version : " + str(server_to_import.kernel_version)) import_KB_list(server_to_import, xtract_dir, logger) import_interfaces(server_to_import, xtract_dir, logger) import_processes(server_to_import, xtract_dir, logger) import_connections(server_to_import, xtract_dir, logger) import_services(server_to_import, xtract_dir, logger) import_software(server_to_import, xtract_dir, logger) import_group(server_to_import, xtract_dir, logger) import_ip_hostname_local(server_to_import, xtract_dir, logger) Linux.remove_extracted_archive(tmp_path, logger) endTime = time.time() logger.info( "Server successfully imported. Time : {0:.2} secs\n".format(endTime - startTime)) return server_to_import
def Import_Debian_server_archive(filename, config): """ Create a Server object from an extraction script result archive """ import time logger = tools.create_logger(__name__, config) server_to_import = Server() startTime = time.time() [tmp_path, xtract_dir] = Linux.extract_archive(server_to_import, logger, filename) logger.info("\nServer hostname : " + str(server_to_import.name)) server_to_import.ostype = "Debian" logger.info("OS type : " + str(server_to_import.ostype)) Linux.import_version_script(server_to_import, xtract_dir, logger) import_osname(server_to_import, xtract_dir, logger) import_osversion(server_to_import, xtract_dir, logger) import_kernel_release(server_to_import, xtract_dir, logger) Linux.import_accounts(server_to_import, xtract_dir, logger) Linux.import_interfaces(server_to_import, xtract_dir, logger) Linux.import_connections(server_to_import, xtract_dir, logger) Linux.import_processes(server_to_import, xtract_dir, logger) Linux.import_files(server_to_import, xtract_dir, logger, config) Linux.import_nsswith(server_to_import, xtract_dir, logger) Linux.import_ssh_config(server_to_import, xtract_dir, logger) Linux.import_sudoers(server_to_import, xtract_dir, logger) Linux.import_fstab(server_to_import, xtract_dir, logger) Linux.import_crontab(server_to_import, xtract_dir, logger, "/var/spool/cron/crontabs") Linux.import_ip_hostname_local(server_to_import, xtract_dir, logger) import_packages(server_to_import, xtract_dir, logger) import_pam(server_to_import, xtract_dir, logger) Linux.remove_extracted_archive(tmp_path, logger) endTime = time.time() logger.info("Server successfully imported. Time : {0:.2} secs\n".format(endTime - startTime)) return server_to_import
def Import_Windows_server_archive(filename, config): """ Create a Server object from an extraction script result archive """ import time logger = tools.create_logger(__name__, config) server_to_import = ServerWindows() startTime = time.time() # [tmp_path, xtract_dir] = Linux.extract_archive(server_to_import, logger, filename) [tmp_path, xtract_dir] = extract_archive(logger, filename) server_to_import.registering_path = config.logs_path server_to_import.ostype = "Windows Server" logger.info("OS type : " + str(server_to_import.ostype)) import_hostname(server_to_import, xtract_dir, logger) logger.info("\nServer hostname : " + str(server_to_import.name)) import_architecture(server_to_import, xtract_dir, logger) import_os_name(server_to_import, xtract_dir, logger) import_os_and_kernel_version(server_to_import, xtract_dir, logger) logger.info("Architecture : " + str(server_to_import.osarchitecture)) logger.info("OS name : " + str(server_to_import.osname)) logger.info("OS version : " + str(server_to_import.osversion)) logger.info("Kernel version : " + str(server_to_import.kernel_version)) import_KB_list(server_to_import, xtract_dir,logger) import_interfaces(server_to_import, xtract_dir, logger) import_processes(server_to_import, xtract_dir, logger) import_connections(server_to_import, xtract_dir, logger) import_services(server_to_import, xtract_dir, logger) import_software(server_to_import, xtract_dir,logger) import_group(server_to_import, xtract_dir,logger) import_ip_hostname_local(server_to_import, xtract_dir,logger) Linux.remove_extracted_archive(tmp_path, logger) endTime = time.time() logger.info("Server successfully imported. Time : {0:.2} secs\n".format(endTime - startTime)) return server_to_import
def Import_server_from_archive(filename, config): """ Function which detect the OS from an archive and extract the server Return server_to_import """ logger = tools.create_logger(__name__, config) os_number = detect_OS(filename, config) if os_number == 0: logger.error("Unable to determine the server OS : " + filename) return None else: server_to_import = None if os_number == 1: server_to_import = Windows.Import_Windows_server_archive(filename, config) elif os_number == 2: server_to_import = Debian.Import_Debian_server_archive(filename, config) elif os_number == 3: server_to_import = CentOS_RHEL.Import_CentOS_RHEL_server_archive(filename, config) else: logger.error("Unable to determine the OS but os_number not 0 :" + filename) return server_to_import
def Import_servers_from_folder(foldername, config): """ Function which import several server archives in a folder """ logger = tools.create_logger(__name__, config) if os.path.isdir(foldername): architecture_to_import = archi.Architecture() architecture_to_import.servers = devices.ServerList() file_list = os.listdir(foldername) for file_elem in file_list: path = os.path.join(foldername, file_elem) srv = Import_server_from_archive(path, config) architecture_to_import.servers.add_server(srv) create_ip_hostname_common(architecture_to_import) logger.info(str(architecture_to_import.servers.counter) + " servers imported from the folder : " + str(foldername)) tools.merge_logs(config) return architecture_to_import else: logger.error("Folder not found") return False
import sys sys.path.append("/path/to/pyCAF/repository") import pycaf.importer as importer import pycaf.analyzer as analyzer import pycaf.tools as tools import pycaf.architecture as archi import re import urllib2 from subprocess import call config = tools.GeneralConfigParse() logs = tools.create_logger(__name__, config, True)
def detect_OS(filename, config): """ Function which detect an OS from the archive Return an integer: 1 : Windows Server 2 : Debian 3 : Red Hat or CentOS 0 : not found """ import tarfile import zipfile import tempfile import shutil logger = tools.create_logger(__name__, config) # Step 1 : zip file -> Windows Server [1] else Linux # Step 2 (Linux) # /etc/debian_version exist -> Debian [2] # Step 3 # /etc/redhat-release exist -> CentOS or Red Hat [3] # Step 1 if zipfile.is_zipfile(filename): return 1 else: try: tarfile.is_tarfile(filename) tmp_path = tempfile.mkdtemp() # 1 - decompress archive compressed = tarfile.open(filename) compressed.extractall(tmp_path) # Get extraction directory name and server hostname for (dirname, dirnames, filenames) in os.walk(tmp_path): if len(dirnames) != 1: raise tarfile.ExtractError("Unknown archive format") hostname = dirnames.pop() tmp_xtract_dir = os.path.join(tmp_path, hostname) if os.path.isdir(tmp_xtract_dir): xtract_dir = tmp_xtract_dir # Extraction of the /etc archive if applicable etc = os.path.join(xtract_dir, "etc.tar.bz2") comp = tarfile.open(etc) comp.extractall(xtract_dir) # Step 2 # Debian if os.path.isfile(os.path.join(xtract_dir, "etc/debian_version")): shutil.rmtree(tmp_path) return 2 elif os.path.isfile(os.path.join(xtract_dir, "etc/redhat-release")): shutil.rmtree(tmp_path) return 3 else: logger.error("Unable to determine the OS, no useful file found in etc/") shutil.rmtree(tmp_path) return 0 except IOError: if os.path.isdir(filename): if os.path.isfile(os.path.join(filename, "ps2.txt")): return 1 elif os.path.isfile(os.path.join(filename, "etc/debian_version")): return 2 elif os.path.isfile(os.path.join(filename, "etc/redhat-release")): return 3 else: logger.error("Folder found but impossible to found a matching file for OS detection") else: logger.error("Path is neither an archive or a folder, check your path") return 0