本文整理汇总了Python中netmiko.ConnectHandler类的典型用法代码示例。如果您正苦于以下问题:Python ConnectHandler类的具体用法?Python ConnectHandler怎么用?Python ConnectHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConnectHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
for device in rtr_list:
rtr_conn = ConnectHandler(**device)
prompt = rtr_conn.find_prompt()
rtr_conn.config_mode()
output = rtr_conn.send_config_from_file(config_file='4_8_cfg.txt')
print output
示例2: main
def main():
ip_addr = '50.76.53.27'
port_rtr1 = 22
port_rtr2 = 8022
username = 'pyclass'
password = '88newclass'
#password = getpass()
pynet1 = {
'device_type': 'cisco_ios',
'ip': ip_addr,
'username': username,
'password': password,
'port': port_rtr1,
}
pynet2 = {
'device_type': 'cisco_ios',
'ip': ip_addr,
'username': username,
'password': password,
'port': port_rtr2,
}
pynet_rtr1 = ConnectHandler(**pynet1)
pynet_rtr2 = ConnectHandler(**pynet2)
out1 = pynet_rtr1.send_config_from_file(config_file='w4-8-commands.txt')
out2 = pynet_rtr2.send_config_from_file(config_file='w4-8-commands.txt')
print "The output in rtr1 is:\n%s\n" % out1
print "The output in rtr2 is:\n%s\n" % out2
示例3: __init__
class Router:
def __init__(self):
self.net_connect = ConnectHandler(**homeRTR)
def show_dhcp_binding(self, mac_end):
command = 'show ip dhcp binding | incl ' + mac_end
output = self.net_connect.send_command(command)
# self.net_connect.disconnect()
return output.split('\n')
def ping_ipaddr(self, ipaddr):
command = 'ping ' + ipaddr
output = self.net_connect.send_command(command)
# self.net_connect.disconnect()
return output.split('\n')
def show_arp(self, mac_end):
command = 'show arp | incl ' + mac_end
output = self.net_connect.send_command(command)
# self.net_connect.disconnect()
return output.split('\n')
def add_dhcp_client(self, config_list):
output = self.net_connect.send_config_set(config_list)
return output.split('\n')
def delete_dhcp_client(self, config_list):
output = self.net_connect.send_config_set(config_list)
return output.split('\n')
示例4: connect_net_device
def connect_net_device(**kwarg):
net_connect = ConnectHandler(**kwarg)
print "Current prompt: {}".format(net_connect.find_prompt())
show_run = net_connect.send_command("sh run")
print "sh run output: \n\n{}".format(show_run)
filename = net_connect.base_prompt + ".txt"
save_file (filename , show_run)
示例5: main
def main():
router1 = {
'device_type': 'cisco_ios',
'ip': '10.9.0.67',
'username': 'myuser',
'password': 'mypass'
}
router2 = {
'device_type': 'cisco_ios',
'ip': '10.63.176.57',
'username': 'myuser',
'password': 'mypass'
}
routers = [router1, router2]
for router in routers:
# Connect to device
device_conn = ConnectHandler(**router)
# Print arp table
print device_conn.send_command("show arp")
print "\n\n"
# Close connection
device_conn.disconnect()
示例6: main
def main():
"""
Write a Python script that retrieves all of the network devices from the database
Using this data, make a Netmiko connection to each device.
Retrieve 'show run'.
Record the time that the program takes to execute.
"""
start_time = datetime.now()
django.setup()
my_devices = NetworkDevice.objects.all()
print
for a_device in my_devices:
a_device_netmiko = create_netmiko_dict(a_device)
remote_conn = ConnectHandler(**a_device_netmiko)
if 'juniper' in a_device.device_type:
show_run = remote_conn.send_command_expect("show configuration")
else:
show_run = remote_conn.send_command_expect("show run")
print a_device.device_name
print '-' * 80
print show_run
print '-' * 80
print
print ">>>>>>>> Total Time: {}".format(datetime.now() - start_time)
print
示例7: main
def main():
for device in rtr_list:
rtr_conn = ConnectHandler(**device)
output = rtr_conn.send_command("show arp")
print device['ip'], " ARP Table:\n"
print "==========================================================="
print output, "\n"
示例8: main
def main():
"""Exercises using Netmiko"""
rtr1_pass = getpass("Enter router password: ")
sw1_pass = getpass("Enter switch password: ")
pynet_rtr1 = {
'device_type': 'cisco_ios',
'ip': '184.105.247.70',
'username': 'pyclass',
'password': rtr1_pass,
}
pynet_sw1 = {
'device_type': 'arista_eos',
'ip': '184.105.247.72',
'username': 'admin1',
'password': sw1_pass,
}
for a_device in (pynet_rtr1, pynet_sw1):
net_connect = ConnectHandler(**a_device)
print "Current Prompt: " + net_connect.find_prompt()
show_arp = net_connect.send_command("show arp")
print
print '#' * 80
print show_arp
print '#' * 80
print
show_run = net_connect.send_command("show run")
filename = net_connect.base_prompt + ".txt"
print "Save show run output: {}\n".format(filename)
save_file(filename, show_run)
示例9: main
def main():
django.setup()
start_time = datetime.now()
devices = NetworkDevice.objects.all()
credentials = Credentials.objects.all()
for a_device in devices:
print "\n", a_device
device_type = a_device.device_type
port = a_device.port
secret = ''
ip = a_device.ip_address
creds = a_device.credentials
username = creds.username
password = creds.password
remote_conn = ConnectHandler(device_type=device_type, ip=ip, username=username, password=password, port=port, secret=secret)
print "#" * 50
print remote_conn.send_command("show arp")
print "#" * 50
pass
elapsed_time = datetime.now() - start_time
print "Elapsed time: {}".format(elapsed_time)
示例10: main
def main():
#password = getpass()
password = '88newclass'
# Define libraries for devices we will connect to
pynet1 = {
'device_type': 'cisco_ios',
'ip': '50.76.53.27',
'username': 'pyclass',
'password': password,
'port': 22,
}
pynet2 = {
'device_type': 'cisco_ios',
'ip': '50.76.53.27',
'username': 'pyclass',
'password': password,
'port': 8022,
}
srx = {
'device_type': 'juniper',
'ip': '50.76.53.27',
'username': 'pyclass',
'password': password,
'port': 9822,
}
pynet_rtr2 = ConnectHandler(**pynet2)
pynet_rtr2.config_mode()
outp = pynet_rtr2.find_prompt()
print outp
outp2 = pynet_rtr2.check_config_mode()
print 'Config mode status is ' + str(outp2)
示例11: main
def main():
print 'Username:',
usern = raw_input()
passwd = getpass()
for i in range(len(DEVICES)):
try:
net_dev = {
'device_type':DEV_TYPE[i],
'ip':DEVICES[i],
'username':usern,
'password':passwd
}
print '>>>>>>>>>>>>> DEVICE: {} <<<<<<<<<<<<<<'.format(DEVICES[i])
# Initiating netmiko connection
net_connect = ConnectHandler(**net_dev)
resp = net_connect.find_prompt()
print resp
# Entering config mode, deploying commands and exiting config mode
resp = net_connect.send_config_set(CONF_COMMANDS)
print resp
except Exception as e:
print e
exit(0)
示例12: main
def main():
'''
Use Netmiko to change the logging buffer size (logging buffered <size>)
and to disable console logging (no logging console) from a file on
both pynet-rtr1 and pynet-rtr2
'''
ip_address = raw_input("Please enter IP address: ")
password = getpass()
pynet_rtr1['ip'] = ip_address
pynet_rtr2['ip'] = ip_address
pynet_rtr1['password'] = password
pynet_rtr2['password'] = password
#for each router load config from
#file and print result
for router in (pynet_rtr1, pynet_rtr2):
ssh_conn = ConnectHandler(verbose=False, **router)
ssh_conn.send_config_from_file('ex8_config.txt')
output = ssh_conn.send_command('show run | in logging')
print "\n>>> {}:{} \n".format(ssh_conn.ip, ssh_conn.port)
print output
print ">>>\n"
示例13: main
def main():
try:
hostname = raw_input("Enter remote host to test: ")
except NameError:
hostname = input("Enter remote host to test: ")
home_dir = path.expanduser("~")
key_file = "{}/.ssh/cisco_rsa".format(home_dir)
cisco_test = {
"ip": hostname,
"username": "testuser2",
"device_type": "cisco_ios",
"use_keys": True,
"key_file": key_file,
"verbose": False,
}
net_connect = ConnectHandler(**cisco_test)
print()
print("Checking prompt: ")
print(net_connect.find_prompt())
print()
print("Testing show ip int brief: ")
output = net_connect.send_command("show ip int brief")
print(output)
print()
示例14: main
def main():
device_list = [cisco_ios, cisco_xr, arista_veos]
start_time = datetime.now()
print
for a_device in device_list:
as_number = a_device.pop('as_number')
net_connect = ConnectHandler(**a_device)
net_connect.enable()
print "{}: {}".format(net_connect.device_type, net_connect.find_prompt())
if check_bgp(net_connect):
print "BGP currently configured"
remove_bgp_config(net_connect, as_number=as_number)
else:
print "No BGP"
# Check BGP is now gone
if check_bgp(net_connect):
raise ValueError("BGP configuration still detected")
# Construct file_name based on device_type
device_type = net_connect.device_type
file_name = 'bgp_' + device_type.split("_ssh")[0] + '.txt'
# Configure BGP
output = configure_bgp(net_connect, file_name)
print output
print
print "Time elapsed: {}\n".format(datetime.now() - start_time)
示例15: main
def main():
'''
Use Netmiko to connect to each of the devices in the database. Execute
'show version' on each device. Calculate the amount of time required to
do this.
'''
django.setup()
devices = NetworkDevice.objects.all()
start_time = datetime.now()
for a_device in devices:
creds = a_device.credentials
remote_conn = ConnectHandler(device_type=a_device.device_type,
ip=a_device.ip_address,
username=creds.username,
password=creds.password,
port=a_device.port, secret='')
print
print a_device
print '#' * 80
print remote_conn.send_command_expect("show version")
print '#' * 80
print
elapsed_time = datetime.now() - start_time
print "Elapsed time: {}".format(elapsed_time)