netmiko & huawei

from netmiko import ConnectHandler
import logging
logging.basicConfig(filename="test.log", level=logging.DEBUG)
huawei = {'device_type': 'huawei', 'ip': '10.10.30.253', 'username': 'root@default', 'password': 'Admin123_.','port': 22}
#huawei = {'device_type': 'huawei_vrpv8', 'ip': '10.10.30.253', 'username': 'root@default', 'password': 'Admin123_.','port': 22}
net_connect = ConnectHandler(**huawei)
sshConfirm = net_connect.find_prompt()
print('login ' + sshConfirm)
#current_view = net_connect.find_prompt()
#print(current_view)
commands = ['dis arp']
output = net_connect.send_config_set(commands)
## not support commit
#output += net_connect.commit()
print(output)
net_connect.disconnect()
[root@bogon ironic]#

[root@bogon ironic]# cat test.log 
DEBUG:paramiko.transport:starting thread (client mode): 0x8f9c0290L
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0--
INFO:paramiko.transport:Connected (version 2.0, client -)
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'ecdh-sha2-nistp521', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp256', u'diffie-hellman-group-exchange-sha1', u'sm2kep-sha2-nistp256'] server key:[u'ssh-dss', u'ssh-rsa', u'ecdsa-sha2-nistp521'] client encrypt:[u'aes256-ctr', u'aes128-ctr'] server encrypt:[u'aes256-ctr', u'aes128-ctr'] client mac:[u'hmac-sha2-256', u'hmac-sha2-256-96', u'hmac-sha1-96'] server mac:[u'hmac-sha2-256', u'hmac-sha2-256-96', u'hmac-sha1-96'] client compress:[u'none', u'zlib'] server compress:[u'none', u'zlib'] client lang:[u''] server lang:[u''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: ecdh-sha2-nistp256
DEBUG:paramiko.transport:HostKey agreed: ecdsa-sha2-nistp521
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ecdsa-sha2-nistp521 host key for 10.10.30.253: b6d87e3a5fee634c324c9699dcb14012
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
DEBUG:netmiko:read_channel: 
Warning: The initial password poses security risks.
The password needs to be changed. Change now? [Y/N]:
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:In set_base_prompt
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: 

DEBUG:netmiko:read_channel: 
Error: Please choose 'YES' or 'NO' first before pressing 'Enter'. [Y/N]:
DEBUG:netmiko:write_channel: 

DEBUG:netmiko:read_channel: 
Error: Please choose 'YES' or 'NO' first before pressing 'Enter'. [Y/N]:
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:read_channel: 
DEBUG:netmiko:write_channel: return

DEBUG:netmiko:Pattern is: >
DEBUG:netmiko:write_channel: exit

DEBUG:paramiko.transport:EOF in transport thread
[root@bogon ironic]# 
67         if "[Y/N]" in prompt:
 68             log.debug("First time logging in")
 69             self.write_channel("N"+self.RETURN)
 70             return self.base_prompt

[root@bogon good]# vi huawei_vlan.py 
  1 from netmiko import ConnectHandler
  2 import logging
  3 logging.basicConfig(filename="test.log", level=logging.DEBUG)
  4 huawei = {'device_type': 'huawei', 'ip': '10.10.30.253', 'username': 'root@default', 'password': 'Admin123_.',    'port': 22}
  5 #huawei = {'device_type': 'huawei_vrpv8', 'ip': '10.10.30.253', 'username': 'root@default', 'password':           'Admin123_.','port': 22}
  6 net_connect = ConnectHandler(**huawei)
  7 sshConfirm = net_connect.find_prompt()
  8 print('login ' + sshConfirm)
  9 #current_view = net_connect.find_prompt()
 10 #print(current_view)
 11 #commands = ['system-view']
 12 #output = net_connect.send_config_set(commands)
 13 commands = ['int 100GE1/0/14', 'port default vlan 1', 'commit']
 14 output = net_connect.send_config_set(commands)
 15 #commands = ['port vlan 1030']
 16 #output = net_connect.send_config_set(commands)
 17 #commands = ['quit']
 18 #output = net_connect.send_config_set(commands)
 19 #commands = ['commit']
 20 #output = net_connect.send_config_set(commands)
 21 #commands = ['return']
 22 #output = net_connect.send_config_set(commands)
 23 ## not support commit
 24 #output += net_connect.commit()
 25 print(output)
 26 net_connect.disconnect()
1、不要systemviwer
2、 命令不能分开写

错误日志

[~JG05-SA-01-CE8850-100GE1/0/14]undo port default vlan 1^M
77526                                           ^^M
77527 Error: Unrecognized command found at '^' position.^M
80493 Enter system view, return user view with return command.
80494 [~JG05-SA-01-CE8850]interface 100GE1/0/14
80495 [~JG05-SA-01-CE8850-100GE1/0/14]undo port default vlan 1
80496 [~JG05-SA-01-CE8850-100GE1/0/14]commit
80497 [~JG05-SA-01-CE8850-100GE1/0/14]return

原文地址:https://www.cnblogs.com/dream397/p/13052506.html