merging CMDConnect and CursesConnect into the single Connect function
This commit is contained in:
parent
abb2fcfa4c
commit
37b3926880
2
setup.py
2
setup.py
|
@ -13,7 +13,7 @@ def main():
|
||||||
long_description='SSH connection and aliases manager with curses and command line interface',
|
long_description='SSH connection and aliases manager with curses and command line interface',
|
||||||
long_description_content_type='text/x-rst',
|
long_description_content_type='text/x-rst',
|
||||||
license='DWTWL 2.55',
|
license='DWTWL 2.55',
|
||||||
version='1.09.3',
|
version='1.09.4',
|
||||||
py_modules=['sshch'],
|
py_modules=['sshch'],
|
||||||
scripts=['sshch/sshch'],
|
scripts=['sshch/sshch'],
|
||||||
keywords='sshch ssh aliases curses manager',
|
keywords='sshch ssh aliases curses manager',
|
||||||
|
|
123
sshch/sshch
123
sshch/sshch
|
@ -22,7 +22,7 @@ from curses import textpad, panel
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
# https://gitlab.com/zlax/sshch
|
# https://gitlab.com/zlax/sshch
|
||||||
version = "1.09.3"
|
version = "1.09.4"
|
||||||
# expand groups by default
|
# expand groups by default
|
||||||
expand_default = True
|
expand_default = True
|
||||||
# path to conf dir and file, default: ~/.config/sshch.conf
|
# path to conf dir and file, default: ~/.config/sshch.conf
|
||||||
|
@ -93,6 +93,41 @@ def ConvertPassword(password):
|
||||||
return password_string
|
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):
|
def ConnectAlias(alias, command=False, threading=False):
|
||||||
exec_string = ""
|
exec_string = ""
|
||||||
if conf.has_option(alias, "password"):
|
if conf.has_option(alias, "password"):
|
||||||
|
@ -155,8 +190,8 @@ def CMDGroup(group):
|
||||||
def CMDEdit(alias):
|
def CMDEdit(alias):
|
||||||
if conf.has_section(alias):
|
if conf.has_section(alias):
|
||||||
if conf.has_option(alias, "exec_string"):
|
if conf.has_option(alias, "exec_string"):
|
||||||
prompt_edit = ("".join(["Enter connection string for existing alias ",
|
prompt_edit = ("".join(["Enter connection string for existing ",
|
||||||
"(example: ssh user@somehost.com):\n"]))
|
"alias (example: ssh user@somehost.com):\n"]))
|
||||||
string = ""
|
string = ""
|
||||||
while string == "":
|
while string == "":
|
||||||
string = input(prompt_edit)
|
string = input(prompt_edit)
|
||||||
|
@ -184,7 +219,8 @@ def CMDPassword(alias):
|
||||||
print("Can't set password for group.")
|
print("Can't set password for group.")
|
||||||
else:
|
else:
|
||||||
if conf.has_section(alias):
|
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 = ""
|
||||||
string = getpass(prompt_pass)
|
string = getpass(prompt_pass)
|
||||||
SetPassword(alias, string)
|
SetPassword(alias, string)
|
||||||
|
@ -194,7 +230,8 @@ def CMDPassword(alias):
|
||||||
|
|
||||||
def CMDRemove(alias):
|
def CMDRemove(alias):
|
||||||
if conf.has_section(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)
|
string = input(prompt_remove)
|
||||||
if string == "yes":
|
if string == "yes":
|
||||||
RemoveAliases([alias])
|
RemoveAliases([alias])
|
||||||
|
@ -204,35 +241,6 @@ def CMDRemove(alias):
|
||||||
print("error: '" + alias + "' alias or group does not exists.")
|
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):
|
def CMDList(option, opt, value, parser):
|
||||||
print(' '.join(str(p) for p in conf.sections()))
|
print(' '.join(str(p) for p in conf.sections()))
|
||||||
|
|
||||||
|
@ -292,7 +300,8 @@ def GroupChildAliases(group):
|
||||||
return setSeq(childaliases)
|
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:
|
if group in previousgroups:
|
||||||
return resultalias, resultstring
|
return resultalias, resultstring
|
||||||
previousgroups.append(group)
|
previousgroups.append(group)
|
||||||
|
@ -339,35 +348,6 @@ def CMDFullList(option, opt, value, parser):
|
||||||
print (p)
|
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):
|
def CursesExit(error=False):
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
if error:
|
if error:
|
||||||
|
@ -450,6 +430,7 @@ def CMDOptions():
|
||||||
|
|
||||||
def format_epilog(self, formatter):
|
def format_epilog(self, formatter):
|
||||||
return self.epilog
|
return self.epilog
|
||||||
|
|
||||||
usage = "usage: %prog [options] [aliases]"
|
usage = "usage: %prog [options] [aliases]"
|
||||||
progname = path.basename(__file__)
|
progname = path.basename(__file__)
|
||||||
epilog = ("".join(["Examples:\n ",
|
epilog = ("".join(["Examples:\n ",
|
||||||
|
@ -509,9 +490,9 @@ def CMDOptions():
|
||||||
if options.keep:
|
if options.keep:
|
||||||
HoldConnection(options.keep)
|
HoldConnection(options.keep)
|
||||||
if options.thread:
|
if options.thread:
|
||||||
CMDConnect(alias, options.thread, True)
|
Connect(alias, options.thread, True)
|
||||||
elif alias:
|
elif alias:
|
||||||
CMDConnect(alias, options.command)
|
Connect(alias, options.command)
|
||||||
|
|
||||||
# curses template from: https://stackoverflow.com/a/30828805/6224462
|
# curses template from: https://stackoverflow.com/a/30828805/6224462
|
||||||
|
|
||||||
|
@ -760,8 +741,8 @@ def CursesMain():
|
||||||
"alias/aliases:"]
|
"alias/aliases:"]
|
||||||
), "", normal_text, highlight_text)
|
), "", normal_text, highlight_text)
|
||||||
command_string = command_textpad.edit(CursesTextpadConfirm)
|
command_string = command_textpad.edit(CursesTextpadConfirm)
|
||||||
CursesConnect(screen, selected,
|
Connect(selected, command_string.replace("\n", "").rstrip(),
|
||||||
command_string.replace("\n", "").rstrip())
|
False, screen)
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
if (key_pressed == ord('t') or key_pressed == ord(
|
if (key_pressed == ord('t') or key_pressed == ord(
|
||||||
'T') or key_pressed == curses.KEY_F11) and row_num != 0:
|
'T') or key_pressed == curses.KEY_F11) and row_num != 0:
|
||||||
|
@ -779,8 +760,8 @@ def CursesMain():
|
||||||
"alias/aliases:"]
|
"alias/aliases:"]
|
||||||
), "", normal_text, highlight_text)
|
), "", normal_text, highlight_text)
|
||||||
command_string = command_textpad.edit(CursesTextpadConfirm)
|
command_string = command_textpad.edit(CursesTextpadConfirm)
|
||||||
CursesConnect(screen, selected,
|
Connect(selected, command_string.replace("\n", "").rstrip(),
|
||||||
command_string.replace("\n", "").rstrip(),True)
|
True, screen)
|
||||||
curses.curs_set(0)
|
curses.curs_set(0)
|
||||||
if (key_pressed == ord('k') or key_pressed == ord('K') or
|
if (key_pressed == ord('k') or key_pressed == ord('K') or
|
||||||
key_pressed == (curses.KEY_F7)) and row_num != 0:
|
key_pressed == (curses.KEY_F7)) and row_num != 0:
|
||||||
|
@ -815,7 +796,7 @@ def CursesMain():
|
||||||
selected.append(strings[i - 1])
|
selected.append(strings[i - 1])
|
||||||
if not len(selected) > 0:
|
if not len(selected) > 0:
|
||||||
selected.append(strings[position - 1].split()[0].strip())
|
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)]
|
selected_strings = [" " for i in range(0, row_num + 1)]
|
||||||
if (key_pressed == 32 or key_pressed == (
|
if (key_pressed == 32 or key_pressed == (
|
||||||
curses.KEY_IC)) and row_num != 0:
|
curses.KEY_IC)) and row_num != 0:
|
||||||
|
|
Loading…
Reference in New Issue