本文整理汇总了Python中netmiko.ConnectHandler.send_command方法的典型用法代码示例。如果您正苦于以下问题:Python ConnectHandler.send_command方法的具体用法?Python ConnectHandler.send_command怎么用?Python ConnectHandler.send_command使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netmiko.ConnectHandler
的用法示例。
在下文中一共展示了ConnectHandler.send_command方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def main():
router = {
'device_type': 'cisco_ios',
'ip': '10.9.0.67',
'username': 'myuser',
'password': 'mypass'
}
# Connect to device
device_conn = ConnectHandler(**router)
# Show logging buffered
print "Pre Change Logging Check:"
print device_conn.send_command("show run | in logging")
# To change the logging buffered value
config_commands = ['logging buffered 25000', 'do wr mem']
output = device_conn.send_config_set(config_commands)
print output
# Show logging buffered
print "Post Change Logging Check:"
print device_conn.send_command("show run | in logging")
# Close connection
device_conn.disconnect()
示例2: clean_unused_files
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def clean_unused_files(self):
# Initiate SSH Connection to device
device = {'device_type': 'cisco_ios',
'ip': self.ip_address,
'username': self.username,
'password': self.password}
ssh_conn = ConnectHandler(**device)
# Loop through the inactive images of the device and attempt to delete.
for image in self.inactive_images:
# Perform a dir to determine if the image exists, to avoid an "image not found" command error.
dir_output = ssh_conn.send_command('dir')
if image in dir_output and image != self.compliant_image:
try:
# Force delete the inactive image from flash. i.e. 'del /force flash:/image.bin'. Allows for different file systems (flash:, slot0:, etc)
ssh_conn.send_command('del /force {}/{}'.format(self.filesystem, image))
print "Deleted image file %s from %s" % (image, self.hostname)
self.inactive_images.remove(image)
except Exception as e:
print "Error - %s" % e
else:
# If file no longer exists (perhaps deleted manually), update the inactive_images list.
print "Image %s not found or filename matches the compliant image for model %s, skipping...." % (image,self.model)
self.inactive_images.remove(image)
ssh_conn.disconnect()
示例3: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def main():
django.setup()
# Load device info and credentials into database
ex1_link_obj_2_credentials.link_device_to_credentials()
devices = NetworkDevice.objects.all()
for a_device in devices:
if a_device.device_name and a_device.credentials:
start_time = datetime.now()
creds = a_device.credentials
username = creds.username
password = creds.password
remote_conn = ConnectHandler(device_type=a_device.device_type,
ip=a_device.ip_address,
username=username,
password=password,
port=a_device.port,
secret='')
# Print out 'show version' output
print
print '#' * 80
print ("'show version' output for device: %s" % a_device.device_name)
print '#' * 80
print remote_conn.send_command("show version")
# Print out elapsed time
print '#' * 80
print ("Elapsed time: " + str(datetime.now() - start_time))
print '#' * 80
示例4: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
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,
}
juniper_srx = {
"device_type": "juniper",
"ip": "50.76.53.27",
"username": "pyclass",
"password": password,
"port": 9822,
}
rtr1 = ConnectHandler(**pynet1)
rtr2 = ConnectHandler(**pynet2)
srx = ConnectHandler(**juniper_srx)
outp = rtr1.send_command("show arp")
print "rtr1 show arp:"
print outp
outp = rtr2.send_command("show arp")
print "rtr2 show arp:"
print outp
outp = srx.send_command("show arp")
print "srx show arp:"
print outp
示例5: setup_module
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def setup_module(module):
module.EXPECTED_RESPONSES = {
'enable_prompt' : 'n7k1#',
'base_prompt' : 'n7k1',
'interface_ip' : '10.3.3.245',
'config_mode' : '(config)'
}
show_ver_command = 'show version'
module.basic_command = 'show ip int brief'
net_connect = ConnectHandler(**cisco_nxos)
module.show_version = net_connect.send_command(show_ver_command)
module.show_ip = net_connect.send_command(module.basic_command)
net_connect.enable()
module.enable_prompt = net_connect.find_prompt()
module.config_mode = net_connect.config_mode()
config_commands = ['logging monitor 3', 'logging monitor 7', 'no logging console']
net_connect.send_config_set(config_commands)
module.exit_config_mode = net_connect.exit_config_mode()
module.config_commands_output = net_connect.send_command("show run | inc 'logging monitor'")
net_connect.disconnect()
示例6: __init__
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
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')
示例7: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
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)
示例8: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
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()
示例9: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def main():
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 curr_device in (pynet_rtr1, pynet_sw1):
sw_con = ConnectHandler(**curr_device)
show_ver = sw_con.send_command("show version")
show_run = sw_con.send_command("show run")
save_file(sw_con.base_prompt + '-ver', show_ver)
save_file(sw_con.base_prompt + '-run', show_run)
示例10: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
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)
示例11: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def main():
'''
SCP transfer cisco_logging.txt to network device
Use ssh_conn as ssh channel into network device
scp_conn must be closed after file transfer
'''
ssh_conn = ConnectHandler(**cisco_881)
scp_conn = SCPConn(ssh_conn)
s_file = 'cisco_logging.txt'
d_file = 'cisco_logging.txt'
print "\n\n"
scp_conn.scp_transfer_file(s_file, d_file)
scp_conn.close()
output = ssh_conn.send_command("show flash: | inc cisco_logging")
print ">> " + output + '\n'
# Disable file copy confirmation
output = ssh_conn.send_config_set(["file prompt quiet"])
# Execute config merge
print "Performing config merge\n"
output = ssh_conn.send_command("copy flash:cisco_logging.txt running-config")
# Verify change
print "Verifying logging buffer change"
output = ssh_conn.send_command("show run | inc logging buffer")
print ">> " + output + '\n'
# Restore copy confirmation
output = ssh_conn.send_config_set(["file prompt alert"])
示例12: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def main():
# Definition of rtr2.
rtr2 = {
'device_type': 'cisco_ios',
'ip': '50.76.53.27',
'username': 'pyclass',
'password': '88newclass',
'port': 8022,
}
# Log into router.
net_connect = ConnectHandler(**rtr2)
# Check current logging buffer size.
output = net_connect.send_command("show run | inc buffered")
print
print "Initial logging buffer size: " + output
# Enter config mode, change logging buffer size, exit config mode.
output = net_connect.config_mode()
output = net_connect.send_command("logging buffer 64000")
output = net_connect.exit_config_mode()
# Check logging buffer size again.
output = net_connect.send_command("show run | inc buffered")
print "Final logging buffer size: " + output
print
示例13: show_version
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def show_version(a_device):
remote_conn=ConnectHandler(device_type=a_device.device_type,ip=a_device.ip_address,username=a_device.credentials.username,password=a_device.credentials.password,port=a_device.port,secret='')
print '\n'
print '#' * 80
print remote_conn.send_command("show version")
print '#' * 80
print '\n'
示例14: main
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def main():
ip_addr = "50.76.53.27"
port_rtr1 = 22
port_rtr2 = 8022
port_srx = 9822
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}
juniper_srx = {
"device_type": "juniper",
"ip": ip_addr,
"username": username,
"password": password,
"port": port_srx,
}
pynet_rtr1 = ConnectHandler(**pynet1)
pynet_rtr2 = ConnectHandler(**pynet2)
pynet_srx = ConnectHandler(**juniper_srx)
out1 = pynet_rtr1.send_command("show arp")
out2 = pynet_rtr2.send_command("show arp")
out3 = pynet_srx.send_command("show arp")
print "The output for rtr1 is:\n%s\n" % out1
print "The output for rtr2 is:\n%s\n" % out2
print "The output for srx is:\n%s\n" % out3
示例15: setup_module
# 需要导入模块: from netmiko import ConnectHandler [as 别名]
# 或者: from netmiko.ConnectHandler import send_command [as 别名]
def setup_module(module):
module.EXPECTED_RESPONSES = {
'enable_prompt' : 'xe-test-rtr#',
'base_prompt' : 'xe-test-rtr',
'interface_ip' : '172.30.0.167',
'config_mode' : '(config)',
}
show_ver_command = 'show version'
module.basic_command = 'show ip int brief'
net_connect = ConnectHandler(**cisco_xe)
module.show_version = net_connect.send_command(show_ver_command)
module.show_ip = net_connect.send_command(module.basic_command)
net_connect.enable()
module.enable_prompt = net_connect.find_prompt()
module.config_mode = net_connect.config_mode()
config_commands = ['logging buffered 20000', 'logging buffered 20010', 'no logging console']
net_connect.send_config_set(config_commands)
module.exit_config_mode = net_connect.exit_config_mode()
module.config_commands_output = net_connect.send_command('show run | inc logging buffer')
net_connect.disconnect()