update
This commit is contained in:
parent
2042178cbe
commit
9898920ab2
@ -10,8 +10,13 @@ pip install conn
|
|||||||
```
|
```
|
||||||
import conn
|
import conn
|
||||||
router = conn.node("unique name","ip/hostname", user="username", password="password")
|
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)
|
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
|
### Using manager configuration
|
||||||
|
36
conn/core.py
36
conn/core.py
@ -119,7 +119,7 @@ class node:
|
|||||||
print(connect)
|
print(connect)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
def run(self, commands,*, folder = '', prompt = '>$|#$|\$.$', stdout = False):
|
def run(self, commands,*, folder = '', prompt = '>$|#$|\$$|>.$|#.$|\$.$', stdout = False):
|
||||||
connect = self._connect()
|
connect = self._connect()
|
||||||
if connect == True:
|
if connect == True:
|
||||||
winsize = self.child.getwinsize()
|
winsize = self.child.getwinsize()
|
||||||
@ -136,6 +136,7 @@ class node:
|
|||||||
output = output + self.child.before.decode() + self.child.after.decode()
|
output = output + self.child.before.decode() + self.child.after.decode()
|
||||||
self.child.expect(prompt)
|
self.child.expect(prompt)
|
||||||
output = output + self.child.before.decode() + self.child.after.decode()
|
output = output + self.child.before.decode() + self.child.after.decode()
|
||||||
|
self.child.close()
|
||||||
if stdout == True:
|
if stdout == True:
|
||||||
print(output)
|
print(output)
|
||||||
if folder != '':
|
if folder != '':
|
||||||
@ -148,6 +149,35 @@ class node:
|
|||||||
else:
|
else:
|
||||||
return connect
|
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):
|
def _connect(self, debug = False):
|
||||||
if self.protocol == "ssh":
|
if self.protocol == "ssh":
|
||||||
cmd = "ssh"
|
cmd = "ssh"
|
||||||
@ -167,7 +197,7 @@ class node:
|
|||||||
passwords = self.__passtx(self.password)
|
passwords = self.__passtx(self.password)
|
||||||
else:
|
else:
|
||||||
passwords = []
|
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":
|
elif self.protocol == "telnet":
|
||||||
cmd = "telnet " + self.host
|
cmd = "telnet " + self.host
|
||||||
if self.port != '':
|
if self.port != '':
|
||||||
@ -180,7 +210,7 @@ class node:
|
|||||||
passwords = self.__passtx(self.password)
|
passwords = self.__passtx(self.password)
|
||||||
else:
|
else:
|
||||||
passwords = []
|
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:
|
else:
|
||||||
raise ValueError("Invalid protocol: " + self.protocol)
|
raise ValueError("Invalid protocol: " + self.protocol)
|
||||||
child = pexpect.spawn(cmd)
|
child = pexpect.spawn(cmd)
|
||||||
|
9
test.py
9
test.py
@ -38,9 +38,14 @@ conf = conn.configfile()
|
|||||||
# ***
|
# ***
|
||||||
conn.connapp(conf, conn.node)
|
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:
|
# for i in list:
|
||||||
# data = conf.getitem(i)
|
# data = conf.getitem(i)
|
||||||
# routeri = conn.node(i,**data,config=conf)
|
# 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)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user