log_debug(message=' Erorr connecting via Telnet.') try: log_debug(message=telnet_message) access = SSH(host, creds['username'], creds['password']) access.connect() except: log_debug(message=' Unable to connect to %s' % host) exit(1) else: log_debug(message=' ERROR: Unknown connection type.') exit(1) access.set_enable(creds['enable']) access.disable_paging() with open(args['command_file'], 'r') as cf: log_debug(message=' Reading the commands file.') for command in cf: command = command.strip() log_debug(message=' Executing %s' % command) if args['command_output'] is True: print(access.command(command)) else: access.command(command) log_debug(message=' Closing connection to %s.' % host) access.close() log_debug(message=' Closing the commands file.') cf.close() log_debug(message=' Closing the hosts file.') hf.close()
#!/usr/bin/env python from netlib.netlib.user_creds import simple_yaml from netlib.netlib.conn_type import SSH from os.path import expanduser import sys creds = simple_yaml() base_dir = expanduser("~/net-ansible") hostname = sys.argv[1] command_file = sys.argv[2] ssh = SSH(hostname, creds['username'], creds['password']) ssh.connect() ssh.set_enable(creds['enable']) with open(base_dir + "/" + command_file) as f: for line in f.readlines(): line = line.strip() ssh.command(line) f.close() ssh.close()