paramiko 和 pexpect在笔记里被跳过
因为,他们都可以被netmiko所取代,不想在他们身上浪费太多时间
补一个地方就是,如果用paramiko遇到了connection莫名自己关闭的情况,参考下面的代码
def create_a_conn(ip_addr, port, username, password):
'''
creat a conn to router using paramiko.SSHClient()
'''
conn_session = paramiko.SSHClient()
conn_session.load_system_host_keys()
conn_session.connect(ip_addr, port, username, password, look_for_keys = Fals
e, allow_agent = False)
conn = conn_session.invoke_shell()# to keep the session go on
conn.keep_this = conn_session
time.sleep(1)
conn.send("terminal length 0\n")
time.sleep(1)
if conn.recv_ready():
conn.recv(65535)
return conn
注意conn.keep_this = conn_session 这一句是解决connection莫名自己关闭的情况的关键
没有这一句会报如下错误
File "/home/hsun/applied_python/local/lib/python2.7/site-packages/paramiko/channel.py", line 715, in send
return self._send(s, m)
File "/home/hsun/applied_python/local/lib/python2.7/site-packages/paramiko/channel.py", line 1075, in _send
raise socket.error('Socket is closed')
socket.error: Socket is closed
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.247.70',
8 'username': 'pyclass',
9 'password': '88newclass',
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进去的,它的优点是可以替你跳过configure terminal这一步,并且数组里可以放多个命令,如下面的这个config_commands数组
但是要注意的是,它不会替你write,如果想write,还需要再加一条命令
18 config_commands = ['logging buffered 19999', 'no logging console']
19 outp = conn1.send_config_set(config_commands)
20 print outp
连接juniper设备
以srx为例
21
22 srx = {
23 'device_type': 'juniper',
24 'ip': '184.105.247.76',
25 'username': 'pyclass',
26 'port': 22,
27 'password': '88newclass',
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()