This commit is contained in:
fluzzi 2022-03-28 10:20:00 -03:00
parent 2042178cbe
commit 9898920ab2
3 changed files with 46 additions and 6 deletions

View File

@ -10,8 +10,13 @@ pip install conn
```
import conn
router = conn.node("unique name","ip/hostname", user="username", password="password")
router.run("show run")
router.run(["term len 0","show run"])
print(router.output)
hasip = router.test("show ip int brief","1.1.1.1")
if hasip:
print("Router have ip 1.1.1.1")
else:
print("router don't have ip 1.1.1.1")
```
### Using manager configuration

View File

@ -119,7 +119,7 @@ class node:
print(connect)
exit(1)
def run(self, commands,*, folder = '', prompt = '>$|#$|\$.$', stdout = False):
def run(self, commands,*, folder = '', prompt = '>$|#$|\$$|>.$|#.$|\$.$', stdout = False):
connect = self._connect()
if connect == True:
winsize = self.child.getwinsize()
@ -136,6 +136,7 @@ class node:
output = output + self.child.before.decode() + self.child.after.decode()
self.child.expect(prompt)
output = output + self.child.before.decode() + self.child.after.decode()
self.child.close()
if stdout == True:
print(output)
if folder != '':
@ -148,6 +149,35 @@ class node:
else:
return connect
def test(self, commands, expected, *, prompt = '>$|#$|\$$|>.$|#.$|\$.$'):
connect = self._connect()
if connect == True:
winsize = self.child.getwinsize()
self.child.setwinsize(65535,winsize[1])
output = ''
if isinstance(commands, list):
for c in commands:
self.child.expect(prompt)
self.child.sendline(c)
output = output + self.child.before.decode() + self.child.after.decode()
else:
self.child.expect(prompt)
self.child.sendline(commands)
output = output + self.child.before.decode() + self.child.after.decode()
expects = [expected, prompt]
results = self.child.expect(expects)
output = output + self.child.before.decode() + self.child.after.decode()
self.output = output
match results:
case 0:
self.child.close()
return True
case 1:
self.child.close()
return False
else:
return connect
def _connect(self, debug = False):
if self.protocol == "ssh":
cmd = "ssh"
@ -167,7 +197,7 @@ class node:
passwords = self.__passtx(self.password)
else:
passwords = []
expects = ['yes/no', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:|[u|U]sername:', '>$|#$|\$.$', 'suspend', pexpect.EOF, "No route to host", "resolve hostname"]
expects = ['yes/no', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:|[u|U]sername:', '>$|#$|\$$|>.$|#.$|\$.$', 'suspend', pexpect.EOF, "No route to host", "resolve hostname"]
elif self.protocol == "telnet":
cmd = "telnet " + self.host
if self.port != '':
@ -180,7 +210,7 @@ class node:
passwords = self.__passtx(self.password)
else:
passwords = []
expects = ['[u|U]sername:', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:', '>$|#$|\$.$', 'suspend', pexpect.EOF, "No route to host", "resolve hostname"]
expects = ['[u|U]sername:', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:', '>$|#$|\$$|>.$|#.$|\$.$', 'suspend', pexpect.EOF, "No route to host", "resolve hostname"]
else:
raise ValueError("Invalid protocol: " + self.protocol)
child = pexpect.spawn(cmd)

View File

@ -38,9 +38,14 @@ conf = conn.configfile()
# ***
conn.connapp(conf, conn.node)
# ***
# list = ["xr@home","ios@home","router228@bbva","router142@bbva"]
# list = ["xr@home","ios@home","csr@home"]
# list = ["huawei@home"]
# for i in list:
# data = conf.getitem(i)
# routeri = conn.node(i,**data,config=conf)
# routeri.run(["term len 0","show run"], folder="test")
# result = routeri.test(["show ip int br"],"10.21.96.47")
# result = routeri.run(["set cli screen-length 0","show configuration"])
# result = routeri.run(["screen-length 0 temporary","display current-configuration"],prompt='>$')
# print(result)