update big
This commit is contained in:
110
conn/core.py
110
conn/core.py
@ -20,19 +20,19 @@ from .configfile import configfile
|
||||
#functions and clsses
|
||||
|
||||
class node:
|
||||
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', type='', user=''):
|
||||
try:
|
||||
config = configfile()
|
||||
self.idletime = config.config["idletime"]
|
||||
except:
|
||||
config = {}
|
||||
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', type='', user='', config=''):
|
||||
if config == '':
|
||||
self.idletime = 0
|
||||
self.key = None
|
||||
else:
|
||||
self.idletime = config.config["idletime"]
|
||||
self.key = config.key
|
||||
self.unique = unique
|
||||
self.id = self.unique.split("@")[0]
|
||||
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user}
|
||||
for key in attr:
|
||||
profile = re.search("^@(.*)", attr[key])
|
||||
if profile:
|
||||
if profile and config != '':
|
||||
setattr(self,key,config.profiles[profile.group(1)][key])
|
||||
elif attr[key] == '' and key == "protocol":
|
||||
try:
|
||||
@ -45,23 +45,29 @@ class node:
|
||||
self.password = []
|
||||
for i, s in enumerate(password):
|
||||
profile = re.search("^@(.*)", password[i])
|
||||
if profile:
|
||||
if profile and config != '':
|
||||
self.password.append(config.profiles[profile.group(1)]["password"])
|
||||
else:
|
||||
self.password = [str(password)]
|
||||
self.password = [password]
|
||||
|
||||
def __passtx(self, passwords, *, keyfile=None):
|
||||
if keyfile is None:
|
||||
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)
|
||||
keyfile = self.key
|
||||
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:
|
||||
decrypted = decryptor.decrypt(ast.literal_eval(str(passwd))).decode("utf-8")
|
||||
dpass.append(decrypted)
|
||||
if isinstance(passwd, str):
|
||||
dpass.append(passwd)
|
||||
else:
|
||||
try:
|
||||
decrypted = decryptor.decrypt(ast.literal_eval(str(passwd))).decode("utf-8")
|
||||
dpass.append(decrypted)
|
||||
except:
|
||||
print("Missing or wrong key")
|
||||
exit(1)
|
||||
return dpass
|
||||
|
||||
|
||||
@ -90,12 +96,53 @@ class node:
|
||||
if len(t) == len(tb):
|
||||
break
|
||||
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
|
||||
|
||||
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":
|
||||
cmd = "ssh"
|
||||
if self.idletime > 0:
|
||||
@ -109,12 +156,12 @@ class node:
|
||||
if self.options != '':
|
||||
cmd = cmd + " " + self.options
|
||||
if self.logs != '':
|
||||
logfile = self._logfile()
|
||||
self.logfile = self._logfile()
|
||||
if self.password[0] != '':
|
||||
passwords = self.__passtx(self.password)
|
||||
else:
|
||||
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":
|
||||
cmd = "telnet " + self.host
|
||||
if self.port != '':
|
||||
@ -122,20 +169,16 @@ class node:
|
||||
if self.options != '':
|
||||
cmd = cmd + " " + self.options
|
||||
if self.logs != '':
|
||||
logfile = self._logfile()
|
||||
self.logfile = self._logfile()
|
||||
if self.password[0] != '':
|
||||
passwords = self.__passtx(self.password)
|
||||
else:
|
||||
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:
|
||||
print("Invalid protocol: " + self.protocol)
|
||||
return
|
||||
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:
|
||||
loops = len(passwords)
|
||||
else:
|
||||
@ -152,7 +195,7 @@ class node:
|
||||
if self.user != '':
|
||||
child.sendline(self.user)
|
||||
else:
|
||||
print(child.after.decode(), end='')
|
||||
self.missingtext = True
|
||||
break
|
||||
case 1 | 2 | 3 | 4 | 5 | 6 |7:
|
||||
print("Connection failed code:" + str(results))
|
||||
@ -162,9 +205,9 @@ class node:
|
||||
if len(passwords) > 0:
|
||||
child.sendline(passwords[i])
|
||||
else:
|
||||
print(child.after.decode(), end='')
|
||||
self.missingtext = True
|
||||
break
|
||||
case 9:
|
||||
case 9 | 11:
|
||||
endloop = True
|
||||
child.sendline()
|
||||
break
|
||||
@ -174,9 +217,8 @@ class node:
|
||||
if endloop:
|
||||
break
|
||||
child.readline(0)
|
||||
if mode == "interact":
|
||||
child.interact()
|
||||
if "logfile" in locals():
|
||||
self._logclean(logfile)
|
||||
self.child = child
|
||||
return True
|
||||
|
||||
|
||||
# script
|
||||
|
Reference in New Issue
Block a user