python脚本

# Read from a list of hostnames to connect to
hosts = open('hosts.txt','r')
hosts = hosts.read()
hosts = hosts.strip().splitlines()

# Get UserName and password from input
userName = input('Username: ')
passWord = getpass.getpass()

# Loop to process hosts in hosts.txt file
for host in hosts:
	# Define device type and connection attributes
	cisco = {
		'device_type': 'cisco_ios',
		'ip': host,
		'username': userName,
		'password': passWord),
	}
	
	# Netmiko SSH Connection Handler
	net_connect = ConnectHandler(**cisco)

	#open file to write command output
	file = open(host + '_output.txt', 'w')
	
	# Execute commands

	output = net_connect.send_command('show run')
	
	# Print output to console screen
	print('-------------- Output from ' + host + '------------------')
	print(output)
	print()
	print()
	
	# Write output to file above
	file.write(output)
	file.close()
原文地址:https://www.cnblogs.com/zhaoyong631/p/13364994.html