Python登录华为USG防火墙

#!/usr/bin/python
#-*- coding: utf-8 -*-

import paramiko
import os
import select
import sys
import tty
import termios

import sys

'''
....xshell..........................
................

'''
# ....socket
trans = paramiko.Transport(('IP地址', 22))
# .......
trans.start_client()

# ....rsa......
'''
default_key_file = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
prikey = paramiko.RSAKey.from_private_key_file(default_key_file)
trans.auth_publickey(username='super', key=prikey)
'''
# ............
trans.auth_password(username='admin', password='密码')
# ......
channel = trans.open_session()
# ....
channel.get_pty()
# .........................xshell......
channel.invoke_shell()

# .........
oldtty = termios.tcgetattr(sys.stdin)
try:
    # ........................,....tab.
    tty.setraw(sys.stdin)
    channel.settimeout(0)

    while True:
        readlist, writelist, errlist = select.select([channel, sys.stdin,], [], [])
        # ..........,sys.stdin....
        if sys.stdin in readlist:
            # ................1...
            input_cmd = sys.stdin.read(1)
            # .........
            channel.sendall(input_cmd)

        # ........,channel.......,.... select...
        if channel in readlist:
            # ....
            result = channel.recv(1024)
            # .......
            if len(result) == 0:
                print("\r\n**** EOF **** \r\n")
                break
            # .....
            sys.stdout.write(result.decode("GBK"))
            sys.stdout.flush()
finally:
    # ......................
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)

# ....
channel.close()
# ....
trans.close()

登录防火墙测试:
 [root@nginx test]# python xx.py 

***********************************************************
*           All rights reserved 2014-2015                 *
*       Without the owner's prior written consent,        *
* no decompiling or reverse-engineering shall be allowed. *
* Notice:                                                 *
*      This is a private communication system.            *
*   Unauthorized access or use may lead to prosecution.   *
***********************************************************

Note: The max number of VTY users is 5, and the current number
      of VTY users on line is 2.  
  ----------------------------------------------------------------------------  
  User last login information:  
  ----------------------------------------------------------------------------  
  Access Type: SSH  
  IP-Address : 117.158.204.49  
  Time       : 2019-01-13 12:49:55 +00:00  
  State      : Login Succeeded  
  ----------------------------------------------------------------------------
<XinXiang_FW>
Info: The Valid period of password less than 10 days.
<XinXiang_FW>
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注