📜 ⬆️ ⬇️

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

In the Python application, the challenge was to get data from the network equipment and configure it remotely via SSH. You can use Paramiko, but you can not invent a bicycle and use the Exscript library based on it. Under the cut - code examples for connecting and receiving information from commands. Due to the lack of documentation for Exscript, this material can be useful to someone great.



To start importing the library (if not in the system - you can download it on the gita)

from Exscript import Account from Exscript.protocols import SSH2 

')
For Cisco, for example, we will ping Google, but nothing prevents us from using the commands for configuration:

 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 


The output will be:
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 there was a task that was struggling for a long time - the reluctance of Exscript to wait for the operation to complete, which takes time and sending confirmation. For example, when diagnosing pairs in a line, you had to confirm the interruption of the for a while service by pressing y and confirming with Enter, and then wait a couple of seconds for the test to complete. On this Exscript was hung up, by a trial and error method deduced the decision.

An example of working in the console when manually connected 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 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 this can be done in Exscript by limiting the random test on uplinks through a deduction in the Description of the To_Smth_Important_Device prohibition 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


Parsing output at its discretion.

Thanks for attention!

Source: https://habr.com/ru/post/283364/


All Articles