from rich import print
import time
import paramiko
class ConnectSSH:
def __init__(self, ip, username, password, disable_paging=True):
print('Calling __init__')
self.ip = ip
self.username = username
self.password = password
self._MAX_READ = 9999
_client = paramiko.SSHClient()
_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
_client.connect(
hostname=ip,
username=username,
password=password,
look_for_keys=False,
allow_agent=False
)
self._ssh = _client.invoke_shell()
# if disable_paging:
# self._ssh.send('screen-length 0 temporary\n')
# self._ssh.send('system-view\n')
time.sleep(1)
self._ssh.recv(self._MAX_READ).decode()
def __enter__(self):
print("Calling __enter__")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Calling __exit__")
self._ssh.close()
def close(self):
self._ssh.close()
def send_display_command(self, command):
self._ssh.send(command + '\n')
time.sleep(1)
result = self._ssh.recv(self._MAX_READ).decode()
return result
def send_config_command(self, commands):
if isinstance(commands, str):
commands = [commands]
for command in commands:
print(f'发送命令:{command}')
self._ssh.send(command + '\n')
time.sleep(0.5)
result = self._ssh.recv(self._MAX_READ).decode()
return result
class HuaweiSSH(ConnectSSH):
def __init__(self, ip, username, password, disable_paging=True):
super().__init__(ip, username, password)
if disable_paging:
self._ssh.send('screen-length 0 temporary\n')
time.sleep(1)
self._ssh.recv(self._MAX_READ).decode()
def system_view(self):
# 进入配置模式
self._ssh.send('system-view\n')
time.sleep(0.5)
result = self._ssh.recv(self._MAX_READ).decode()
return result
def exit_system_view(self):
# 退出配置模式
self._ssh.send('quit\n')
time.sleep(0.5)
result = self._ssh.recv(self._MAX_READ).decode()
return result
def send_config_command(self, commands):
result = self.system_view()
result += super().send_config_command(commands)
result += self.exit_system_view()
return result
def send_save_config(self):
commands = ['save', 'Y']
result = super().send_config_command(commands)
return result
snmp_config_script = '''snmp-agent
acl number 2000
rule 1 permit source 1.0.0.1 0
rule 100 deny
snmp-agent sys-info version v2c v3
snmp-agent community read cipher zxyy@456 acl 2000
snmp-agent community write cipher zxyy@123 acl 2000
# snmp-agent group v3 dc-admin
snmp-agent usm-user v3 admin dc-admin
snmp-agent usm-user v3 admin dc-admin authentication-mode sha zxyylj5tgb3edc privacy-mode des56 zxyylj5tgb3edc acl 2000
snmp-agent mib-view included nt iso
snmp-agent mib-view included rd iso
snmp-agent mib-view included wt iso
snmp-agent mib-view included iso-view iso
snmp-agent group v3 dc-admin privacy read-view rd write-view wt notify-view nt
snmp-agent inform timeout 5'''.split('\n')
with HuaweiSSH('10.1.0.2', 'admin', 'admin@123') as fw1:
print(fw1.send_display_command('disp ospf peer brief'))
print(fw1.send_display_command('disp ospf routing'))
print(fw1.send_display_command('disp current'))
print(fw1.send_config_command(snmp_config_script))
print(fw1.send_save_config())
[+] 输出:
Calling __init__
Calling __enter__
disp ospf peer brief
2022-11-07 09:49:34.190
OSPF Process 1 with Router ID 10.1.0.2
Peer Statistic Information
----------------------------------------------------------------------------
Area Id Interface Neighbor id State
0.0.0.2 GigabitEthernet1/0/0 10.0.1.1 Full
----------------------------------------------------------------------------
Total Peer(s): 1
<USG6000V1>
disp ospf routing
2022-11-07 09:49:35.200
OSPF Process 1 with Router ID 10.1.0.2
Routing Tables
Routing for Network
Destination Cost Type NextHop AdvRouter Area
10.1.0.0/24 1 Transit 10.1.0.2 10.1.0.2 0.0.0.2
10.0.1.0/24 1 Stub 10.1.0.1 10.0.1.1 0.0.0.2
10.0.2.0/24 21 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
10.0.3.0/24 21 Stub 10.1.0.1 10.0.3.3 0.0.0.2
10.0.4.0/24 22 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
10.0.5.0/24 23 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
10.0.24.0/24 22 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
10.0.45.0/24 23 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
10.0.123.0/24 21 Transit 10.1.0.1 10.0.3.3 0.0.0.2
10.3.0.0/24 21 Stub 10.1.0.1 10.0.3.3 0.0.0.2
10.3.1.0/24 21 Stub 10.1.0.1 10.0.3.3 0.0.0.2
10.5.0.0/24 23 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
10.5.1.0/24 23 Inter-area 10.1.0.1 10.0.2.2 0.0.0.2
Total Nets: 13
Intra Area: 6 Inter Area: 7 ASE: 0 NSSA: 0
<USG6000V1>
disp current
2022-11-07 09:49:36.200
!Software Version V500R005C10SPC300
#
sysname USG6000V1
#
l2tp domain suffix-separator @
#
ipsec sha2 compatible enable
#
undo telnet server enable
undo telnet ipv6 server enable
#
update schedule location-sdb weekly Sun 04:37
#
firewall defend action discard
#
lldp enable
#
banner enable
#
user-manage web-authentication security port 8887
undo privacy-statement english
undo privacy-statement chinese
page-setting
user-manage security version tlsv1.1 tlsv1.2
password-policy
level high
user-manage single-sign-on ad
user-manage single-sign-on tsm
user-manage single-sign-on radius
user-manage auto-sync online-user
#
web-manager security version tlsv1.1 tlsv1.2
web-manager enable
web-manager security enable
undo web-manager config-guide enable
#
firewall dataplane to manageplane application-apperceive default-action drop
#
undo ips log merge enable
#
decoding uri-cache disable
#
update schedule ips-sdb daily 06:48
update schedule av-sdb daily 06:48
update schedule sa-sdb daily 06:48
update schedule cnc daily 06:48
update schedule file-reputation daily 06:48
#
ip vpn-instance default
ipv4-family
#
time-range worktime
period-range 08:00:00 to 18:00:00 working-day
#
acl number 2000
rule 1 permit source 1.0.0.1 0
rule 100 deny
#
ike proposal default
encryption-algorithm aes-256 aes-192 aes-128
dh group14
authentication-algorithm sha2-512 sha2-384 sha2-256
authentication-method pre-share
integrity-algorithm hmac-sha2-256
prf hmac-sha2-256
#
aaa
authentication-scheme default
authentication-scheme admin_local
authentication-scheme admin_radius_local
authentication-scheme admin_hwtacacs_local
authentication-scheme admin_ad_local
authentication-scheme admin_ldap_local
authentication-scheme admin_radius
authentication-scheme admin_hwtacacs
authentication-scheme admin_ad
authorization-scheme default
accounting-scheme default
domain default
service-type internetaccess ssl-vpn l2tp ike
internet-access mode password
reference user current-domain
manager-user audit-admin
password cipher @%@%y2=m'G2v]BAyHP5*:dEGmQaNMt3g)ajyVEuaO9AL!Wt5QaQm@%@%
service-type web terminal
level 15
manager-user api-admin
password cipher @%@%.]'sQT/_8(j9+s;Y/l0"m|c=Pz4UNos^O$KWm"5Hj~N<|c@m@%@%
level 15
manager-user admin
password cipher @%@%TCrDV1kkNR+XQ=JQCCDB<<K;_\>O9v`jeVfJd&6WJ6F0<K><@%@%
service-type web terminal ssh
level 15
role system-admin
role device-admin
role device-admin(monitor)
role audit-admin
bind manager-user audit-admin role audit-admin
bind manager-user admin role system-admin
#
l2tp-group default-lns
#
interface GigabitEthernet0/0/0
undo shutdown
ip binding vpn-instance default
ip address 192.168.0.1 255.255.255.0
alias GE0/METH
#
interface GigabitEthernet1/0/0
undo shutdown
ip address 10.1.0.2 255.255.255.0
service-manage http permit
service-manage https permit
service-manage ping permit
service-manage ssh permit
service-manage snmp permit
service-manage telnet permit
#
interface GigabitEthernet1/0/1
undo shutdown
#
interface GigabitEthernet1/0/2
undo shutdown
#
interface GigabitEthernet1/0/3
undo shutdown
#
interface GigabitEthernet1/0/4
undo shutdown
#
interface GigabitEthernet1/0/5
undo shutdown
#
interface GigabitEthernet1/0/6
undo shutdown
ip address 192.168.208.111 255.255.255.0
service-manage http permit
service-manage https permit
service-manage ping permit
service-manage ssh permit
service-manage snmp permit
service-manage telnet permit
#
interface Virtual-if0
#
interface NULL0
#
firewall zone local
set priority 100
#
firewall zone trust
set priority 85
add interface GigabitEthernet0/0/0
add interface GigabitEthernet1/0/0
#
firewall zone untrust
set priority 5
add interface GigabitEthernet1/0/6
#
firewall zone dmz
set priority 50
#
ospf 1 router-id 10.1.0.2
default-route-advertise always
spf-schedule-interval millisecond 5000
area 0.0.0.2
network 10.1.0.0 0.0.0.255
#
ip route-static 0.0.0.0 0.0.0.0 192.168.208.2
#
snmp-agent
snmp-agent local-engineid 800007DB0300E0FCAE2949
snmp-agent community read cipher %^%#YlQ"4IEC@I{QYvYf@4z/nh38.9KO5Za<1,-W8@%B2%bJ-)xZ@HhmGrQ&9`H@b.@=~7@Ad2&Rb1GFLjE5%^%# acl 2000
snmp-agent community write cipher %^%#o4(h6p"~>G^-U}B&fTO5f]IpNa;$SUM`4o4vY<+<=}/*,1--jIhA@49"]gGGbKRCE4|`Q7lu)i2-q;("%^%# acl 2000
snmp-agent sys-info version v2c v3
snmp-agent group v3 dc-admin privacy read-view rd write-view wt notify-view nt
snmp-agent mib-view included nt iso
snmp-agent mib-view included rd iso
snmp-agent mib-view included wt iso
snmp-agent mib-view included iso-view iso
snmp-agent usm-user v3 admin
snmp-agent usm-user v3 admin group dc-admin
snmp-agent usm-user v3 admin authentication-mode sha cipher %^%#*p+<01EJsN.Q}]/*V3:/"7_<TD:fcSaG3/OW6Z+K%^%#
snmp-agent usm-user v3 admin privacy-mode des56 cipher %^%#j/aT6E<s#D%~aaH9\@`0"7_<TD:fcSaG3/OW6Z+K%^%#
snmp-agent usm-user v3 admin acl 2000
snmp-agent inform timeout 5
#
undo ssh server compatible-ssh1x enable
stelnet server enable
ssh authentication-type default password
ssh user admin
ssh user admin authentication-type all
ssh user admin service-type all
ssh server cipher aes256_ctr aes128_ctr
ssh server hmac sha2_256 sha1
ssh client cipher aes256_ctr aes128_ctr
ssh client hmac sha2_256 sha1
#
firewall detect ftp
#
user-interface con 0
authentication-mode aaa
user-interface vty 0 4
authentication-mode aaa
protocol inbound ssh
user-interface vty 16 20
#
pki realm default
#
sa
#
location
#
multi-linkif
mode proportion-of-weight
#
right-manager server-group
#
device-classification
device-group pc
device-group mobile-terminal
device-group undefined-group
#
user-manage server-sync tsm
#
security-policy
default action permit
#
auth-policy
#
traffic-policy
#
policy-based-route
#
nat-policy
rule name NAT
source-zone trust
destination-zone untrust
action source-nat easy-ip
#
quota-policy
#
pcp-policy
#
dns-transparent-policy
#
rightm-policy
#
return
<USG6000V1>
发送命令:snmp-agent
发送命令:acl number 2000
发送命令: rule 1 permit source 1.0.0.1 0
发送命令: rule 100 deny
发送命令:snmp-agent sys-info version v2c v3
发送命令:snmp-agent community read cipher zxyy@456 acl 2000
发送命令:snmp-agent community write cipher zxyy@123 acl 2000
发送命令:# snmp-agent group v3 dc-admin
发送命令:snmp-agent usm-user v3 admin dc-admin
发送命令:snmp-agent usm-user v3 admin dc-admin authentication-mode sha zxyylj5tgb3edc privacy-mode des56 zxyylj5tgb3edc acl 2000
发送命令:snmp-agent mib-view included nt iso
发送命令:snmp-agent mib-view included rd iso
发送命令:snmp-agent mib-view included wt iso
发送命令:snmp-agent mib-view included iso-view iso
发送命令:snmp-agent group v3 dc-admin privacy read-view rd write-view wt notify-view nt
发送命令:snmp-agent inform timeout 5
system-view
Enter system view, return user view with Ctrl+Z.
[USG6000V1]snmp-agent
[USG6000V1]acl number 2000
[USG6000V1-acl-basic-2000] rule 1 permit source 1.0.0.1 0
Warning: The rule already exists. Are you sure to update? [Y/N]:n
[USG6000V1-acl-basic-2000]snmp-agent sys-info version v2c v3
Warning: SNMPv1/SNMPv2c is not secure, and it is recommended to use SNMPv3.
[USG6000V1]snmp-agent community read cipher zxyy@456 acl 2000
[USG6000V1]snmp-agent community write cipher zxyy@123 acl 2000
[USG6000V1]# snmp-agent group v3 dc-admin
[USG6000V1]snmp-agent usm-user v3 admin dc-admin
Warning: The privacy and authentication passwords are the same, which is insecure. It is recommended that the privacy and authentication passwords be different.
[USG6000V1]snmp-agent usm-user v3 admin dc-admin authentication-mode sha zxyylj5 [1Dtgb3edc privacy-mode des56 zxyylj5tgb3edc acl 2000
Warning: The privacy and authentication passwords are the same, which is insecure. It is recommended that the privacy and authentication passwords be different.
Warning: DES56 is less secure, and it is recommended to use AES128 or higher.
[USG6000V1]snmp-agent mib-view included nt iso
[USG6000V1]snmp-agent mib-view included rd iso
[USG6000V1]snmp-agent mib-view included wt iso
[USG6000V1]snmp-agent mib-view included iso-view iso
[USG6000V1]snmp-agent group v3 dc-admin privacy read-view rd write-view wt notif [1Dy-view nt
[USG6000V1]snmp-agent inform timeout 5
[USG6000V1]quit
<USG6000V1>
发送命令:save
发送命令:Y
save
The current configuration will be written to hda1:/vrpcfg.cfg.
Are you sure to continue?[Y/N]Y
Now saving the current configuration to the slot 0.
Save the configuration successfully.
<USG6000V1>
Calling __exit__