log_error(message=' Invalid Hosts File.') exit(1) if not os.path.isfile(args['command_file']): log_error(message=' Invalid Commands File.') exit(1) with open(args['hosts_file'], 'r') as hf: for host in hf: host = host.strip() ssh_message = " Attempting to log into %s via SSH." % host telnet_message = " Attempting to log into %s via Telnet." % host log_debug(message=' Reading %s from the host file.' % host) if args['protocol'] == 'ssh': try: log_debug(message=ssh_message) access = SSH(host, creds['username'], creds['password']) access.connect() except: log_debug(message=' Error connecting via SSH.') try: log_debug(message=telnet_message) access = Telnet(host, creds['username'], creds['password']) access.connect() except: log_debug(message=' Unable to connect to %s' % host) exit(1) elif args['protocol'] == 'telnet': try: log_debug(telnet_message) access = Telnet(host, creds['username'], creds['password'])
#!/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()