首页 > 代码库 > netmiko初窥

netmiko初窥

paramiko 和 pexpect在笔记里被跳过

因为,他们都可以被netmiko所取代,不想在他们身上浪费太多时间

netmiko

总体来讲还是很简单的,因为比较人性化

  • 创建conn
  1 #!/usr/bin/env python  2  3 from netmiko import ConnectHandler  4  5 pynet1 = {  6 ‘device_type‘: ‘cisco_ios‘,  7 ‘ip‘: ‘184.105.xx.xx‘,  8 ‘username‘: ‘pyclass‘,  9 ‘password‘: ‘******‘, 10 } 11 12 conn1 = ConnectHandler(**pynet1) 13 outp = conn1.send_command("show run | inc logging") 14 print outp

就这么简单就可以输出了

要注意它的ConnectHandler括号里的俩星号,标示用的,不能少

  • 利用dir()探索一个全新的library
>>> dir(netmiko)[‘ConnectHandler‘, ‘FileTransfer‘, ‘NetMikoAuthenticationException‘, ‘NetMikoTimeoutException‘, ‘NetmikoAuthError‘, ‘NetmikoTimeoutError‘, ‘SCPConn‘, ‘__all__‘, ‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘__path__‘, ‘__version__‘, ‘a10‘, ‘alcatel‘, ‘arista‘, ‘avaya‘, ‘base_connection‘, ‘brocade‘, ‘cisco‘, ‘dell‘, ‘enterasys‘, ‘extreme‘, ‘f5‘, ‘fortinet‘, ‘hp‘, ‘huawei‘, ‘juniper‘, ‘linux‘, ‘netmiko_globals‘, ‘ovs‘, ‘paloalto‘, ‘platforms‘, ‘quanta‘, ‘scp_handler‘, ‘ssh_connection‘, ‘ssh_dispatcher‘, ‘ssh_exception‘]>>>
>>> dir(netmiko.ConnectHandler)[‘__call__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__delattr__‘, ‘__dict__‘, ‘__doc__‘, ‘__format__‘, ‘__get__‘, ‘__getattribute__‘, ‘__globals__‘, ‘__hash__‘, ‘__init__‘, ‘__module__‘, ‘__name__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘func_closure‘, ‘func_code‘, ‘func_defaults‘, ‘func_dict‘, ‘func_doc‘, ‘func_globals‘, ‘func_name‘]>>>>>>
  • find_prompt()
 print conn1.find_prompt()
  • send_config_set()
    命令是以数组的形式pass进去的
 18 config_commands = [‘logging buffered 19999‘] 19 outp = conn1.send_config_set(config_commands) 20 print outp
  • 连接juniper设备

以srx为例

 21 22 srx = { 23 ‘device_type‘: ‘juniper‘, 24 ‘ip‘: ‘184.105.xx.xx‘, 25 ‘username‘: ‘pyclass‘, 26 ‘port‘: 22, 27 ‘password‘: ‘******‘, 28 } 29 30 conn2 = ConnectHandler(**srx) 31 outp = conn2.send_command("show arp") 32 print outp 33 34 print dir(conn2) 35 print dir(conn1)

可以看到conn1 conn2这俩connection的命名空间是不太一样的

注意看config_mode()和check_config_mode()

  • config_mode() and check_config_mode()
    check_config_mode()返回布尔值, True就说明现在正在config mode

config_mode()进入cofig mode
exit_config_mode退出config mode

  • commit()

这个就是juniper的commit

 conn2.commit()
?

netmiko初窥