From 5113aef8c2d89d2382eb4f31ca6c08dd9e0e8058 Mon Sep 17 00:00:00 2001 From: fluzzi Date: Fri, 18 Mar 2022 16:16:31 -0300 Subject: [PATCH] updates + ad requriements --- conn/core.py | 29 +++++++++++++++++++---------- conn/requirements.txt | 3 +++ test.py | 18 +++++++++++------- 3 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 conn/requirements.txt diff --git a/conn/core.py b/conn/core.py index 13727da..89d3ba8 100755 --- a/conn/core.py +++ b/conn/core.py @@ -87,8 +87,12 @@ class node: logfile = re.sub(r'\$\{date (.*)}',now.strftime(dateconf.group(1)), logfile) return logfile - def _logclean(self, logfile): - t = open(logfile, "r").read().replace("\n","",1).replace("\a","") + def _logclean(self, logfile, var = False): + if var == False: + t = open(logfile, "r").read() + else: + t = logfile + t = t.replace("\n","",1).replace("\a","") t = t.replace('\n\n', '\n') t = re.sub('.\[K', '', t) while True: @@ -98,10 +102,13 @@ class node: t = tb ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/ ]*[@-~])') t = ansi_escape.sub('', t) - d = open(logfile, "w") - d.write(t) - d.close() - return + if var == False: + d = open(logfile, "w") + d.write(t) + d.close() + return + else: + return t def interact(self, missingtext = False): connect = self._connect() @@ -116,7 +123,7 @@ class node: if "logfile" in dir(self): self._logclean(self.logfile) - def run(self, commands,*, folder = '', prompt = '>$|#$|\$.$'): + def run(self, commands,*, folder = '', prompt = '>$|#$|\$.$', stdout = False): connect = self._connect() if connect == True: output = '' @@ -127,18 +134,20 @@ class node: output = output + self.child.before.decode() + self.child.after.decode() else: self.child.expect(prompt) - print(self.child.sendline(commands)) + self.child.sendline(commands) output = output + self.child.before.decode() + self.child.after.decode() self.child.expect(prompt) output = output + self.child.before.decode() + self.child.after.decode() if folder == '': - print(output) + if stdout == True: + print(output) else: with open(folder + "/" + self.unique, "w") as f: f.write(output) f.close() self._logclean(folder + "/" + self.unique) - + self.output = output + return output diff --git a/conn/requirements.txt b/conn/requirements.txt new file mode 100644 index 0000000..b229ac8 --- /dev/null +++ b/conn/requirements.txt @@ -0,0 +1,3 @@ +pexpect~=4.8.0 +pycryptodome~=3.14.1 +PyYAML~=6.0 diff --git a/test.py b/test.py index d9578bf..39c9044 100755 --- a/test.py +++ b/test.py @@ -17,13 +17,17 @@ conf = conn.configfile("test.yaml") # print(yaml.dump(conf.profiles)) # conf.saveconfig("test.yaml") # *** -xr=conn.node("xr@home", **conf.connections["home"]["xr"], config=conf) -ios=conn.node("ios@home", **conf.connections["home"]["ios"], config=conf) -norman = conn.node("norman@home", **conf.connections["home"]["norman"], config=conf) -eve = conn.node("eve@home", **conf.connections["home"]["eve"], config=conf) +# xr=conn.node("xr@home", **conf.connections["home"]["xr"], config=conf) +# ios=conn.node("ios@home", **conf.connections["home"]["ios"], config=conf) +# norman = conn.node("norman@home", **conf.connections["home"]["norman"], config=conf) +# eve = conn.node("eve@home", **conf.connections["home"]["eve"], config=conf) +router228 = conn.node("router228@bbva", **conf.connections["bbva"]["router228"], config=conf) +# router228.interact() +router228.run(["term len 0","show ip int br"]) # xr.run(["term len 0","show ip bgp", "show ip bgp summ"], folder="test") -# ios.run(["term len 0","show ip bgp", "show ip bgp summ"], folder="test") +# ios.run(["term len 0","show ip bgp", "show ip bgp summ"]) # norman.run(["ls -la", "pwd"], folder = "test") -# eve.run(["ls -la", "pwd"], folder = "test") -xr.interact() +# test = eve.run(["ls -la", "pwd"]) +print(router228.output) +# xr.interact()