Add new jumphost feature to nodes

bug fix when adding a node without name
This commit is contained in:
fluzzi32 committed 2023-12-01 18:30:29 -03:00
1 parent b7e21806df
commit 6517d75465
4 files changed
+93 -16

No files matched your search

+47 -4
View File
@@ -33,7 +33,7 @@ class node:
'''
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags=''):
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost=''):
'''
### Parameters:
@@ -66,6 +66,8 @@ class node:
- tags (dict) : Tags useful for automation and personal porpuse
like "os", "prompt" and "screenleght_command"
- jumphost (str): Reference another node to be used as a jumphost
'''
if config == '':
self.idletime = 0
@@ -74,7 +76,7 @@ class node:
self.idletime = config.config["idletime"]
self.key = config.key
self.unique = unique
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags}
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
for key in attr:
profile = re.search("^@(.*)", str(attr[key]))
if profile and config != '':
@@ -97,6 +99,45 @@ class node:
self.password.append(config.profiles[profile.group(1)]["password"])
else:
self.password = [password]
if self.jumphost != "" and config != '':
self.jumphost = config.getitem(self.jumphost)
for key in self.jumphost:
profile = re.search("^@(.*)", str(self.jumphost[key]))
if profile:
try:
self.jumphost[key] = config.profiles[profile.group(1)][key]
except:
self.jumphost[key] = ""
elif self.jumphost[key] == '' and key == "protocol":
try:
self.jumphost[key] = config.profiles["default"][key]
except:
self.jumphost[key] = "ssh"
if isinstance(self.jumphost["password"],list):
jumphost_password = []
for i, s in enumerate(self.jumphost["password"]):
profile = re.search("^@(.*)", self.jumphost["password"][i])
if profile:
jumphost_password.append(config.profiles[profile.group(1)]["password"])
self.jumphost["password"] = jumphost_password
else:
self.jumphost["password"] = [self.jumphost["password"]]
if self.jumphost["password"] != [""]:
self.password = self.jumphost["password"] + self.password
if self.jumphost["protocol"] == "ssh":
jumphost_cmd = self.jumphost["protocol"] + " -W %h:%p"
if self.port != '':
jumphost_cmd = jumphost_cmd + " -p " + self.jumphost["port"]
if self.options != '':
jumphost_cmd = jumphost_cmd + " " + self.jumphost["options"]
if self.user == '':
jumphost_cmd = jumphost_cmd + " {}".format(self.jumphost["host"])
else:
jumphost_cmd = jumphost_cmd + " {}".format("@".join([self.jumphost["user"],self.jumphost["host"]]))
self.jumphost = f"-o ProxyCommand=\"{jumphost_cmd}\""
else:
self.jumphost = ""
def _passtx(self, passwords, *, keyfile=None):
# decrypts passwords, used by other methdos.
@@ -203,8 +244,8 @@ class node:
'''
connect = self._connect(debug = debug)
if connect == True:
# size = re.search('columns=([0-9]+).*lines=([0-9]+)',str(os.get_terminal_size()))
# self.child.setwinsize(int(size.group(2)),int(size.group(1)))
size = re.search('columns=([0-9]+).*lines=([0-9]+)',str(os.get_terminal_size()))
self.child.setwinsize(int(size.group(2)),int(size.group(1)))
print("Connected to " + self.unique + " at " + self.host + (":" if self.port != '' else '') + self.port + " via: " + self.protocol)
if 'logfile' in dir(self):
# Initialize self.mylog
@@ -431,6 +472,8 @@ class node:
cmd = cmd + " " + self.options
if self.logs != '':
self.logfile = self._logfile()
if self.jumphost != '':
cmd = cmd + " " + self.jumphost
if self.password[0] != '':
passwords = self._passtx(self.password)
else: