update big
This commit is contained in:
parent
4a593f2016
commit
255b2bd4ef
@ -15,7 +15,7 @@ from Crypto.PublicKey import RSA
|
|||||||
|
|
||||||
class configfile:
|
class configfile:
|
||||||
|
|
||||||
def __init__(self, conf = None):
|
def __init__(self, conf = None, *, key = None):
|
||||||
home = os.path.expanduser("~")
|
home = os.path.expanduser("~")
|
||||||
self.defaultdir = home + '/.config/conn'
|
self.defaultdir = home + '/.config/conn'
|
||||||
self.defaultfile = self.defaultdir + '/config.yaml'
|
self.defaultfile = self.defaultdir + '/config.yaml'
|
||||||
@ -24,8 +24,11 @@ class configfile:
|
|||||||
self.dir = self.defaultdir
|
self.dir = self.defaultdir
|
||||||
self.file = self.defaultfile
|
self.file = self.defaultfile
|
||||||
else:
|
else:
|
||||||
self.dir = os.path.dirname(conf)
|
|
||||||
self.file = conf
|
self.file = conf
|
||||||
|
if key == None:
|
||||||
|
self.key = self.defaultkey
|
||||||
|
else:
|
||||||
|
self.key = key
|
||||||
if os.path.exists(self.file):
|
if os.path.exists(self.file):
|
||||||
config = self.loadconfig(self.file)
|
config = self.loadconfig(self.file)
|
||||||
else:
|
else:
|
||||||
@ -33,23 +36,85 @@ class configfile:
|
|||||||
self.config = config["config"]
|
self.config = config["config"]
|
||||||
self.connections = config["connections"]
|
self.connections = config["connections"]
|
||||||
self.profiles = config["profiles"]
|
self.profiles = config["profiles"]
|
||||||
|
if not os.path.exists(self.key):
|
||||||
|
self.createkey(self.key)
|
||||||
|
self.privatekey = RSA.import_key(open(self.key).read())
|
||||||
|
self.publickey = self.privatekey.publickey()
|
||||||
|
|
||||||
|
|
||||||
def loadconfig(self, conf):
|
def loadconfig(self, conf):
|
||||||
ymlconf = open(conf)
|
ymlconf = open(conf)
|
||||||
return yaml.load(ymlconf.read(), Loader=yaml.FullLoader)
|
return yaml.load(ymlconf.read(), Loader=yaml.CLoader)
|
||||||
|
|
||||||
def createconfig(self, conf):
|
def createconfig(self, conf):
|
||||||
defaultconfig = {'config': {'case': False, 'frun': False, 'idletime': 30}, 'connections': {}, 'profiles': {}}
|
defaultconfig = {'config': {'case': False, 'frun': False, 'idletime': 30}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"" }}}
|
||||||
if not os.path.exists(conf):
|
if not os.path.exists(conf):
|
||||||
with open(conf, "w") as f:
|
with open(conf, "w") as f:
|
||||||
yaml.dump(defaultconfig, f, explicit_start=True)
|
yaml.dump(defaultconfig, f, explicit_start=True, Dumper=yaml.CDumper)
|
||||||
f.close()
|
f.close()
|
||||||
ymlconf = open(conf)
|
ymlconf = open(conf)
|
||||||
return yaml.load(ymlconf.read(), Loader=yaml.FullLoader)
|
return yaml.load(ymlconf.read(), Loader=yaml.CLoader)
|
||||||
|
|
||||||
|
def saveconfig(self, conf):
|
||||||
|
newconfig = {"config":{}, "connections": {}, "profiles": {}}
|
||||||
|
newconfig["config"] = self.config
|
||||||
|
newconfig["connections"] = self.connections
|
||||||
|
newconfig["profiles"] = self.profiles
|
||||||
|
with open(conf, "w") as f:
|
||||||
|
yaml.dump(newconfig, f, explicit_start=True, Dumper=yaml.CDumper)
|
||||||
|
f.close()
|
||||||
|
|
||||||
def createkey(self, keyfile):
|
def createkey(self, keyfile):
|
||||||
key = RSA.generate(2048)
|
key = RSA.generate(2048)
|
||||||
with open('mykey.pem','wb') as f:
|
with open(keyfile,'wb') as f:
|
||||||
f.write(key.export_key('PEM'))
|
f.write(key.export_key('PEM'))
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
def _explode_unique(self, unique):
|
||||||
|
uniques = unique.split("@")
|
||||||
|
result = {"id": uniques[0]}
|
||||||
|
if len(uniques) == 2:
|
||||||
|
result["folder"] = uniques[1]
|
||||||
|
elif len(uniques) == 3:
|
||||||
|
result["folder"] = uniques[2]
|
||||||
|
result["subfolder"] = uniques[1]
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='' ):
|
||||||
|
if folder == '':
|
||||||
|
self.connections[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "type": "connection"}
|
||||||
|
elif folder != '' and subfolder == '':
|
||||||
|
self.connections[folder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "type": "connection"}
|
||||||
|
elif folder != '' and subfolder != '':
|
||||||
|
self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "type": "connection"}
|
||||||
|
|
||||||
|
|
||||||
|
def _connections_del(self,*, id, folder='', subfolder=''):
|
||||||
|
if folder == '':
|
||||||
|
del self.connections[id]
|
||||||
|
elif folder != '' and subfolder == '':
|
||||||
|
del self.connections[folder][id]
|
||||||
|
elif folder != '' and subfolder != '':
|
||||||
|
del self.connections[folder][subfolder][id]
|
||||||
|
|
||||||
|
def _folder_add(self,*, folder, subfolder = ''):
|
||||||
|
if subfolder == '':
|
||||||
|
if folder not in self.connections:
|
||||||
|
self.connections[folder] = {"type": "folder"}
|
||||||
|
else:
|
||||||
|
if subfolder not in self.connections[folder]:
|
||||||
|
self.connections[folder][subfolder] = {"type": "subfolder"}
|
||||||
|
|
||||||
|
def _folder_del(self,*, folder, subfolder=''):
|
||||||
|
if subfolder == '':
|
||||||
|
del self.connections[folder]
|
||||||
|
else:
|
||||||
|
del self.connections[folder][subfolder]
|
||||||
|
|
||||||
|
|
||||||
|
def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='' ):
|
||||||
|
self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user}
|
||||||
|
|
||||||
|
|
||||||
|
def _profiles_del(self,*, id ):
|
||||||
|
del self.profiles[id]
|
||||||
|
106
conn/core.py
106
conn/core.py
@ -20,19 +20,19 @@ from .configfile import configfile
|
|||||||
#functions and clsses
|
#functions and clsses
|
||||||
|
|
||||||
class node:
|
class node:
|
||||||
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', type='', user=''):
|
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', type='', user='', config=''):
|
||||||
try:
|
if config == '':
|
||||||
config = configfile()
|
|
||||||
self.idletime = config.config["idletime"]
|
|
||||||
except:
|
|
||||||
config = {}
|
|
||||||
self.idletime = 0
|
self.idletime = 0
|
||||||
|
self.key = None
|
||||||
|
else:
|
||||||
|
self.idletime = config.config["idletime"]
|
||||||
|
self.key = config.key
|
||||||
self.unique = unique
|
self.unique = unique
|
||||||
self.id = self.unique.split("@")[0]
|
self.id = self.unique.split("@")[0]
|
||||||
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user}
|
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user}
|
||||||
for key in attr:
|
for key in attr:
|
||||||
profile = re.search("^@(.*)", attr[key])
|
profile = re.search("^@(.*)", attr[key])
|
||||||
if profile:
|
if profile and config != '':
|
||||||
setattr(self,key,config.profiles[profile.group(1)][key])
|
setattr(self,key,config.profiles[profile.group(1)][key])
|
||||||
elif attr[key] == '' and key == "protocol":
|
elif attr[key] == '' and key == "protocol":
|
||||||
try:
|
try:
|
||||||
@ -45,23 +45,29 @@ class node:
|
|||||||
self.password = []
|
self.password = []
|
||||||
for i, s in enumerate(password):
|
for i, s in enumerate(password):
|
||||||
profile = re.search("^@(.*)", password[i])
|
profile = re.search("^@(.*)", password[i])
|
||||||
if profile:
|
if profile and config != '':
|
||||||
self.password.append(config.profiles[profile.group(1)]["password"])
|
self.password.append(config.profiles[profile.group(1)]["password"])
|
||||||
else:
|
else:
|
||||||
self.password = [str(password)]
|
self.password = [password]
|
||||||
|
|
||||||
def __passtx(self, passwords, *, keyfile=None):
|
def __passtx(self, passwords, *, keyfile=None):
|
||||||
if keyfile is None:
|
keyfile = self.key
|
||||||
home = os.path.expanduser("~")
|
|
||||||
keyfile = home + '/.config/conn/.osk'
|
|
||||||
key = RSA.import_key(open(keyfile).read())
|
|
||||||
publickey = key.publickey()
|
|
||||||
encryptor = PKCS1_OAEP.new(publickey)
|
|
||||||
decryptor = PKCS1_OAEP.new(key)
|
|
||||||
dpass = []
|
dpass = []
|
||||||
|
if keyfile is None:
|
||||||
|
keyfile = self.key
|
||||||
|
else:
|
||||||
|
key = RSA.import_key(open(keyfile).read())
|
||||||
|
decryptor = PKCS1_OAEP.new(key)
|
||||||
for passwd in passwords:
|
for passwd in passwords:
|
||||||
|
if isinstance(passwd, str):
|
||||||
|
dpass.append(passwd)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
decrypted = decryptor.decrypt(ast.literal_eval(str(passwd))).decode("utf-8")
|
decrypted = decryptor.decrypt(ast.literal_eval(str(passwd))).decode("utf-8")
|
||||||
dpass.append(decrypted)
|
dpass.append(decrypted)
|
||||||
|
except:
|
||||||
|
print("Missing or wrong key")
|
||||||
|
exit(1)
|
||||||
return dpass
|
return dpass
|
||||||
|
|
||||||
|
|
||||||
@ -90,12 +96,53 @@ class node:
|
|||||||
if len(t) == len(tb):
|
if len(t) == len(tb):
|
||||||
break
|
break
|
||||||
t = tb
|
t = tb
|
||||||
|
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/ ]*[@-~])')
|
||||||
|
t = ansi_escape.sub('', t)
|
||||||
d = open(logfile, "w")
|
d = open(logfile, "w")
|
||||||
d.write(t)
|
d.write(t)
|
||||||
d.close()
|
d.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
def connect(self, mode = None):
|
def interact(self, missingtext = False):
|
||||||
|
connect = self._connect()
|
||||||
|
if connect == True:
|
||||||
|
print("Connected to " + self.unique + " at " + self.host + (":" if self.port != '' else '') + self.port + " via: " + self.protocol)
|
||||||
|
if 'logfile' in dir(self):
|
||||||
|
self.child.logfile_read = open(self.logfile, "wb")
|
||||||
|
# self.child.logfile = sys.stdout.buffer
|
||||||
|
if 'missingtext' in dir(self):
|
||||||
|
print(child.after.decode(), end='')
|
||||||
|
self.child.interact()
|
||||||
|
if "logfile" in dir(self):
|
||||||
|
self._logclean(self.logfile)
|
||||||
|
|
||||||
|
def run(self, commands,*, folder = '', prompt = '>$|#$|\$.$'):
|
||||||
|
connect = self._connect()
|
||||||
|
if connect == True:
|
||||||
|
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)
|
||||||
|
print(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)
|
||||||
|
else:
|
||||||
|
with open(folder + "/" + self.unique, "w") as f:
|
||||||
|
f.write(output)
|
||||||
|
f.close()
|
||||||
|
self._logclean(folder + "/" + self.unique)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _connect(self):
|
||||||
if self.protocol == "ssh":
|
if self.protocol == "ssh":
|
||||||
cmd = "ssh"
|
cmd = "ssh"
|
||||||
if self.idletime > 0:
|
if self.idletime > 0:
|
||||||
@ -109,12 +156,12 @@ class node:
|
|||||||
if self.options != '':
|
if self.options != '':
|
||||||
cmd = cmd + " " + self.options
|
cmd = cmd + " " + self.options
|
||||||
if self.logs != '':
|
if self.logs != '':
|
||||||
logfile = self._logfile()
|
self.logfile = self._logfile()
|
||||||
if self.password[0] != '':
|
if self.password[0] != '':
|
||||||
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']
|
expects = ['yes/no', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:|[u|U]sername:', '>$|#$|\$.$', 'suspend', pexpect.EOF]
|
||||||
elif self.protocol == "telnet":
|
elif self.protocol == "telnet":
|
||||||
cmd = "telnet " + self.host
|
cmd = "telnet " + self.host
|
||||||
if self.port != '':
|
if self.port != '':
|
||||||
@ -122,20 +169,16 @@ class node:
|
|||||||
if self.options != '':
|
if self.options != '':
|
||||||
cmd = cmd + " " + self.options
|
cmd = cmd + " " + self.options
|
||||||
if self.logs != '':
|
if self.logs != '':
|
||||||
logfile = self._logfile()
|
self.logfile = self._logfile()
|
||||||
if self.password[0] != '':
|
if self.password[0] != '':
|
||||||
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']
|
expects = ['[u|U]sername:', 'refused', 'supported', 'cipher', 'sage', 'timeout', 'unavailable', 'closed', '[p|P]assword:', '>$|#$|\$.$', 'suspend', pexpect.EOF]
|
||||||
else:
|
else:
|
||||||
print("Invalid protocol: " + self.protocol)
|
print("Invalid protocol: " + self.protocol)
|
||||||
return
|
return
|
||||||
child = pexpect.spawn(cmd)
|
child = pexpect.spawn(cmd)
|
||||||
if 'logfile' in locals():
|
|
||||||
child.logfile_read = open(logfile, "wb")
|
|
||||||
# child.logfile_read = sys.stdout.buffer
|
|
||||||
print("Connecting to " + self.unique + " at " + self.host + (":" if self.port != '' else '') + self.port + " via: " + self.protocol)
|
|
||||||
if len(passwords) > 0:
|
if len(passwords) > 0:
|
||||||
loops = len(passwords)
|
loops = len(passwords)
|
||||||
else:
|
else:
|
||||||
@ -152,7 +195,7 @@ class node:
|
|||||||
if self.user != '':
|
if self.user != '':
|
||||||
child.sendline(self.user)
|
child.sendline(self.user)
|
||||||
else:
|
else:
|
||||||
print(child.after.decode(), end='')
|
self.missingtext = True
|
||||||
break
|
break
|
||||||
case 1 | 2 | 3 | 4 | 5 | 6 |7:
|
case 1 | 2 | 3 | 4 | 5 | 6 |7:
|
||||||
print("Connection failed code:" + str(results))
|
print("Connection failed code:" + str(results))
|
||||||
@ -162,9 +205,9 @@ class node:
|
|||||||
if len(passwords) > 0:
|
if len(passwords) > 0:
|
||||||
child.sendline(passwords[i])
|
child.sendline(passwords[i])
|
||||||
else:
|
else:
|
||||||
print(child.after.decode(), end='')
|
self.missingtext = True
|
||||||
break
|
break
|
||||||
case 9:
|
case 9 | 11:
|
||||||
endloop = True
|
endloop = True
|
||||||
child.sendline()
|
child.sendline()
|
||||||
break
|
break
|
||||||
@ -174,9 +217,8 @@ class node:
|
|||||||
if endloop:
|
if endloop:
|
||||||
break
|
break
|
||||||
child.readline(0)
|
child.readline(0)
|
||||||
if mode == "interact":
|
self.child = child
|
||||||
child.interact()
|
return True
|
||||||
if "logfile" in locals():
|
|
||||||
self._logclean(logfile)
|
|
||||||
|
|
||||||
# script
|
# script
|
||||||
|
28
test.py
28
test.py
@ -1,7 +1,29 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import conn
|
import conn
|
||||||
|
import yaml
|
||||||
|
|
||||||
conf = conn.configfile()
|
conf = conn.configfile("test.yaml")
|
||||||
conn1=conn.node("pruebas@conn", **conf.connections["home"]["xr"])
|
# ***
|
||||||
conn1.connect("interact")
|
# conf._connections_del(id = "zab3mu", folder="teco")
|
||||||
|
# conf._connections_add(id = "zzztest", folder="teco" ,host = "10.21.96.45", user="sarabada")
|
||||||
|
# conf._folder_add(folder="zzz")
|
||||||
|
# conf._folder_add(folder="zzz", subfolder="achus")
|
||||||
|
# conf._connections_add(id = "zzztec", subfolder="achus", folder="zzz" ,host = "10.21.96.45")
|
||||||
|
# conf._connections_add(id = "zzztec", subfolder="achus", folder="zzz" ,host = "10.21.96.45", options=" saracatanga")
|
||||||
|
# conf._folder_del(folder = "zzz", subfolder = "achus")
|
||||||
|
# conf._profiles_add(id = "test", user = 'tesuser')
|
||||||
|
# conf._profiles_add(id = "test", user = 'tesuser', protocol = 'telnet')
|
||||||
|
# conf._profiles_del(id = "test")
|
||||||
|
# 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.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")
|
||||||
|
# norman.run(["ls -la", "pwd"], folder = "test")
|
||||||
|
# eve.run(["ls -la", "pwd"], folder = "test")
|
||||||
|
xr.interact()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user