def sync_workspace(workspaces_ini_path, local_recon_path, host_name=''):
    ssh_config = configparser.ConfigParser()
    ssh_config.read(os.path.join(local_recon_path, 'servers.ini'))

    workspaces = configparser.ConfigParser()
    workspaces.read(workspaces_ini_path)

    servers = []
    server_observers = {}

    if host_name:
        curr_server = get_servers(os.path.join(local_recon_path,
                                               'servers.ini'),
                                  host_name=host_name)
        servers.append((host_name, curr_server))
    else:
        for host_name in workspaces.sections():
            # Initiating the Connection
            curr_server = get_servers(os.path.join(local_recon_path,
                                                   'servers.ini'),
                                      host_name=host_name)
            servers.append((host_name, curr_server))

    for host_name, curr_server in servers:
        # Initiating the Client and Server paths
        ## Get The HOME Directory of the SSH Server
        stdin, stdout, stderr = curr_server['connection'].exec_command(
            "echo $HOME")
        ssh_server_home_dir = stdout.readlines()[0].split('\n')[0]

        ## Get the Workspace Directory of SSH Client
        ssh_client_localpath = workspaces[host_name]

        for workspace_name in ssh_client_localpath.keys():
            workspace_observers = {
                workspace_name:
                start_watch_dog(curr_server,
                                ssh_client_localpath[workspace_name],
                                ssh_server_home_dir)
            }
            server_observers.update({host_name: workspace_observers})

    print('[+] Closing the Observers ...')
    for host, workspace_observers in server_observers.items():
        for workspace_name, observer in workspace_observers.items():
            print(
                f'\t[+] Closing the {host} Observer for {workspace_name} ...')
    print('[+] All Observers Terminated Successfully.')

    # print ('[+] Closing the SSH Connections ...')
    # for host_name, server in servers:
    #     server['connection'].close()
    #     print (f'\t[+] Closing the SSH Connection for {host_name}')
    # print ('[+] All SSH Connections Terminated Successfully.')


# if __name__ == '__main__':
#     sync_workspace('workspaces.ini', host_name='')
Beispiel #2
0
	las <PROCESS>: Sync all paths on workspace.ini and Execute <PROCESS> on Default Server.
	las --server SERVER_NAME --workspace WORKSPACE_NAME <PROCESS>: Sync selected workspace on workspace.ini and Execute <PROCESS> on Selected Server (SERVER_NAME must be on servers.ini).
'''

# TODO: Adjust it for the Generalized <las sync -s "SERVER" -w "WORKSPACE"> Command
if __name__ == '__main__':
    properties = configparser.ConfigParser()
    properties.read(os.path.join('.', 'config', 'props.ini'))
    properties = properties['properties']

    workspaces_config = configparser.ConfigParser()
    workspaces_config.read(os.path.join('.', 'config', 'workspaces.ini'))

    server_workspaces = workspaces_config[properties['default-server']]
    default_server = get_servers(os.path.join('.', 'config', 'servers.ini'),
                                 properties['default-server'])

    autosync = True
    handlers = {}
    observers = {}

    for workspace_name in server_workspaces:
        ## Get the Workspace Directory of SSH Client
        workspace_path = server_workspaces[workspace_name]

        ## Instantiate a WorkspaceWatchDog WatchDog (handler)
        handlers[workspace_name] = WorkspaceWatchDog(
            local=True, verbose=True, workspace_name=workspace_name)
        ## Instantiate (and Start) a Recursive Observer Object for the given handler
        observers[workspace_name] = Observer()
        observers[workspace_name].schedule(handlers[workspace_name],
print ('[+] Created Local Tree.')

## Transfer scripts and lib to recon path ##
copy_tree("lib",f"{local_recon_path}/lib")
copy("las",f"{local_recon_path}")
copy("flas",f"{local_recon_path}")

print ('[+] Local file transfering complete.')

## CREATE THE INI FILE THAT CONTAINS INFO ABOUT THE SERVERS
ini_lib.server_ini_creator(local_recon_path)
print ('[+] Created Configuration ini')
ini_path = os.path.join(config_path, 'servers.ini')

## GET SERVER OBJECTS (N)
servers = connections.get_servers(ini_path)
pp.pprint(servers)

## CREATE REMOTE DIRECTORY TREE (N)
for srv in servers:
	stdin, stdout, stderr = servers[srv]['connection'].exec_command(create_dir_tree(servers[srv]['recon_path'],dirs))
print ('[+] Created Remote Tree.')

#### WORKSPACES ####
# Create workspaces ini
workspace_path = ini_lib.workspace_ini_creator(config_path)
workspace_sync.synchronize(workspace_path, os.path.join(local_recon_path,config_path), daemon_mode=False)
print ('[+] Created workspaces ini.')

#### PROPERTIES ####
# Create props ini
def synchronize(workspaces_ini_path, servers_ini_path, host_name='', daemon_mode=True, verbose=False):
    ssh_config = configparser.ConfigParser()
    ssh_config.read(os.path.join(servers_ini_path, 'servers.ini'))
    # ssh_config.read(os.path.join(os.environ['RECON_LOCAL_PATH'],'servers.ini'))

    workspaces = configparser.ConfigParser()
    workspaces.read(workspaces_ini_path)
    
    servers = {}
    server_observers = {}

    if host_name:
        # curr_server = get_servers(os.path.join(os.environ['RECON_LOCAL_PATH'],'servers.ini'), host_name=host_name)
        curr_server = get_servers(os.path.join(servers_ini_path, 'servers.ini'), host_name=host_name)
        servers[host_name] = curr_server
    else:
        for host_name in workspaces.sections():
            # Initiating the Connection
            # curr_server = get_servers(os.path.join(os.environ['RECON_LOCAL_PATH'],'servers.ini'), host_name=host_name)
            curr_server = get_servers(os.path.join(servers_ini_path, 'servers.ini'), host_name=host_name)
            servers[host_name] = curr_server

    for host_name, curr_server in servers.items():
        # Initiating the Client and Server paths
        ## Get The HOME Directory of the SSH Server
        _, stdout, _ = curr_server['connection'].exec_command("echo $HOME")
        ssh_server_home_dir = stdout.readlines()[0].split('\n')[0]
        
        ## Get the Workspace Directory of SSH Client 
        ssh_client_localpath = workspaces[host_name]
        
        for workspace_name in ssh_client_localpath.keys():
            workspace_observers = {workspace_name:start_watchdog(curr_server, ssh_client_localpath[workspace_name], ssh_server_home_dir, hostname=host_name, verbose=verbose)}
            server_observers.update({host_name:workspace_observers})

    if (not daemon_mode):
        print ('[+] Closing the Observers ...')
        close_watchdog(server_observers)
        print ('[+] All Observers Terminated Successfully.')

        print ('[+] Closing the SSH Connections ...')
        for host_name in servers:
            close_connection(servers, host_name)
        print ('[+] All SSH Connections Terminated Successfully.')
    else:
        try:
            while True:
                print(strftime("%Y-%m-%d %H:%M:%S", gmtime()), end='\r', flush=True)
                sleep(1)
        except KeyboardInterrupt:
            print ('[+] Closing the Observers ...')
            close_watchdog(server_observers)
            print ('[+] All Observers Terminated Successfully.')
        finally:
            print ('[+] Closing the SSH Connections ...')
            for host_name in servers:
                close_connection(servers, host_name)
            print ('[+] All SSH Connections Terminated Successfully.')


# if __name__ == '__main__':
#     sync_workspace('workspaces.ini', 'servers.ini', host_name='')