Using python Exscript library to work with Cisco and Huawei equipment via SSH

To start, we import the library (if not in the system, you can download it on the git)
from Exscript import Account
from Exscript.protocols import SSH2
For Cisco, for example, we ping Google, but nothing prevents us from using the commands to configure:
acc = Account('USERNAMESSHAAA', 'PASS')
con = SSH2()
con.connect('ROUTER_IP')
con.login(acc)
con.execute('terminal length 0')
con.execute('ping vrf INTERNET 8.8.8.8')
con.send('exit')
output = con.response
Output will:
ping vrf INTERNET 8.8.8.8
Wed May 11 19: 13: 25.551 FET
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 8.8.8.8, timeout is 2 seconds:
!!!
Success rate is 100 percent (5/5), round-trip min / avg / max = 20/20/21 ms
RP / 2 / RSP2 / CPU1: Msk-1-sr9000 #
It was on Huawei that a task arose that it struggled for a long time - Exscript’s reluctance to wait for an operation to complete, requiring time and sending confirmation. For example, when diagnosing pairs in a line, it was necessary to confirm the interruption of the for a while service by pressing y and confirming by Enter, and then wait a couple of seconds until the test is completed. On this, Exscript was hung up, and it brought out a solution by trial and error.
An example of working in the console when manually connecting to the switch to perform this task:
sys
Enter system view, return user view with Ctrl + Z.
[SW] int g0 /
0/5 [SW-GigabitEthernet0 / 0/5] v
Warning: The command will stop service for a while, continue? [Y / N]: y
Pair A length: 56meter (s)
Pair B length : 56meter (s)
Pair C length: 56meter (s)
Pair D length: 56meter (s)
Pair A state: Open
Pair B state: Open
Pair C state: Open
Pair D state: Open
[SW-GigabitEthernet0 / 0/5]
And here you can do in Exscript, limiting a random test on uplinks through a deduction in the Description of the To_Smth_Important_Device ban trigger:
acc = Account('USERNAMESSHAAA', 'PASS')
con = SSH2()
con.connect('ROUTER_IP')
con.login(acc)
con.execute('screen-length 0 temporary')
con.execute('sys')
con.execute('interface GigabitEthernet0/0/2')
con.execute('disp th')
upck = ''
upck = con.response
if 'To' not in upck:
con.set_prompt(r'Y')
con.execute('vi')
con.set_prompt('Pair D state.{0,20}')
con.execute('Y')
output = con.response
else:
output = 'UPLINK DETECTED! TEST ON UPLINKS RESTRICTED!'
con.send('quit\r')
con.send('quit\r')
con.send('quit\r')
In Output:
/ N]: Y
Info: This operation may take a few seconds. Please wait for a moment ........ done.
Pair A length: 56meter (s)
Pair B length: 56meter (s)
Pair C length: 56meter (s)
Pair D length: 56meter (s)
Pair A state: Open
Pair B state: Open
Pair C state: Open
Pair D state : Open
You can parse the output at your discretion.
Thanks for attention!