merging CMDConnect and CursesConnect into the single Connect function

This commit is contained in:
ivan 2022-06-14 12:33:32 +03:00
parent abb2fcfa4c
commit 37b3926880
2 changed files with 116 additions and 135 deletions

View File

@ -13,7 +13,7 @@ def main():
long_description='SSH connection and aliases manager with curses and command line interface',
long_description_content_type='text/x-rst',
license='DWTWL 2.55',
version='1.09.3',
version='1.09.4',
py_modules=['sshch'],
scripts=['sshch/sshch'],
keywords='sshch ssh aliases curses manager',

View File

@ -22,7 +22,7 @@ from curses import textpad, panel
from threading import Thread
# https://gitlab.com/zlax/sshch
version = "1.09.3"
version = "1.09.4"
# expand groups by default
expand_default = True
# path to conf dir and file, default: ~/.config/sshch.conf
@ -93,6 +93,41 @@ def ConvertPassword(password):
return password_string
def Connect(aliases, command=False, threading=False, screen=False):
if screen:
curses.endwin()
groups = []
connectaliases = []
for a in conf.sections():
if conf.has_option(a, "group"):
groups.append(a)
for alias in aliases:
if alias in groups:
group_aliases = GroupChildAliases(alias)
for ga in group_aliases:
connectaliases.append(ga)
else:
connectaliases.append(alias)
connectaliases = setSeq(connectaliases)
threads = {}
for alias in connectaliases:
if conf.has_section(alias):
print("Connecting to " + alias + "...")
if threading:
threads[alias] = Thread(target=ConnectAlias, args=(alias,
command, True))
threads[alias].start()
else:
ConnectAlias(alias, command)
if not threading:
print("... " + alias + " session finished.")
else:
print("error: '" + alias + "' alias does not exists")
if screen:
print("Press 'enter' to continue.")
screen.getch()
def ConnectAlias(alias, command=False, threading=False):
exec_string = ""
if conf.has_option(alias, "password"):
@ -155,8 +190,8 @@ def CMDGroup(group):
def CMDEdit(alias):
if conf.has_section(alias):
if conf.has_option(alias, "exec_string"):
prompt_edit = ("".join(["Enter connection string for existing alias ",
"(example: ssh user@somehost.com):\n"]))
prompt_edit = ("".join(["Enter connection string for existing ",
"alias (example: ssh user@somehost.com):\n"]))
string = ""
while string == "":
string = input(prompt_edit)
@ -184,7 +219,8 @@ def CMDPassword(alias):
print("Can't set password for group.")
else:
if conf.has_section(alias):
prompt_pass = ("[UNSAFE] Enter password for sshpass (Ctrl+C - cancel, blank - clear password):\n")
prompt_pass = ("[UNSAFE] Enter password for sshpass (Ctrl+C" +
" - cancel, blank - clear password):\n")
string = ""
string = getpass(prompt_pass)
SetPassword(alias, string)
@ -194,7 +230,8 @@ def CMDPassword(alias):
def CMDRemove(alias):
if conf.has_section(alias):
prompt_remove = ("Type 'yes' if you sure to remove '" + alias + "' alias or group: ")
prompt_remove = ("Type 'yes' if you sure to remove '" + alias +
"' alias or group: ")
string = input(prompt_remove)
if string == "yes":
RemoveAliases([alias])
@ -204,35 +241,6 @@ def CMDRemove(alias):
print("error: '" + alias + "' alias or group does not exists.")
def CMDConnect(aliases, command=False, threading=False):
groups = []
connectaliases = []
for a in conf.sections():
if conf.has_option(a, "group"):
groups.append(a)
for alias in aliases:
if alias in groups:
group_aliases = GroupChildAliases(alias)
for ga in group_aliases:
connectaliases.append(ga)
else:
connectaliases.append(alias)
connectaliases = setSeq(connectaliases)
threads = {}
for alias in connectaliases:
if conf.has_section(alias):
print("Connecting to " + alias + "...")
if threading:
threads[alias] = Thread(target=ConnectAlias, args=(alias, command, True))
threads[alias].start()
else:
ConnectAlias(alias, command)
if not threading:
print("... " + alias + " session finished.")
else:
print("error: '" + alias + "' alias does not exists")
def CMDList(option, opt, value, parser):
print(' '.join(str(p) for p in conf.sections()))
@ -292,7 +300,8 @@ def GroupChildAliases(group):
return setSeq(childaliases)
def GroupTreeRecursion(level, group, treelist, resultalias, resultstring, expandlist, previousgroups):
def GroupTreeRecursion(level, group, treelist, resultalias, resultstring,
expandlist, previousgroups):
if group in previousgroups:
return resultalias, resultstring
previousgroups.append(group)
@ -339,35 +348,6 @@ def CMDFullList(option, opt, value, parser):
print (p)
def CursesConnect(screen, aliases, command=False, threading=False):
curses.endwin()
groups = []
connectaliases = []
for a in conf.sections():
if conf.has_option(a, "group"):
groups.append(a)
for alias in aliases:
if alias in groups:
group_aliases = GroupChildAliases(alias)
for ga in group_aliases:
connectaliases.append(ga)
else:
connectaliases.append(alias)
connectaliases = setSeq(connectaliases)
threads = {}
for alias in connectaliases:
print("Connecting to " + alias + "...")
if threading:
threads[alias] = Thread(target=ConnectAlias, args=(alias, command, True))
threads[alias].start()
else:
ConnectAlias(alias, command)
if not threading:
print("... " + alias + " session finished.")
print("Press 'enter' to continue.")
screen.getch()
def CursesExit(error=False):
curses.endwin()
if error:
@ -450,6 +430,7 @@ def CMDOptions():
def format_epilog(self, formatter):
return self.epilog
usage = "usage: %prog [options] [aliases]"
progname = path.basename(__file__)
epilog = ("".join(["Examples:\n ",
@ -509,9 +490,9 @@ def CMDOptions():
if options.keep:
HoldConnection(options.keep)
if options.thread:
CMDConnect(alias, options.thread, True)
Connect(alias, options.thread, True)
elif alias:
CMDConnect(alias, options.command)
Connect(alias, options.command)
# curses template from: https://stackoverflow.com/a/30828805/6224462
@ -760,8 +741,8 @@ def CursesMain():
"alias/aliases:"]
), "", normal_text, highlight_text)
command_string = command_textpad.edit(CursesTextpadConfirm)
CursesConnect(screen, selected,
command_string.replace("\n", "").rstrip())
Connect(selected, command_string.replace("\n", "").rstrip(),
False, screen)
curses.curs_set(0)
if (key_pressed == ord('t') or key_pressed == ord(
'T') or key_pressed == curses.KEY_F11) and row_num != 0:
@ -779,8 +760,8 @@ def CursesMain():
"alias/aliases:"]
), "", normal_text, highlight_text)
command_string = command_textpad.edit(CursesTextpadConfirm)
CursesConnect(screen, selected,
command_string.replace("\n", "").rstrip(),True)
Connect(selected, command_string.replace("\n", "").rstrip(),
True, screen)
curses.curs_set(0)
if (key_pressed == ord('k') or key_pressed == ord('K') or
key_pressed == (curses.KEY_F7)) and row_num != 0:
@ -815,7 +796,7 @@ def CursesMain():
selected.append(strings[i - 1])
if not len(selected) > 0:
selected.append(strings[position - 1].split()[0].strip())
CursesConnect(screen, selected)
Connect(selected, False, False, screen)
selected_strings = [" " for i in range(0, row_num + 1)]
if (key_pressed == 32 or key_pressed == (
curses.KEY_IC)) and row_num != 0: