Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
ivan | 8ab5a13cca | |
ivan | fc9640a491 | |
Anton Samofal | 8ee868ab69 | |
ivan | 2c5c87f555 | |
ivan | cd501bdc1e | |
ivan | 37b3926880 | |
ivan | abb2fcfa4c | |
ivan | 7d7e2ff775 | |
ivan | eddfedb6c3 | |
ivan | e11978957f | |
ivan | 032b4b1ab0 | |
ivan | 7fa8bdb6f9 | |
ivan | 10ca6ad15b | |
ivan | 48d491312a | |
ivan | 0bbf06340a | |
ivan | ed10aa8f03 | |
ivan | 0fd4ff9b74 | |
ivan | a39cd7ae68 |
|
@ -4,7 +4,8 @@ sshch is released under DWTWL 2.55 license
|
||||||
|
|
||||||
sshch compatible with pyhon2 and python3, no additional libraries are required
|
sshch compatible with pyhon2 and python3, no additional libraries are required
|
||||||
### Screenshot
|
### Screenshot
|
||||||
![sshch](https://raw.githubusercontent.com/zlaxy/sshch/master/sshch_screenshot.png)
|
![sshch](https://dev.ussr.win/zlax/sshch/raw/branch/master/sshch_screenshot.png)
|
||||||
|
|
||||||
### Installing
|
### Installing
|
||||||
**You can install a release version from pip:**
|
**You can install a release version from pip:**
|
||||||
```bash
|
```bash
|
||||||
|
|
6
setup.py
6
setup.py
|
@ -8,15 +8,15 @@ def main():
|
||||||
setup(name='sshch',
|
setup(name='sshch',
|
||||||
author='zlaxy',
|
author='zlaxy',
|
||||||
author_email='zlaxyi@gmail.com',
|
author_email='zlaxyi@gmail.com',
|
||||||
url='https://github.com/zlaxy/sshch/',
|
url='https://gitlab.com/zlax/sshch',
|
||||||
description='Ssh connection and aliases manager',
|
description='Ssh connection and aliases manager',
|
||||||
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.0',
|
version='1.09.7',
|
||||||
py_modules=['sshch'],
|
py_modules=['sshch'],
|
||||||
scripts=['sshch/sshch'],
|
scripts=['sshch/sshch'],
|
||||||
keywords='sshch ssh aliases manager',
|
keywords='sshch ssh aliases curses manager',
|
||||||
python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4',
|
python_requires='>=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4',
|
||||||
|
|
||||||
# http://pypi.python.org/pypi?%3Aaction=list_classifiers
|
# http://pypi.python.org/pypi?%3Aaction=list_classifiers
|
||||||
|
|
368
sshch/sshch
368
sshch/sshch
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from __future__ import division
|
from __future__ import division
|
||||||
|
@ -7,7 +7,8 @@ try:
|
||||||
import configparser
|
import configparser
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import ConfigParser as configparser # Python 2.x import
|
import ConfigParser as configparser # Python 2.x import
|
||||||
|
import locale
|
||||||
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
from os import path
|
from os import path
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
@ -18,13 +19,24 @@ import base64
|
||||||
import time
|
import time
|
||||||
import curses
|
import curses
|
||||||
from curses import textpad, panel
|
from curses import textpad, panel
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
# https://github.com/zlaxy/sshch
|
# https://gitlab.com/zlax/sshch
|
||||||
version = "1.0"
|
version = "1.09.7"
|
||||||
# path to conf file, default: ~/.config/sshch.conf
|
|
||||||
conf_file = path.expanduser("~") + '/.config/sshch.conf'
|
|
||||||
# expand groups by default
|
# expand groups by default
|
||||||
expand_default = True
|
expand_default = True
|
||||||
|
# path to conf dir and file, default: ~/.config/sshch.conf
|
||||||
|
conf_dir = path.expanduser("~") + '/.config'
|
||||||
|
conf_file = conf_dir + '/' + 'sshch.conf'
|
||||||
|
|
||||||
|
|
||||||
|
class GroupTree(object):
|
||||||
|
"""Group object with relatives information"""
|
||||||
|
def __init__(self, group):
|
||||||
|
self.group = group
|
||||||
|
self.aliases = []
|
||||||
|
self.children = []
|
||||||
|
self.parent = []
|
||||||
|
|
||||||
|
|
||||||
def AddNewAlias(alias):
|
def AddNewAlias(alias):
|
||||||
|
@ -64,17 +76,74 @@ def RemoveAliases(aliases):
|
||||||
conf.write(open(conf_file, "w"))
|
conf.write(open(conf_file, "w"))
|
||||||
|
|
||||||
|
|
||||||
def ConnectAlias(alias, command=False):
|
def ConvertPassword(password):
|
||||||
|
password_string = "'"
|
||||||
|
for char in password:
|
||||||
|
if char == "'":
|
||||||
|
password_string += "'"+'"'+"'"+'"'+"'"
|
||||||
|
elif char == '"':
|
||||||
|
password_string += "''"+'"'+"''"
|
||||||
|
elif char == ';':
|
||||||
|
password_string += "'"+r'\;'+"'"
|
||||||
|
elif char == "\\":
|
||||||
|
password_string += "'"+'"'+"\\"+"\\"+'"'+"'"
|
||||||
|
else:
|
||||||
|
password_string += char
|
||||||
|
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):
|
||||||
exec_string = ""
|
exec_string = ""
|
||||||
if conf.has_option(alias, "password"):
|
if conf.has_option(alias, "password"):
|
||||||
password = base64.b32decode(base64.b16decode(
|
password = base64.b32decode(base64.b16decode(
|
||||||
base64.b64decode(conf.get(alias, "password"))))
|
base64.b64decode(conf.get(alias, "password"))))
|
||||||
exec_string = 'sshpass -p "' + password.decode('utf-8') + '" '
|
exec_string = "sshpass -p " + ConvertPassword(password.decode('utf-8')) + " "
|
||||||
if conf.has_option(alias, "exec_string"):
|
if conf.has_option(alias, "exec_string"):
|
||||||
exec_string = exec_string + conf.get(alias, "exec_string")
|
exec_string = exec_string + conf.get(alias, "exec_string")
|
||||||
if command:
|
if command:
|
||||||
exec_string = exec_string + " " + command
|
exec_string = exec_string + " " + command
|
||||||
|
try:
|
||||||
subprocess.Popen(exec_string, shell=True).communicate()[0]
|
subprocess.Popen(exec_string, shell=True).communicate()[0]
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if threading:
|
||||||
|
print ("... "+alias+" session output finished.")
|
||||||
|
|
||||||
|
|
||||||
def HoldConnection(alias):
|
def HoldConnection(alias):
|
||||||
|
@ -124,8 +193,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)
|
||||||
|
@ -153,7 +222,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)
|
||||||
|
@ -163,7 +233,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])
|
||||||
|
@ -173,102 +244,113 @@ 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):
|
|
||||||
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 = conf.get(alias, "group").split()
|
|
||||||
for ga in group_aliases:
|
|
||||||
if not ga in connectaliases:
|
|
||||||
connectaliases.append(ga)
|
|
||||||
else:
|
|
||||||
if not alias in connectaliases:
|
|
||||||
connectaliases.append(alias)
|
|
||||||
for alias in connectaliases:
|
|
||||||
if conf.has_section(alias):
|
|
||||||
print("Connecting to " + alias + "...")
|
|
||||||
ConnectAlias(alias, command)
|
|
||||||
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()))
|
||||||
|
|
||||||
|
|
||||||
def GetTreeList(strings=True, expandlist=True):
|
def setSeq(seq):
|
||||||
|
# py2&3 fastest way to remove duplicates from a list:
|
||||||
|
# https://www.rupython.com/x432-4-496.html
|
||||||
|
seen = set()
|
||||||
|
seen_add = seen.add
|
||||||
|
return [x for x in seq if not (x in seen or seen_add(x))]
|
||||||
|
|
||||||
|
|
||||||
|
def GroupChildRecursion(group, childaliases, treelist):
|
||||||
|
if treelist[group].children:
|
||||||
|
for a in treelist[group].children:
|
||||||
|
GroupChildRecursion(a, childaliases, treelist)
|
||||||
|
if treelist[group].aliases:
|
||||||
|
for a in treelist[group].aliases:
|
||||||
|
childaliases.append(a)
|
||||||
|
return childaliases
|
||||||
|
|
||||||
|
|
||||||
|
def GroupChildAliases(group):
|
||||||
aliases = []
|
aliases = []
|
||||||
groups = []
|
groups = []
|
||||||
resultalias = []
|
rootgroups = []
|
||||||
resultstring = []
|
treelist = {}
|
||||||
for a in conf.sections():
|
for a in conf.sections():
|
||||||
if conf.has_option(a, "group"):
|
if conf.has_option(a, "group"):
|
||||||
groups.append(a)
|
groups.append(a)
|
||||||
elif conf.has_option(a, "exec_string"):
|
elif conf.has_option(a, "exec_string"):
|
||||||
aliases.append(a)
|
aliases.append(a)
|
||||||
|
rootaliases = list(aliases)
|
||||||
for g in groups:
|
for g in groups:
|
||||||
resultalias.append(g)
|
treelist[g] = GroupTree(g)
|
||||||
resultstring.append(">> "+g)
|
|
||||||
group_aliases = conf.get(g, "group").split()
|
group_aliases = conf.get(g, "group").split()
|
||||||
for ga in group_aliases:
|
for ga in group_aliases:
|
||||||
if expandlist == False:
|
if ga in groups:
|
||||||
pass
|
treelist[g].children.append(ga)
|
||||||
elif expandlist == True or g in expandlist:
|
elif ga in aliases:
|
||||||
if conf.has_option(ga, "exec_string"):
|
treelist[g].aliases.append(ga)
|
||||||
resultalias.append(ga+" "+g)
|
|
||||||
result = "".join([" ", str(ga), " (", (conf.get(ga, "exec_string") if
|
|
||||||
conf.has_option(ga, "exec_string") else ""), ")",
|
|
||||||
(" [password]" if conf.has_option(ga, "password") else "")])
|
|
||||||
resultstring.append(result)
|
|
||||||
try:
|
try:
|
||||||
aliases.remove(ga)
|
rootaliases.remove(ga)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
for a in aliases:
|
for g in groups:
|
||||||
|
if treelist[g].children:
|
||||||
|
for child in treelist[g].children:
|
||||||
|
treelist[child].parent.append(g)
|
||||||
|
for g in groups:
|
||||||
|
if not treelist[g].parent:
|
||||||
|
rootgroups.append(g)
|
||||||
|
if group == False:
|
||||||
|
return aliases, groups, rootaliases, rootgroups, treelist
|
||||||
|
else:
|
||||||
|
childaliases = GroupChildRecursion(group, [], treelist)
|
||||||
|
return setSeq(childaliases)
|
||||||
|
|
||||||
|
|
||||||
|
def GroupTreeRecursion(level, group, treelist, resultalias, resultstring,
|
||||||
|
expandlist, previousgroups):
|
||||||
|
if group in previousgroups:
|
||||||
|
return resultalias, resultstring
|
||||||
|
previousgroups.append(group)
|
||||||
|
resultalias.append(group)
|
||||||
|
if expandlist == True or group in expandlist:
|
||||||
|
resultstring.append(' '*(level-1)+">> "+group)
|
||||||
|
for g in treelist[group].children:
|
||||||
|
resultalias, resultstring = GroupTreeRecursion(level+1, g,
|
||||||
|
treelist, resultalias, resultstring, expandlist, previousgroups)
|
||||||
|
for ga in treelist[group].aliases:
|
||||||
|
if conf.has_option(ga, "exec_string"):
|
||||||
|
resultalias.append(ga)
|
||||||
|
result = "".join([' '*(level-1)+" ~ ", str(ga), " (",
|
||||||
|
(conf.get(ga, "exec_string") if conf.has_option(ga,
|
||||||
|
"exec_string") else ""), ")",
|
||||||
|
(" [password]" if conf.has_option(ga, "password") else "")])
|
||||||
|
resultstring.append(result)
|
||||||
|
else:
|
||||||
|
resultstring.append(' '*(level-1)+"<> "+group)
|
||||||
|
return resultalias, resultstring
|
||||||
|
|
||||||
|
|
||||||
|
def GetTreeList(strings=True, expandlist=True):
|
||||||
|
resultalias = []
|
||||||
|
resultstring = []
|
||||||
|
aliases, groups, rootaliases, rootgroups, treelist = GroupChildAliases(False)
|
||||||
|
for g in rootgroups:
|
||||||
|
resultalias, resultstring = GroupTreeRecursion(1, g, treelist,
|
||||||
|
resultalias, resultstring, expandlist, [])
|
||||||
|
for a in rootaliases:
|
||||||
resultalias.append(a)
|
resultalias.append(a)
|
||||||
result = "".join([str(a), " (", (conf.get(a, "exec_string") if
|
result = "".join([str(a), " (", (conf.get(a, "exec_string") if
|
||||||
conf.has_option(a, "exec_string") else ""), ")",
|
conf.has_option(a, "exec_string") else ""), ")",
|
||||||
(" [password]" if conf.has_option(a, "password") else "")])
|
(" [password]" if conf.has_option(a, "password") else "")])
|
||||||
resultstring.append(result)
|
resultstring.append(result)
|
||||||
if strings:
|
if strings:
|
||||||
return resultstring;
|
return resultstring
|
||||||
else:
|
else:
|
||||||
return resultalias;
|
return resultalias
|
||||||
|
|
||||||
|
|
||||||
def CMDFullList(option, opt, value, parser):
|
def CMDFullList(option, opt, value, parser):
|
||||||
for p in GetTreeList():
|
for p in GetTreeList():
|
||||||
print (p)
|
print (p)
|
||||||
|
|
||||||
|
|
||||||
def CursesConnect(screen, aliases, command=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 = conf.get(alias, "group").split()
|
|
||||||
for ga in group_aliases:
|
|
||||||
if not ga in connectaliases:
|
|
||||||
connectaliases.append(ga)
|
|
||||||
else:
|
|
||||||
if not alias in connectaliases:
|
|
||||||
connectaliases.append(alias)
|
|
||||||
for alias in connectaliases:
|
|
||||||
print("Connecting to " + alias + "...")
|
|
||||||
ConnectAlias(alias, command)
|
|
||||||
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:
|
||||||
|
@ -276,9 +358,21 @@ def CursesExit(error=False):
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
class CursesTextpadEsc(Exception):
|
||||||
|
"ESC key has been pressed"
|
||||||
|
|
||||||
|
|
||||||
def CursesTextpadConfirm(value):
|
def CursesTextpadConfirm(value):
|
||||||
if value == 10:
|
if value == 10: # Enter
|
||||||
value = 7
|
value = 7
|
||||||
|
elif value == 27: # Esc
|
||||||
|
raise CursesTextpadEsc()
|
||||||
|
elif value == curses.KEY_DC: # Del
|
||||||
|
value = curses.ascii.EOT
|
||||||
|
elif value == curses.KEY_HOME: # Home
|
||||||
|
value = curses.ascii.SOH
|
||||||
|
elif value == curses.KEY_END: # End
|
||||||
|
value = curses.ascii.ENQ
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@ -351,6 +445,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 ",
|
||||||
|
@ -363,7 +458,7 @@ def CMDOptions():
|
||||||
"ssh user@somehost.com\n ",
|
"ssh user@somehost.com\n ",
|
||||||
"ssh gates@8.8.8.8 -p 667\n ",
|
"ssh gates@8.8.8.8 -p 667\n ",
|
||||||
"ssh root@somehost.com -t tmux a\n",
|
"ssh root@somehost.com -t tmux a\n",
|
||||||
"Also, you can edit config file manually: ", conf_file, "\n"]))
|
"Also, you can edit the config file manually: ", conf_file, "\n"]))
|
||||||
opts = FormatedParser(usage=usage, version="%prog " + version,
|
opts = FormatedParser(usage=usage, version="%prog " + version,
|
||||||
epilog=epilog)
|
epilog=epilog)
|
||||||
opts.add_option('-l', '--list', action="callback",
|
opts.add_option('-l', '--list', action="callback",
|
||||||
|
@ -379,7 +474,11 @@ def CMDOptions():
|
||||||
help="add new group for aliases")
|
help="add new group for aliases")
|
||||||
opts.add_option('-c', '--command', action="store", type="string",
|
opts.add_option('-c', '--command', action="store", type="string",
|
||||||
dest="command", metavar="command", default=False,
|
dest="command", metavar="command", default=False,
|
||||||
help="execute command for aliases or group")
|
help="execute command for aliases and groups")
|
||||||
|
opts.add_option('-t', '--thread', action="store", type="string",
|
||||||
|
dest="thread", metavar="command", default=False,
|
||||||
|
help="parallel command execution for aliases and groups " +
|
||||||
|
"(ssh key authentication or set password required)" )
|
||||||
opts.add_option('-k', '--keep', action="store", type="string",
|
opts.add_option('-k', '--keep', action="store", type="string",
|
||||||
dest="keep", metavar="alias", default=False,
|
dest="keep", metavar="alias", default=False,
|
||||||
help="hold connection with specified alias")
|
help="hold connection with specified alias")
|
||||||
|
@ -405,40 +504,44 @@ def CMDOptions():
|
||||||
CMDRemove(options.remove)
|
CMDRemove(options.remove)
|
||||||
if options.keep:
|
if options.keep:
|
||||||
HoldConnection(options.keep)
|
HoldConnection(options.keep)
|
||||||
if alias:
|
if options.thread:
|
||||||
CMDConnect(alias, options.command)
|
Connect(alias, options.thread, True)
|
||||||
|
elif alias:
|
||||||
|
Connect(alias, options.command)
|
||||||
|
|
||||||
# curses template from: https://stackoverflow.com/a/30828805/6224462
|
# curses template from: https://stackoverflow.com/a/30828805/6224462
|
||||||
|
|
||||||
|
|
||||||
def CursesMain():
|
def CursesMain():
|
||||||
help_screen = ("".join([" Press:\n",
|
help_screen = ("".join([" Press:\n",
|
||||||
" 'z'/'x' or arrows - navigation\n",
|
" 'z'/'x', 'w'/'s' or arrows - navigation\n",
|
||||||
" 'a'/'F2' - add new alias (without spaces)\n",
|
" 'a'/'F2' - add new alias (without spaces)\n",
|
||||||
" 'g'/'F5' - add new group (spaces will be stripped)\n",
|
" 'g'/'F5' - add new group (spaces will be stripped)\n",
|
||||||
" 'e'/'F4' - edit existing alias/group\n",
|
" 'e'/'F4' - edit existing alias/group\n",
|
||||||
" 'p'/'F6' - set alias's password for sshpass [UNSAFE]\n",
|
" 'p'/'F6' - set alias's password for sshpass [UNSAFE]\n",
|
||||||
" 'space'/'insert' - select\n",
|
" 'space'/'insert' - select\n",
|
||||||
" 'r'/'F8' - remove selected alias/aliases\n",
|
" 'r'/'F8' - remove selected alias/aliases\n",
|
||||||
" 'c'/'F3' - execute specific command with selected alias/aliases\n",
|
" 'c'/'F3' - command execution for alias (group/aliases - in turn)\n",
|
||||||
|
" 't'/'F11' - parallel command execution for aliases and groups\n",
|
||||||
|
" (ssh key authentication or set password required)\n",
|
||||||
" 'k'/'F7' - hold connection with selected alias\n",
|
" 'k'/'F7' - hold connection with selected alias\n",
|
||||||
" 'enter'/'F9' - connect to selected alias/aliases,\n",
|
" 'enter'/'F9' - connect to selected alias/aliases,\n",
|
||||||
" expand/collapse group\n",
|
" expand/collapse group\n",
|
||||||
" 'q'/'F10' - quit\n",
|
" 'q'/'F10' - quit\n",
|
||||||
" Run program with '--help' option to view command line help.\n",
|
" Run program with '--help' option to view command line help.\n",
|
||||||
" Also, you can edit config file manually:\n",
|
" Also, you can edit the config file manually:\n",
|
||||||
" ", conf_file]))
|
" ", conf_file]))
|
||||||
if expand_default == True:
|
if expand_default == True:
|
||||||
groups = []
|
groups = []
|
||||||
for a in conf.sections():
|
for a in conf.sections():
|
||||||
if conf.has_option(a, "group"):
|
if conf.has_option(a, "group"):
|
||||||
groups.append(a)
|
groups.append(a)
|
||||||
expanded = groups
|
expanded = list(groups)
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
stringsfull = GetTreeList(True, expanded)
|
stringsfull = GetTreeList(True, expanded)
|
||||||
elif expand_default == False:
|
elif expand_default == False:
|
||||||
groups = []
|
groups = []
|
||||||
expanded = groups
|
expanded = []
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
stringsfull = GetTreeList(True, expanded)
|
stringsfull = GetTreeList(True, expanded)
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
|
@ -492,12 +595,17 @@ def CursesMain():
|
||||||
new_alias_textpad = CursesTextpad(screen, 1, width - 8,
|
new_alias_textpad = CursesTextpad(screen, 1, width - 8,
|
||||||
(height // 2) - 1, 4, "Enter new alias:", "",
|
(height // 2) - 1, 4, "Enter new alias:", "",
|
||||||
normal_text, highlight_text)
|
normal_text, highlight_text)
|
||||||
|
try:
|
||||||
add_alias = new_alias_textpad.edit(CursesTextpadConfirm)
|
add_alias = new_alias_textpad.edit(CursesTextpadConfirm)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
add_alias = ""
|
||||||
|
if not add_alias == "":
|
||||||
add_alias = add_alias.split()[0].strip()
|
add_alias = add_alias.split()[0].strip()
|
||||||
if not add_alias == "":
|
if not add_alias == "":
|
||||||
add_result = AddNewAlias(add_alias)
|
add_result = AddNewAlias(add_alias)
|
||||||
if add_result == True:
|
if add_result == True:
|
||||||
add_string = ""
|
add_string = ""
|
||||||
|
try:
|
||||||
while add_string == "":
|
while add_string == "":
|
||||||
string_textpad = CursesTextpad(screen, 3,
|
string_textpad = CursesTextpad(screen, 3,
|
||||||
width - 8, (height // 2) - 1, 4,
|
width - 8, (height // 2) - 1, 4,
|
||||||
|
@ -507,6 +615,9 @@ def CursesMain():
|
||||||
CursesTextpadConfirm)
|
CursesTextpadConfirm)
|
||||||
SetAliasString(add_alias,
|
SetAliasString(add_alias,
|
||||||
add_string.replace("\n", "").rstrip())
|
add_string.replace("\n", "").rstrip())
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
RemoveAliases([add_alias])
|
||||||
|
add_alias = ""
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
stringsfull = GetTreeList(True, expanded)
|
stringsfull = GetTreeList(True, expanded)
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
|
@ -525,12 +636,17 @@ def CursesMain():
|
||||||
new_group_textpad = CursesTextpad(screen, 1, width - 8,
|
new_group_textpad = CursesTextpad(screen, 1, width - 8,
|
||||||
(height // 2) - 1, 4, "Enter group name (without spaces):", "",
|
(height // 2) - 1, 4, "Enter group name (without spaces):", "",
|
||||||
normal_text, highlight_text)
|
normal_text, highlight_text)
|
||||||
|
try:
|
||||||
add_group = new_group_textpad.edit(CursesTextpadConfirm)
|
add_group = new_group_textpad.edit(CursesTextpadConfirm)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
add_group = ""
|
||||||
|
if not add_group == "":
|
||||||
add_group = add_group.split()[0].strip()
|
add_group = add_group.split()[0].strip()
|
||||||
if not add_group == "":
|
if not add_group == "":
|
||||||
add_result = AddNewAlias(add_group)
|
add_result = AddNewAlias(add_group)
|
||||||
if add_result == True:
|
if add_result == True:
|
||||||
add_string = ""
|
add_string = ""
|
||||||
|
try:
|
||||||
while add_string.rstrip() == "":
|
while add_string.rstrip() == "":
|
||||||
string_textpad = CursesTextpad(screen, 3,
|
string_textpad = CursesTextpad(screen, 3,
|
||||||
width - 8, (height // 2) - 1, 4,
|
width - 8, (height // 2) - 1, 4,
|
||||||
|
@ -541,6 +657,9 @@ def CursesMain():
|
||||||
SetGroupString(add_group,
|
SetGroupString(add_group,
|
||||||
add_string.replace("\n", "").rstrip())
|
add_string.replace("\n", "").rstrip())
|
||||||
expanded.append(add_group)
|
expanded.append(add_group)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
RemoveAliases([add_group])
|
||||||
|
add_group = ""
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
stringsfull = GetTreeList(True, expanded)
|
stringsfull = GetTreeList(True, expanded)
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
|
@ -569,7 +688,12 @@ def CursesMain():
|
||||||
"group") if conf.has_option(strings[position - 1],
|
"group") if conf.has_option(strings[position - 1],
|
||||||
"group") else ""),
|
"group") else ""),
|
||||||
normal_text, highlight_text)
|
normal_text, highlight_text)
|
||||||
|
try:
|
||||||
edit_string = string_textpad.edit(CursesTextpadConfirm)
|
edit_string = string_textpad.edit(CursesTextpadConfirm)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
edit_string = conf.get(strings[position - 1],
|
||||||
|
"group") if conf.has_option(strings[position - 1],
|
||||||
|
"group") else ""
|
||||||
SetGroupString(strings[position - 1],
|
SetGroupString(strings[position - 1],
|
||||||
edit_string.replace("\n", "").rstrip())
|
edit_string.replace("\n", "").rstrip())
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
|
@ -586,7 +710,12 @@ def CursesMain():
|
||||||
"exec_string") if conf.has_option(strings[position - 1].split()[0].strip(),
|
"exec_string") if conf.has_option(strings[position - 1].split()[0].strip(),
|
||||||
"exec_string") else ""),
|
"exec_string") else ""),
|
||||||
normal_text, highlight_text)
|
normal_text, highlight_text)
|
||||||
|
try:
|
||||||
edit_string = string_textpad.edit(CursesTextpadConfirm)
|
edit_string = string_textpad.edit(CursesTextpadConfirm)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
edit_string = conf.get(strings[position - 1].split()[0].strip(),
|
||||||
|
"exec_string") if conf.has_option(strings[position - 1].split()[0].strip(),
|
||||||
|
"exec_string") else ""
|
||||||
SetAliasString(strings[position - 1].split()[0].strip(),
|
SetAliasString(strings[position - 1].split()[0].strip(),
|
||||||
edit_string.replace("\n", "").rstrip())
|
edit_string.replace("\n", "").rstrip())
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
|
@ -650,9 +779,34 @@ def CursesMain():
|
||||||
"Enter specific command to execute with selected ",
|
"Enter specific command to execute with selected ",
|
||||||
"alias/aliases:"]
|
"alias/aliases:"]
|
||||||
), "", normal_text, highlight_text)
|
), "", normal_text, highlight_text)
|
||||||
|
try:
|
||||||
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)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
command_string = ""
|
||||||
|
curses.curs_set(0)
|
||||||
|
if (key_pressed == ord('t') or key_pressed == ord(
|
||||||
|
'T') or key_pressed == curses.KEY_F11) and row_num != 0:
|
||||||
|
selected = []
|
||||||
|
for i in range(1, row_num + 1):
|
||||||
|
if selected_strings[i] == "*":
|
||||||
|
selected.append(strings[i - 1])
|
||||||
|
if not len(selected) > 0:
|
||||||
|
selected.append(strings[position - 1].split()[0].strip())
|
||||||
|
curses.curs_set(1)
|
||||||
|
command_textpad = CursesTextpad(screen, 3, width - 8,
|
||||||
|
(height // 2) - 1, 4,
|
||||||
|
"".join([
|
||||||
|
"Enter specific command to execute with selected ",
|
||||||
|
"alias/aliases:"]
|
||||||
|
), "", normal_text, highlight_text)
|
||||||
|
try:
|
||||||
|
command_string = command_textpad.edit(CursesTextpadConfirm)
|
||||||
|
Connect(selected, command_string.replace("\n", "").rstrip(),
|
||||||
|
True, screen)
|
||||||
|
except CursesTextpadEsc:
|
||||||
|
command_string = ""
|
||||||
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:
|
||||||
|
@ -687,7 +841,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:
|
||||||
|
@ -712,7 +866,8 @@ def CursesMain():
|
||||||
page = page + 1
|
page = page + 1
|
||||||
position = 1 + (max_row * (page - 1))
|
position = 1 + (max_row * (page - 1))
|
||||||
if key_pressed == curses.KEY_DOWN or key_pressed == ord(
|
if key_pressed == curses.KEY_DOWN or key_pressed == ord(
|
||||||
'x') or key_pressed == ord('X'):
|
'x') or key_pressed == ord('X') or key_pressed == ord(
|
||||||
|
's') or key_pressed == ord('S'):
|
||||||
if page == 1:
|
if page == 1:
|
||||||
if position < i:
|
if position < i:
|
||||||
position = position + 1
|
position = position + 1
|
||||||
|
@ -730,7 +885,8 @@ def CursesMain():
|
||||||
page = page + 1
|
page = page + 1
|
||||||
position = 1 + (max_row * (page - 1))
|
position = 1 + (max_row * (page - 1))
|
||||||
if key_pressed == curses.KEY_UP or key_pressed == ord(
|
if key_pressed == curses.KEY_UP or key_pressed == ord(
|
||||||
'z') or key_pressed == ord('Z'):
|
'z') or key_pressed == ord('Z') or key_pressed == ord(
|
||||||
|
'w') or key_pressed == ord('W'):
|
||||||
if page == 1:
|
if page == 1:
|
||||||
if position > 1:
|
if position > 1:
|
||||||
position = position - 1
|
position = position - 1
|
||||||
|
@ -786,10 +942,29 @@ if __name__ == "__main__":
|
||||||
except NameError:
|
except NameError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if not path.exists(conf_dir):
|
||||||
|
try:
|
||||||
|
from os import makedirs
|
||||||
|
makedirs(conf_dir)
|
||||||
|
except:
|
||||||
|
print("Can't make dir " + conf_dir)
|
||||||
|
exit()
|
||||||
|
|
||||||
conf = configparser.RawConfigParser()
|
conf = configparser.RawConfigParser()
|
||||||
|
|
||||||
if not path.exists(conf_file):
|
if not path.exists(conf_file):
|
||||||
|
try:
|
||||||
open(conf_file, 'w')
|
open(conf_file, 'w')
|
||||||
|
except:
|
||||||
|
print("Can't make file at " + conf_dir)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
try:
|
||||||
conf.read(conf_file)
|
conf.read(conf_file)
|
||||||
|
except:
|
||||||
|
print("Error: can't read config file " + conf_file)
|
||||||
|
exit()
|
||||||
|
|
||||||
if len(argv) > 1:
|
if len(argv) > 1:
|
||||||
try:
|
try:
|
||||||
CMDOptions()
|
CMDOptions()
|
||||||
|
@ -798,6 +973,10 @@ if __name__ == "__main__":
|
||||||
except configparser.Error:
|
except configparser.Error:
|
||||||
print("Error: can't parse your config file, please check it manually or make new one")
|
print("Error: can't parse your config file, please check it manually or make new one")
|
||||||
exit()
|
exit()
|
||||||
|
except IOError:
|
||||||
|
print("Error: can't use your config file, please check permissionss of " + conf_file)
|
||||||
|
exit()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
CursesMain()
|
CursesMain()
|
||||||
|
@ -809,3 +988,6 @@ if __name__ == "__main__":
|
||||||
except curses.error:
|
except curses.error:
|
||||||
CursesExit("".join(["Error: can't show some curses element, maybe ",
|
CursesExit("".join(["Error: can't show some curses element, maybe ",
|
||||||
"your terminal is too small"]))
|
"your terminal is too small"]))
|
||||||
|
except IOError:
|
||||||
|
CursesExit("".join(["Error: can't use your config file, please ",
|
||||||
|
"check permissionss of ", conf_file]))
|
||||||
|
|
Loading…
Reference in New Issue