code optimisation
This commit is contained in:
parent
6d3eeb8434
commit
8c4cdf3707
510
sshch.py
510
sshch.py
|
@ -4,36 +4,25 @@
|
||||||
from __future__ import division
|
from __future__ import division
|
||||||
from os import path
|
from os import path
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from optparse import OptionParser
|
|
||||||
from getpass import getpass
|
|
||||||
from curses import textpad, panel
|
|
||||||
from math import *
|
from math import *
|
||||||
|
from getpass import getpass
|
||||||
|
from optparse import OptionParser
|
||||||
|
from curses import textpad, panel
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import subprocess
|
import subprocess
|
||||||
import base64
|
import base64
|
||||||
import curses
|
import curses
|
||||||
|
|
||||||
# https://github.com/zlaxy/sshch
|
# https://github.com/zlaxy/sshch
|
||||||
version="0.3"
|
version="0.4"
|
||||||
# path to conf file, default: ~/.config/sshch.conf
|
# path to conf file, default: ~/.config/sshch.conf
|
||||||
conf_file = path.expanduser("~") + '/.config/sshch.conf'
|
conf_file = path.expanduser("~") + '/.config/sshch.conf'
|
||||||
|
|
||||||
help_screen = (" Press:\n 'z'/'x' or arrows - navigation\n 'a' - " +
|
|
||||||
"add new alias\n 'e' - edit existing alias\n 'p' - " +
|
|
||||||
"set alias's password for sshpass [UNSAFE]\n " +
|
|
||||||
"'space' - select\n 'r' - remove selected alias/" +
|
|
||||||
"aliases\n 'c' - execute specific command with " +
|
|
||||||
"selected alias/aliases\n 'enter' - connect to " +
|
|
||||||
"selected alias/aliases\n 'q' - quit\n Run sshch " +
|
|
||||||
"with '--help' option to view command line help.\n " +
|
|
||||||
"Also, you can edit config file manually:\n " +
|
|
||||||
conf_file)
|
|
||||||
|
|
||||||
def AddNewAlias(alias):
|
def AddNewAlias(alias):
|
||||||
if not conf.has_section(alias):
|
if not conf.has_section(alias):
|
||||||
conf.add_section(alias)
|
conf.add_section(alias)
|
||||||
conf.write(open(conf_file, "w"))
|
conf.write(open(conf_file, "w"))
|
||||||
return 1
|
return True
|
||||||
else:
|
else:
|
||||||
return "error: '" + alias + "' already exists"
|
return "error: '" + alias + "' already exists"
|
||||||
|
|
||||||
|
@ -41,72 +30,19 @@ def SetAliasString(alias, string):
|
||||||
conf.set(alias, "exec_string", string)
|
conf.set(alias, "exec_string", string)
|
||||||
conf.write(open(conf_file, "w"))
|
conf.write(open(conf_file, "w"))
|
||||||
|
|
||||||
def Add(alias):
|
|
||||||
if not conf.has_section(alias):
|
|
||||||
conf.add_section(alias)
|
|
||||||
promptadd = ("Enter connection string for new alias " +
|
|
||||||
"(example: ssh user@somehost.com):\n")
|
|
||||||
string = ""
|
|
||||||
while string == "":
|
|
||||||
string = raw_input (promptadd)
|
|
||||||
conf.set(alias, "exec_string", string)
|
|
||||||
conf.write(open(conf_file, "w"))
|
|
||||||
else:
|
|
||||||
print "error: '" + alias + "' already exists."
|
|
||||||
|
|
||||||
def Edit(alias):
|
|
||||||
if conf.has_section(alias):
|
|
||||||
promptedit = ("Enter connection string for existing alias " +
|
|
||||||
"(example: ssh user@somehost.com):\n")
|
|
||||||
string = ""
|
|
||||||
while string == "":
|
|
||||||
string = raw_input (promptedit)
|
|
||||||
conf.set(alias, "exec_string", string)
|
|
||||||
conf.write(open(conf_file, "w"))
|
|
||||||
else:
|
|
||||||
print "error: '" + alias + "' alias is not exists."
|
|
||||||
|
|
||||||
def List(option, opt, value, parser):
|
|
||||||
print ' '.join(str(p) for p in conf.sections())
|
|
||||||
|
|
||||||
def SetPassword(alias, string):
|
def SetPassword(alias, string):
|
||||||
string = base64.b64encode(base64.b16encode(
|
string = base64.b64encode(base64.b16encode(
|
||||||
base64.b32encode(string)))
|
base64.b32encode(string)))
|
||||||
conf.set(alias, "password", string)
|
conf.set(alias, "password", string)
|
||||||
conf.write(open(conf_file, "w"))
|
conf.write(open(conf_file, "w"))
|
||||||
|
|
||||||
def Password(alias):
|
|
||||||
if conf.has_section(alias):
|
|
||||||
promptpass = ("[UNSAFE] Enter password for sshpass: ")
|
|
||||||
string = ""
|
|
||||||
while string == "":
|
|
||||||
string = getpass (promptpass)
|
|
||||||
string = base64.b64encode(base64.b16encode(
|
|
||||||
base64.b32encode(string)))
|
|
||||||
conf.set(alias, "password", string)
|
|
||||||
conf.write(open(conf_file, "w"))
|
|
||||||
else:
|
|
||||||
print "error: '" + alias + "' alias is not exists."
|
|
||||||
|
|
||||||
def RemoveAliases(aliases):
|
def RemoveAliases(aliases):
|
||||||
for alias in aliases:
|
for alias in aliases:
|
||||||
conf.remove_section(alias)
|
conf.remove_section(alias)
|
||||||
conf.write(open(conf_file, "w"))
|
conf.write(open(conf_file, "w"))
|
||||||
|
|
||||||
def Remove(alias):
|
|
||||||
if conf.has_section(alias):
|
|
||||||
promptremove = ("Type 'yes' if you sure to remove " +
|
|
||||||
alias + " alias: ")
|
|
||||||
string = raw_input (promptremove)
|
|
||||||
if string == "yes":
|
|
||||||
conf.remove_section(alias)
|
|
||||||
conf.write(open(conf_file, "w"))
|
|
||||||
else:
|
|
||||||
print "'" + alias + "' alias was not deleted."
|
|
||||||
else:
|
|
||||||
print "error: '" + alias + "' alias is not exists."
|
|
||||||
|
|
||||||
def ConnectAlias(alias, command=False):
|
def ConnectAlias(alias, command=False):
|
||||||
|
print "Connecting to " + alias + "..."
|
||||||
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(
|
||||||
|
@ -117,93 +53,96 @@ def ConnectAlias(alias, command=False):
|
||||||
exec_string = exec_string + " " + command
|
exec_string = exec_string + " " + command
|
||||||
p = subprocess.Popen(exec_string, shell=True)
|
p = subprocess.Popen(exec_string, shell=True)
|
||||||
streamdata = p.communicate()[0]
|
streamdata = p.communicate()[0]
|
||||||
|
print "... " + alias + " session finished."
|
||||||
|
|
||||||
def CursesConnect(aliases, command=False):
|
def CMDAdd(alias):
|
||||||
for alias in aliases:
|
result = AddNewAlias(alias)
|
||||||
curses.endwin()
|
if result == True:
|
||||||
print "Connecting to " + alias + "..."
|
prompt_add = ("Enter connection string for new alias " +
|
||||||
ConnectAlias(alias, command)
|
"(example: ssh user@somehost.com):\n")
|
||||||
print "... " + alias + " finished."
|
string = ""
|
||||||
exit()
|
while string == "":
|
||||||
|
string = raw_input (prompt_add)
|
||||||
|
SetAliasString(alias, string)
|
||||||
|
else:
|
||||||
|
print result
|
||||||
|
|
||||||
def Connect(aliases, command):
|
def CMDEdit(alias):
|
||||||
|
if conf.has_section(alias):
|
||||||
|
prompt_edit = ("Enter connection string for existing alias " +
|
||||||
|
"(example: ssh user@somehost.com):\n")
|
||||||
|
string = ""
|
||||||
|
while string == "":
|
||||||
|
string = raw_input (prompt_edit)
|
||||||
|
SetAliasString(alias, string)
|
||||||
|
else:
|
||||||
|
print "error: '" + alias + "' alias is not exists"
|
||||||
|
|
||||||
|
def CMDPassword(alias):
|
||||||
|
if conf.has_section(alias):
|
||||||
|
prompt_pass = ("[UNSAFE] Enter password for sshpass: ")
|
||||||
|
string = ""
|
||||||
|
string = getpass (prompt_pass)
|
||||||
|
if not string == "": SetPassword(alias, string)
|
||||||
|
else:
|
||||||
|
print "error: '" + alias + "' alias is not exists"
|
||||||
|
|
||||||
|
def CMDRemove(alias):
|
||||||
|
if conf.has_section(alias):
|
||||||
|
prompt_remove = ("Type 'yes' if you sure to remove '" +
|
||||||
|
alias + "' alias: ")
|
||||||
|
string = raw_input (prompt_remove)
|
||||||
|
if string == "yes":
|
||||||
|
RemoveAliases([alias])
|
||||||
|
else:
|
||||||
|
print "'" + alias + "' alias was not deleted."
|
||||||
|
else:
|
||||||
|
print "error: '" + alias + "' alias is not exists."
|
||||||
|
|
||||||
|
def CMDConnect(aliases, command=False):
|
||||||
for alias in aliases:
|
for alias in aliases:
|
||||||
if conf.has_section(alias):
|
if conf.has_section(alias):
|
||||||
exec_string = ""
|
ConnectAlias(alias, command)
|
||||||
if conf.has_option(alias, "password"):
|
|
||||||
password = base64.b32decode(base64.b16decode(
|
|
||||||
base64.b64decode(conf.get(alias, "password"))))
|
|
||||||
exec_string = "sshpass -p " + password + " "
|
|
||||||
exec_string = exec_string + conf.get(alias, "exec_string")
|
|
||||||
if command:
|
|
||||||
exec_string = exec_string + " " + command
|
|
||||||
p = subprocess.Popen(exec_string, shell=True)
|
|
||||||
streamdata = p.communicate()[0]
|
|
||||||
else:
|
else:
|
||||||
print "error: '" + alias + "' alias is not exists."
|
print "error: '" + alias + "' alias is not exists"
|
||||||
|
|
||||||
def Options():
|
def CMDList(option, opt, value, parser):
|
||||||
class FormatedParser(OptionParser):
|
print ', '.join(str(p) for p in conf.sections())
|
||||||
def format_epilog(self, formatter):
|
|
||||||
return self.epilog
|
|
||||||
|
|
||||||
usage = "usage: %prog [options] [aliases]"
|
def CursesConnect(screen, aliases, command=False):
|
||||||
progname = path.basename(__file__)
|
curses.endwin()
|
||||||
epilog = ("Examples:\n " + progname + " existingalias\n " +
|
for alias in aliases:
|
||||||
progname + " -a newremoteserver\n " + progname +
|
ConnectAlias(alias, command)
|
||||||
" --edit=newremoteserver -p newremoteserver\n " +
|
print "Press 'enter' to continue."
|
||||||
progname + ' -c "ls -l" newremoteserver\n ' + progname +
|
screen.getch()
|
||||||
" -c reboot existingalias newremoteserver\n" +
|
|
||||||
"Examples of connection string:\n " +
|
|
||||||
"ssh user@somehost.com\n " +
|
|
||||||
"ssh gates@8.8.8.8 -p 667\n " +
|
|
||||||
"ssh root@somehost.com -t tmux a\n" +
|
|
||||||
"Also, you can edit config file manually: " +
|
|
||||||
conf_file + "\n")
|
|
||||||
opts = FormatedParser(usage=usage, version="%prog " + version,
|
|
||||||
epilog=epilog)
|
|
||||||
opts.add_option('-l', '--list', action = "callback", callback=List,
|
|
||||||
help="show list of all existing aliases")
|
|
||||||
opts.add_option('-a', '--add', action="store",
|
|
||||||
type="string", dest="add",
|
|
||||||
metavar="alias", default=False,
|
|
||||||
help="add new alias for connection string")
|
|
||||||
opts.add_option('-c', '--command', action="store",
|
|
||||||
type="string", dest="command",
|
|
||||||
metavar="command", default=False,
|
|
||||||
help="add command for executing alias")
|
|
||||||
opts.add_option('-e', '--edit', action="store",
|
|
||||||
type="string", dest='edit',
|
|
||||||
metavar="alias", default=False,
|
|
||||||
help="edit existing connection string")
|
|
||||||
opts.add_option('-p', '--password', action="store",
|
|
||||||
type="string", dest='password',
|
|
||||||
metavar="alias", default=False,
|
|
||||||
help="set and store password for sshpass [UNSAFE]")
|
|
||||||
opts.add_option('-r', '--remove', action="store",
|
|
||||||
type="string", dest='remove',
|
|
||||||
metavar="alias", default=False,
|
|
||||||
help="remove existing alias of connection string")
|
|
||||||
|
|
||||||
options, alias = opts.parse_args()
|
def CursesExit():
|
||||||
if options.add:
|
curses.endwin()
|
||||||
Add(options.add)
|
exit()
|
||||||
if options.edit:
|
|
||||||
Edit(options.edit)
|
|
||||||
if options.password:
|
|
||||||
Password(options.password)
|
|
||||||
if options.remove:
|
|
||||||
Remove(options.remove)
|
|
||||||
if alias:
|
|
||||||
Connect(alias, options.command)
|
|
||||||
|
|
||||||
def TextBoxConfirm(value):
|
def CursesTextpadConfirm(value):
|
||||||
if value == 10:
|
if value == 10:
|
||||||
value = 7
|
value = 7
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def Panel(screen, h, w, y, x, text, text_colorpair=0, deco_colorpair=0,
|
def CursesTextpad(screen, h, w, y, x, title="", value="",
|
||||||
confirm=0):
|
text_colorpair=0, deco_colorpair=0):
|
||||||
|
new_window = curses.newwin(h + 3, w + 2, y - 1, x - 1)
|
||||||
|
title_window = new_window.subwin(1, w , y , x)
|
||||||
|
title_window.addstr(0, 0, title, text_colorpair)
|
||||||
|
title_window.refresh()
|
||||||
|
sub_window = new_window.subwin(h, w, y + 1, x)
|
||||||
|
textbox_field = textpad.Textbox(sub_window, insert_mode=True)
|
||||||
|
new_window.attron(deco_colorpair)
|
||||||
|
new_window.box()
|
||||||
|
new_window.attroff(deco_colorpair)
|
||||||
|
new_window.refresh()
|
||||||
|
sub_window.addstr(0, 0 , value, text_colorpair)
|
||||||
|
sub_window.attron(text_colorpair)
|
||||||
|
return textbox_field
|
||||||
|
|
||||||
|
def CursesPanel(screen, h, w, y, x, text,
|
||||||
|
text_colorpair=0, deco_colorpair=0, confirm=0):
|
||||||
new_window = curses.newwin(h, w, y, x)
|
new_window = curses.newwin(h, w, y, x)
|
||||||
new_window.erase()
|
new_window.erase()
|
||||||
new_window.attron(deco_colorpair)
|
new_window.attron(deco_colorpair)
|
||||||
|
@ -218,7 +157,7 @@ def Panel(screen, h, w, y, x, text, text_colorpair=0, deco_colorpair=0,
|
||||||
hidden_password = ""
|
hidden_password = ""
|
||||||
keych = ""
|
keych = ""
|
||||||
position = 2
|
position = 2
|
||||||
while 1:
|
while True:
|
||||||
keych = screen.getch()
|
keych = screen.getch()
|
||||||
if keych == ord("\n"):
|
if keych == ord("\n"):
|
||||||
break
|
break
|
||||||
|
@ -246,31 +185,64 @@ def Panel(screen, h, w, y, x, text, text_colorpair=0, deco_colorpair=0,
|
||||||
else:
|
else:
|
||||||
screen.getch()
|
screen.getch()
|
||||||
|
|
||||||
def TextBox(screen, h, w, y, x, title="", value="", text_colorpair=0,
|
def CMDOptions():
|
||||||
deco_colorpair=0):
|
class FormatedParser(OptionParser):
|
||||||
new_window = curses.newwin(h + 3, w + 2, y - 1, x - 1)
|
def format_epilog(self, formatter):
|
||||||
title_window = new_window.subwin(1, w , y , x)
|
return self.epilog
|
||||||
title_window.addstr(0, 0, title, text_colorpair)
|
usage = "usage: %prog [options] [aliases]"
|
||||||
title_window.refresh()
|
progname = path.basename(__file__)
|
||||||
sub_window = new_window.subwin(h, w, y + 1, x)
|
epilog = ("Examples:\n " +
|
||||||
textbox_field = textpad.Textbox(sub_window, insert_mode=True)
|
progname + " existingalias\n " +
|
||||||
new_window.attron(deco_colorpair)
|
progname + " -a newremoteserver\n " +
|
||||||
new_window.box()
|
progname + " --edit=newremoteserver -p newremoteserver\n " +
|
||||||
new_window.attroff(deco_colorpair)
|
progname + ' -c "ls -l" newremoteserver\n ' +
|
||||||
new_window.refresh()
|
progname + " -c reboot existingalias newremoteserver\n"
|
||||||
sub_window.addstr(0, 0 , value, text_colorpair)
|
"Examples of connection string:\n " +
|
||||||
sub_window.attron(text_colorpair)
|
"ssh user@somehost.com\n " +
|
||||||
return textbox_field
|
"ssh gates@8.8.8.8 -p 667\n " +
|
||||||
|
"ssh root@somehost.com -t tmux a\n" +
|
||||||
def CursesExit(screen):
|
"Also, you can edit config file manually: " + conf_file + "\n")
|
||||||
curses.nocbreak()
|
opts = FormatedParser(usage=usage, version="%prog " + version,
|
||||||
screen.keypad(0)
|
epilog=epilog)
|
||||||
curses.echo()
|
opts.add_option('-l', '--list', action = "callback",
|
||||||
curses.endwin()
|
callback=CMDList, help="show list of all existing aliases")
|
||||||
exit()
|
opts.add_option('-a', '--add', action="store", type="string",
|
||||||
|
dest="add", metavar="alias", default=False,
|
||||||
|
help="add new alias for connection string")
|
||||||
|
opts.add_option('-c', '--command', action="store", type="string",
|
||||||
|
dest="command", metavar="command", default=False,
|
||||||
|
help="add command for executing alias")
|
||||||
|
opts.add_option('-e', '--edit', action="store", type="string",
|
||||||
|
dest='edit', metavar="alias", default=False,
|
||||||
|
help="edit existing connection string")
|
||||||
|
opts.add_option('-p', '--password', action="store", type="string",
|
||||||
|
dest='password', metavar="alias", default=False,
|
||||||
|
help="set and store password for sshpass [UNSAFE]")
|
||||||
|
opts.add_option('-r', '--remove', action="store", type="string",
|
||||||
|
dest='remove', metavar="alias", default=False,
|
||||||
|
help="remove existing alias of connection string")
|
||||||
|
options, alias = opts.parse_args()
|
||||||
|
if options.add: CMDAdd(options.add)
|
||||||
|
if options.edit: CMDEdit(options.edit)
|
||||||
|
if options.password: CMDPassword(options.password)
|
||||||
|
if options.remove: CMDRemove(options.remove)
|
||||||
|
if alias: CMDConnect(alias, options.command)
|
||||||
|
|
||||||
# curses template from: https://stackoverflow.com/a/30828805/6224462
|
# curses template from: https://stackoverflow.com/a/30828805/6224462
|
||||||
def MainScreen():
|
def CursesMain():
|
||||||
|
help_screen = (" Press:\n" +
|
||||||
|
" 'z'/'x' or arrows - navigation\n" +
|
||||||
|
" 'a'/'F2' - add new alias\n" +
|
||||||
|
" 'e'/'F4' - edit existing alias\n" +
|
||||||
|
" 'p'/'F6' - set alias's password for sshpass [UNSAFE]\n" +
|
||||||
|
" 'space'/'insert' - select\n" +
|
||||||
|
" 'r'/'F8' - remove selected alias/aliases\n" +
|
||||||
|
" 'c'/'F3' - execute specific command with selected alias/aliases\n" +
|
||||||
|
" 'enter'/'F9' - connect to selected alias/aliases\n" +
|
||||||
|
" 'q'/'F10' - quit\n" +
|
||||||
|
" Run program with '--help' option to view command line help.\n" +
|
||||||
|
" Also, you can edit config file manually:\n" +
|
||||||
|
" " + conf_file)
|
||||||
strings = conf.sections()
|
strings = conf.sections()
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
selected_strings = [" " for i in range(0, row_num + 1)]
|
selected_strings = [" " for i in range(0, row_num + 1)]
|
||||||
|
@ -299,91 +271,93 @@ def MainScreen():
|
||||||
else:
|
else:
|
||||||
if (i == position):
|
if (i == position):
|
||||||
box.addnstr(i, 2, "[" + selected_strings[i] + "] " +
|
box.addnstr(i, 2, "[" + selected_strings[i] + "] " +
|
||||||
str(i) + " " + strings[i - 1] + " (" +
|
str(i) + " " + strings[i - 1] + " (" +
|
||||||
conf.get(strings[i - 1], "exec_string") +
|
conf.get(strings[i - 1], "exec_string") + ")",
|
||||||
")", width - 6, highlight_text)
|
width - 6, highlight_text)
|
||||||
else:
|
else:
|
||||||
box.addnstr(i, 2, "[" + selected_strings[i] + "] " +
|
box.addnstr(i, 2, "[" + selected_strings[i] + "] " +
|
||||||
str(i) + " " + strings[i - 1] + " (" +
|
str(i) + " " + strings[i - 1] + " (" +
|
||||||
conf.get(strings[i - 1], "exec_string") +
|
conf.get(strings[i - 1], "exec_string") + ")",
|
||||||
")", width - 6, normal_text)
|
width - 6, normal_text)
|
||||||
if i == row_num:
|
if i == row_num:
|
||||||
break
|
break
|
||||||
screen.refresh()
|
screen.refresh()
|
||||||
box.refresh()
|
box.refresh()
|
||||||
key_pressed = screen.getch()
|
key_pressed = screen.getch()
|
||||||
while 1:
|
while True:
|
||||||
if key_pressed == ord('q') or key_pressed == ord(
|
if key_pressed == ord('q') or key_pressed == ord(
|
||||||
'Q') or key_pressed == 27:
|
'Q') or key_pressed == (
|
||||||
CursesExit(screen)
|
27) or key_pressed == curses.KEY_F10:
|
||||||
|
CursesExit()
|
||||||
if key_pressed == ord('h') or key_pressed == ord(
|
if key_pressed == ord('h') or key_pressed == ord(
|
||||||
'H') or key_pressed == 265:
|
'H') or key_pressed == curses.KEY_F1:
|
||||||
Panel(screen, height - 4, width - 6, 2, 3, help_screen,
|
CursesPanel(screen, height - 4, width - 6, 2, 3,
|
||||||
normal_text, highlight_text)
|
help_screen, normal_text, highlight_text)
|
||||||
if key_pressed == ord('a') or key_pressed == ord('A'):
|
if key_pressed == ord('a') or key_pressed == ord(
|
||||||
newalias = TextBox(screen, 1, width - 8, (height // 2) - 1,
|
'A') or key_pressed == curses.KEY_F2:
|
||||||
4, "Enter new alias:", "", normal_text,
|
new_alias_textpad = CursesTextpad(screen, 1, width - 8,
|
||||||
highlight_text)
|
(height // 2) - 1, 4, "Enter new alias:", "",
|
||||||
addalias = newalias.edit(TextBoxConfirm)
|
normal_text, highlight_text)
|
||||||
if not addalias.rstrip() == "":
|
add_alias = new_alias_textpad.edit(CursesTextpadConfirm)
|
||||||
addresult = AddNewAlias(addalias.rstrip())
|
if not add_alias.rstrip() == "":
|
||||||
if not addresult == 1: Panel(screen, 3, width - 6,
|
add_result = AddNewAlias(add_alias.rstrip())
|
||||||
(height // 2) - 1, 3,
|
if not add_result == True: CursesPanel(screen, 3,
|
||||||
addresult, normal_text,
|
width - 6, (height // 2) - 1, 3, add_result,
|
||||||
highlight_text)
|
normal_text, highlight_text)
|
||||||
else:
|
else:
|
||||||
addstr = ""
|
add_string = ""
|
||||||
while addstr.rstrip() == "":
|
while add_string.rstrip() == "":
|
||||||
newstr = TextBox(screen, 3, width - 8,
|
string_textpad = CursesTextpad(screen, 3,
|
||||||
(height // 2) - 1, 4,
|
width - 8, (height // 2) - 1, 4,
|
||||||
"Enter full execution string:",
|
"Enter full execution string:",
|
||||||
"ssh ", normal_text,
|
"ssh ", normal_text, highlight_text)
|
||||||
highlight_text)
|
add_string = string_textpad.edit(
|
||||||
addstr = newstr.edit(TextBoxConfirm)
|
CursesTextpadConfirm)
|
||||||
SetAliasString(addalias.rstrip(), addstr.rstrip())
|
SetAliasString(add_alias.rstrip(),
|
||||||
|
add_string.rstrip())
|
||||||
strings = conf.sections()
|
strings = conf.sections()
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
selected_strings.append(" ")
|
selected_strings.append(" ")
|
||||||
pages = int(ceil(row_num / max_row))
|
pages = int(ceil(row_num / max_row))
|
||||||
box.refresh()
|
box.refresh()
|
||||||
if key_pressed == ord('e') or key_pressed == ord('E'):
|
if (key_pressed == ord('e') or key_pressed == ord(
|
||||||
editstr = ""
|
'E') or key_pressed == curses.KEY_F4) and row_num != 0:
|
||||||
while editstr.rstrip() == "":
|
edit_string = ""
|
||||||
newstr = TextBox(screen, 3, width - 8,
|
while edit_string.rstrip() == "":
|
||||||
(height // 2) - 1, 4,
|
string_textpad = CursesTextpad(screen, 3, width - 8,
|
||||||
"Enter new execution string:",
|
(height // 2) - 1, 4, "Enter new execution string:",
|
||||||
conf.get(strings[position - 1],
|
conf.get(strings[position - 1], "exec_string"),
|
||||||
"exec_string"),
|
normal_text, highlight_text)
|
||||||
normal_text, highlight_text)
|
edit_string = string_textpad.edit(CursesTextpadConfirm)
|
||||||
editstr = newstr.edit(TextBoxConfirm)
|
SetAliasString(strings[position - 1], edit_string.rstrip())
|
||||||
SetAliasString(strings[position - 1], editstr.rstrip())
|
|
||||||
strings = conf.sections()
|
strings = conf.sections()
|
||||||
if key_pressed == ord('p') or key_pressed == ord('P'):
|
if (key_pressed == ord('p') or key_pressed == ord(
|
||||||
password = ""
|
'P') or key_pressed == curses.KEY_F6) and row_num != 0:
|
||||||
password = Panel(screen, 4, width - 6, (height // 2) - 1, 3,
|
set_password = ""
|
||||||
" Enter user password for sshpass and " +
|
set_password = CursesPanel(screen, 4, width - 6,
|
||||||
"press 'enter':\n>", normal_text,
|
(height // 2) - 1, 3,
|
||||||
highlight_text, "password")
|
" Enter user password for sshpass and press 'enter':" +
|
||||||
if not password == "":
|
"\n>", normal_text, highlight_text, "password")
|
||||||
SetPassword(strings[position - 1], password)
|
if not set_password == "":
|
||||||
if key_pressed == ord('r') or key_pressed == ord('R'):
|
SetPassword(strings[position - 1], set_password)
|
||||||
|
if (key_pressed == ord('r') or key_pressed == ord(
|
||||||
|
'R') or key_pressed == curses.KEY_F8 or key_pressed == (
|
||||||
|
curses.KEY_DC)) and row_num != 0:
|
||||||
selected = []
|
selected = []
|
||||||
for i in range(1, row_num + 1):
|
for i in range(1, row_num + 1):
|
||||||
if selected_strings[i] == "*":
|
if selected_strings[i] == "*":
|
||||||
selected.append(strings[i - 1])
|
selected.append(strings[i - 1])
|
||||||
if len(selected) > 0:
|
if len(selected) > 0:
|
||||||
remove_confirm = ("Are you sure to remove " +
|
remove_confirm = ("Are you sure to remove " +
|
||||||
str(len(selected)) +
|
str(len(selected)) + " selected aliases? (y/N)")
|
||||||
" selected aliases? (y/N)")
|
|
||||||
else:
|
else:
|
||||||
remove_confirm = ("Are you sure to remove '" +
|
remove_confirm = ("Are you sure to remove '" +
|
||||||
strings[position - 1] +
|
strings[position - 1] + "' alias? (y/N)")
|
||||||
"' alias? (y/N)")
|
|
||||||
selected.append(strings[position - 1])
|
selected.append(strings[position - 1])
|
||||||
remove = Panel(screen, 4, width - 6, (height // 2) - 1, 3,
|
remove_result = CursesPanel(screen, 4, width - 6,
|
||||||
remove_confirm, normal_text, highlight_text,
|
(height // 2) - 1, 3, remove_confirm, normal_text,
|
||||||
"remove")
|
highlight_text, "remove")
|
||||||
if remove == "confirm":
|
if remove_result == "confirm":
|
||||||
RemoveAliases(selected)
|
RemoveAliases(selected)
|
||||||
strings = conf.sections()
|
strings = conf.sections()
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
|
@ -392,20 +366,49 @@ def MainScreen():
|
||||||
position = 1
|
position = 1
|
||||||
page = 1
|
page = 1
|
||||||
box.refresh()
|
box.refresh()
|
||||||
if key_pressed == ord('c') or key_pressed == ord('C'):
|
if (key_pressed == ord('c') or key_pressed == ord(
|
||||||
|
'C') or key_pressed == curses.KEY_F3) and row_num != 0:
|
||||||
selected = []
|
selected = []
|
||||||
for i in range(1, row_num + 1):
|
for i in range(1, row_num + 1):
|
||||||
if selected_strings[i] == "*":
|
if selected_strings[i] == "*":
|
||||||
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])
|
selected.append(strings[position - 1])
|
||||||
newcstr = TextBox(screen, 3, width - 8,
|
command_textpad = CursesTextpad(screen, 3, width - 8,
|
||||||
(height // 2) - 1, 4,
|
(height // 2) - 1, 4,
|
||||||
"Enter specific command to execute with" +
|
"Enter specific command to execute with selected " +
|
||||||
" selected alias/aliases:", "",
|
"alias/aliases:", "", normal_text, highlight_text)
|
||||||
normal_text, highlight_text)
|
command_string = command_textpad.edit(CursesTextpadConfirm)
|
||||||
cstr = newcstr.edit(TextBoxConfirm)
|
CursesConnect(screen, selected, command_string.rstrip())
|
||||||
CursesConnect(selected, cstr.rstrip())
|
if (key_pressed == ord("\n") or key_pressed == (
|
||||||
|
curses.KEY_F9)) 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])
|
||||||
|
CursesConnect(screen, selected)
|
||||||
|
if key_pressed == 32 or key_pressed == curses.KEY_IC:
|
||||||
|
if selected_strings[position] == ' ':
|
||||||
|
selected_strings[position] = '*'
|
||||||
|
else: selected_strings[position] = ' '
|
||||||
|
if page == 1:
|
||||||
|
if position < i:
|
||||||
|
position = position + 1
|
||||||
|
else:
|
||||||
|
if pages > 1:
|
||||||
|
page = page + 1
|
||||||
|
position = 1 + (max_row * (page - 1))
|
||||||
|
elif page == pages:
|
||||||
|
if position < row_num:
|
||||||
|
position = position + 1
|
||||||
|
else:
|
||||||
|
if position < max_row + (max_row * (page - 1)):
|
||||||
|
position = position + 1
|
||||||
|
else:
|
||||||
|
page = 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'):
|
||||||
if page == 1:
|
if page == 1:
|
||||||
|
@ -440,24 +443,11 @@ def MainScreen():
|
||||||
if page > 1:
|
if page > 1:
|
||||||
page = page - 1
|
page = page - 1
|
||||||
position = 1 + (max_row * (page - 1))
|
position = 1 + (max_row * (page - 1))
|
||||||
|
|
||||||
if key_pressed == curses.KEY_RIGHT or (key_pressed ==
|
if key_pressed == curses.KEY_RIGHT or (key_pressed ==
|
||||||
curses.KEY_NPAGE):
|
curses.KEY_NPAGE):
|
||||||
if page < pages:
|
if page < pages:
|
||||||
page = page + 1
|
page = page + 1
|
||||||
position = (1 + (max_row * (page - 1)))
|
position = (1 + (max_row * (page - 1)))
|
||||||
if key_pressed == ord("\n") 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])
|
|
||||||
CursesConnect(selected)
|
|
||||||
if key_pressed == 32:
|
|
||||||
if selected_strings[position] == ' ':
|
|
||||||
selected_strings[position] = '*'
|
|
||||||
else: selected_strings[position] = ' '
|
|
||||||
box.erase()
|
box.erase()
|
||||||
screen.border(0)
|
screen.border(0)
|
||||||
box.border(0)
|
box.border(0)
|
||||||
|
@ -470,24 +460,20 @@ def MainScreen():
|
||||||
if (i + (max_row * (page - 1)) == (position +
|
if (i + (max_row * (page - 1)) == (position +
|
||||||
(max_row * (page - 1)))):
|
(max_row * (page - 1)))):
|
||||||
box.addnstr(i - (max_row * (page - 1)), 2, "[" +
|
box.addnstr(i - (max_row * (page - 1)), 2, "[" +
|
||||||
selected_strings[i] + "] " + str(i) +
|
selected_strings[i] + "] " + str(i) + " " +
|
||||||
" " + strings[i - 1] + " (" +
|
strings[i - 1] + " (" + conf.get(strings[i - 1],
|
||||||
conf.get(strings[i - 1],
|
"exec_string") + ")", width - 6, highlight_text)
|
||||||
"exec_string") + ")", width - 6,
|
|
||||||
highlight_text)
|
|
||||||
else:
|
else:
|
||||||
box.addnstr(i - (max_row * (page - 1)), 2, "[" +
|
box.addnstr(i - (max_row * (page - 1)), 2, "[" +
|
||||||
selected_strings[i] + "] " + str(i) +
|
selected_strings[i] + "] " + str(i) + " " +
|
||||||
" " + strings[i - 1] + " (" +
|
strings[i - 1] + " (" + conf.get(strings[i - 1],
|
||||||
conf.get(strings[i - 1],
|
"exec_string") + ")", width - 6, normal_text)
|
||||||
"exec_string") + ")", width - 6,
|
|
||||||
normal_text)
|
|
||||||
if i == row_num:
|
if i == row_num:
|
||||||
break
|
break
|
||||||
screen.refresh()
|
screen.refresh()
|
||||||
box.refresh()
|
box.refresh()
|
||||||
key_pressed = screen.getch()
|
key_pressed = screen.getch()
|
||||||
CursesExit(screen)
|
CursesExit()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
conf = ConfigParser.RawConfigParser()
|
conf = ConfigParser.RawConfigParser()
|
||||||
|
@ -495,6 +481,6 @@ if __name__ == "__main__":
|
||||||
open(conf_file, 'w')
|
open(conf_file, 'w')
|
||||||
conf.read(conf_file)
|
conf.read(conf_file)
|
||||||
if len(argv) > 1:
|
if len(argv) > 1:
|
||||||
Options()
|
CMDOptions()
|
||||||
else:
|
else:
|
||||||
MainScreen()
|
CursesMain()
|
||||||
|
|
Loading…
Reference in New Issue