|
|
|
@@ -1,20 +1,26 @@
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
try:
|
|
|
|
|
import configparser
|
|
|
|
|
except ImportError:
|
|
|
|
|
import ConfigParser as configparser # Python 2.x import
|
|
|
|
|
|
|
|
|
|
from os import path
|
|
|
|
|
from sys import argv
|
|
|
|
|
from math import ceil
|
|
|
|
|
from getpass import getpass
|
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
from curses import textpad
|
|
|
|
|
import ConfigParser
|
|
|
|
|
import subprocess
|
|
|
|
|
import base64
|
|
|
|
|
import time
|
|
|
|
|
import curses
|
|
|
|
|
from curses import textpad, panel
|
|
|
|
|
|
|
|
|
|
# https://github.com/zlaxy/sshch
|
|
|
|
|
version = "0.55"
|
|
|
|
|
version = "0.9"
|
|
|
|
|
# path to conf file, default: ~/.config/sshch.conf
|
|
|
|
|
conf_file = path.expanduser("~") + '/.config/sshch.conf'
|
|
|
|
|
|
|
|
|
@@ -47,19 +53,25 @@ def RemoveAliases(aliases):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ConnectAlias(alias, command=False):
|
|
|
|
|
print "Connecting to " + alias + "..."
|
|
|
|
|
exec_string = ""
|
|
|
|
|
if conf.has_option(alias, "password"):
|
|
|
|
|
password = base64.b32decode(base64.b16decode(
|
|
|
|
|
base64.b64decode(conf.get(alias, "password"))))
|
|
|
|
|
exec_string = "sshpass -p " + password + " "
|
|
|
|
|
exec_string = 'sshpass -p "' + password + '" '
|
|
|
|
|
if conf.has_option(alias, "exec_string"):
|
|
|
|
|
exec_string = exec_string + conf.get(alias, "exec_string")
|
|
|
|
|
if command:
|
|
|
|
|
exec_string = exec_string + " " + command
|
|
|
|
|
# Variables bellow is newer used
|
|
|
|
|
subprocess.Popen(exec_string, shell=True)
|
|
|
|
|
# p.communicate()[0]
|
|
|
|
|
print "... " + alias + " session finished."
|
|
|
|
|
subprocess.Popen(exec_string, shell=True).communicate()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def HoldConnection(alias):
|
|
|
|
|
print("Connecting to " + alias + ". Press CTRL+C to cancel.")
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
while True:
|
|
|
|
|
ConnectAlias(alias)
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CMDAdd(alias):
|
|
|
|
@@ -69,10 +81,10 @@ def CMDAdd(alias):
|
|
|
|
|
"(example: ssh user@somehost.com):\n"]))
|
|
|
|
|
string = ""
|
|
|
|
|
while string == "":
|
|
|
|
|
string = raw_input(prompt_add)
|
|
|
|
|
string = input(prompt_add)
|
|
|
|
|
SetAliasString(alias, string)
|
|
|
|
|
else:
|
|
|
|
|
print result
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CMDEdit(alias):
|
|
|
|
@@ -81,10 +93,10 @@ def CMDEdit(alias):
|
|
|
|
|
"(example: ssh user@somehost.com):\n"]))
|
|
|
|
|
string = ""
|
|
|
|
|
while string == "":
|
|
|
|
|
string = raw_input(prompt_edit)
|
|
|
|
|
string = input(prompt_edit)
|
|
|
|
|
SetAliasString(alias, string)
|
|
|
|
|
else:
|
|
|
|
|
print "error: '" + alias + "' alias is not exists"
|
|
|
|
|
print("error: '" + alias + "' alias is not exists")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CMDPassword(alias):
|
|
|
|
@@ -95,52 +107,57 @@ def CMDPassword(alias):
|
|
|
|
|
if not string == "":
|
|
|
|
|
SetPassword(alias, string)
|
|
|
|
|
else:
|
|
|
|
|
print "error: '" + alias + "' alias is not exists"
|
|
|
|
|
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)
|
|
|
|
|
string = input(prompt_remove)
|
|
|
|
|
if string == "yes":
|
|
|
|
|
RemoveAliases([alias])
|
|
|
|
|
else:
|
|
|
|
|
print "'" + alias + "' alias was not deleted."
|
|
|
|
|
print("'" + alias + "' alias was not deleted.")
|
|
|
|
|
else:
|
|
|
|
|
print "error: '" + alias + "' alias is not exists."
|
|
|
|
|
print("error: '" + alias + "' alias is not exists.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CMDConnect(aliases, command=False):
|
|
|
|
|
for alias in aliases:
|
|
|
|
|
if conf.has_section(alias):
|
|
|
|
|
print("Connecting to " + alias + "...")
|
|
|
|
|
ConnectAlias(alias, command)
|
|
|
|
|
print("... " + alias + " session finished.")
|
|
|
|
|
else:
|
|
|
|
|
print "error: '" + alias + "' alias is not exists"
|
|
|
|
|
print("error: '" + alias + "' alias is not exists")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 CMDFullList(option, opt, value, parser):
|
|
|
|
|
for p in conf.sections():
|
|
|
|
|
to_print = "".join([str(p), " - ", conf.get(p, "exec_string"),
|
|
|
|
|
(" [password]" if conf.has_option(p, "password") else ""), "\n"])
|
|
|
|
|
to_print = "".join([str(p), " - ", (conf.get(p, "exec_string") if
|
|
|
|
|
conf.has_option(p, "exec_string") else ""),
|
|
|
|
|
(" [password]" if conf.has_option(p, "password") else "")])
|
|
|
|
|
print(to_print)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CursesConnect(screen, aliases, command=False):
|
|
|
|
|
curses.endwin()
|
|
|
|
|
for alias in aliases:
|
|
|
|
|
print("Connecting to " + alias + "...")
|
|
|
|
|
ConnectAlias(alias, command)
|
|
|
|
|
print "Press 'enter' to continue."
|
|
|
|
|
print("... " + alias + " session finished.")
|
|
|
|
|
print("Press 'enter' to continue.")
|
|
|
|
|
screen.getch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def CursesExit(error=False):
|
|
|
|
|
curses.endwin()
|
|
|
|
|
if error:
|
|
|
|
|
print error
|
|
|
|
|
print(error)
|
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -176,7 +193,7 @@ def CursesPanel(screen, h, w, y, x, text,
|
|
|
|
|
new_window.attroff(deco_colorpair)
|
|
|
|
|
sub_window = new_window.subwin(h - 2, w - 2, y + 1, x + 1)
|
|
|
|
|
sub_window.insstr(0, 0, text)
|
|
|
|
|
curses.panel.new_panel(new_window)
|
|
|
|
|
panel = curses.panel.new_panel(new_window)
|
|
|
|
|
curses.panel.update_panels()
|
|
|
|
|
screen.refresh()
|
|
|
|
|
if confirm == "password":
|
|
|
|
@@ -245,6 +262,9 @@ def CMDOptions():
|
|
|
|
|
opts.add_option('-c', '--command', action="store", type="string",
|
|
|
|
|
dest="command", metavar="command", default=False,
|
|
|
|
|
help="add command for executing alias")
|
|
|
|
|
opts.add_option('-k', '--keep', action="store", type="string",
|
|
|
|
|
dest="keep", metavar="alias", default=False,
|
|
|
|
|
help="hold connection with specified alias")
|
|
|
|
|
opts.add_option('-e', '--edit', action="store", type="string",
|
|
|
|
|
dest='edit', metavar="alias", default=False,
|
|
|
|
|
help="edit existing connection string")
|
|
|
|
@@ -263,6 +283,8 @@ def CMDOptions():
|
|
|
|
|
CMDPassword(options.password)
|
|
|
|
|
if options.remove:
|
|
|
|
|
CMDRemove(options.remove)
|
|
|
|
|
if options.keep:
|
|
|
|
|
HoldConnection(options.keep)
|
|
|
|
|
if alias:
|
|
|
|
|
CMDConnect(alias, options.command)
|
|
|
|
|
|
|
|
|
@@ -278,6 +300,7 @@ def CursesMain():
|
|
|
|
|
" 'space'/'insert' - select\n",
|
|
|
|
|
" 'r'/'F8' - remove selected alias/aliases\n",
|
|
|
|
|
" 'c'/'F3' - execute specific command with selected alias/aliases\n",
|
|
|
|
|
" 'k'/'F7' - hold connection with selected alias\n",
|
|
|
|
|
" 'enter'/'F9' - connect to selected alias/aliases\n",
|
|
|
|
|
" 'q'/'F10' - quit\n",
|
|
|
|
|
" Run program with '--help' option to view command line help.\n",
|
|
|
|
@@ -315,8 +338,9 @@ def CursesMain():
|
|
|
|
|
else:
|
|
|
|
|
password = ""
|
|
|
|
|
exec_string = ["[", selected_strings[i], "] ", str(i), " ",
|
|
|
|
|
strings[i - 1], " (", conf.get(strings[i - 1], "exec_string"),
|
|
|
|
|
")", password]
|
|
|
|
|
strings[i - 1], " (", (conf.get(strings[i - 1],
|
|
|
|
|
"exec_string") if conf.has_option(strings[i - 1],
|
|
|
|
|
"exec_string") else ""), ")", password]
|
|
|
|
|
if (i == position):
|
|
|
|
|
box.addnstr(i, 2, "".join(exec_string), width - 6, highlight_text)
|
|
|
|
|
else:
|
|
|
|
@@ -369,7 +393,9 @@ def CursesMain():
|
|
|
|
|
while edit_string.rstrip() == "":
|
|
|
|
|
string_textpad = CursesTextpad(screen, 3, width - 8,
|
|
|
|
|
(height // 2) - 1, 4, "Enter new execution string:",
|
|
|
|
|
conf.get(strings[position - 1], "exec_string"),
|
|
|
|
|
(conf.get(strings[position - 1],
|
|
|
|
|
"exec_string") if conf.has_option(strings[position - 1],
|
|
|
|
|
"exec_string") else ""),
|
|
|
|
|
normal_text, highlight_text)
|
|
|
|
|
edit_string = string_textpad.edit(CursesTextpadConfirm)
|
|
|
|
|
SetAliasString(strings[position - 1],
|
|
|
|
@@ -428,6 +454,10 @@ def CursesMain():
|
|
|
|
|
command_string = command_textpad.edit(CursesTextpadConfirm)
|
|
|
|
|
CursesConnect(screen, selected,
|
|
|
|
|
command_string.replace("\n", "").rstrip())
|
|
|
|
|
if (key_pressed == ord('k') or key_pressed == ord('K') or
|
|
|
|
|
key_pressed == (curses.KEY_F7)) and row_num != 0:
|
|
|
|
|
curses.endwin()
|
|
|
|
|
HoldConnection(strings[position - 1])
|
|
|
|
|
if (key_pressed == ord("\n") or key_pressed == (
|
|
|
|
|
curses.KEY_F9)) and row_num != 0:
|
|
|
|
|
selected = []
|
|
|
|
@@ -512,8 +542,9 @@ def CursesMain():
|
|
|
|
|
else:
|
|
|
|
|
password = ""
|
|
|
|
|
exec_string = ["[", selected_strings[i], "] ", str(i), " ",
|
|
|
|
|
strings[i - 1], " (", conf.get(strings[i - 1], "exec_string"),
|
|
|
|
|
")", password]
|
|
|
|
|
strings[i - 1], " (", (conf.get(strings[i - 1],
|
|
|
|
|
"exec_string") if conf.has_option(strings[i - 1],
|
|
|
|
|
"exec_string") else ""), ")", password]
|
|
|
|
|
if (i + (max_row * (page - 1)) == (position + (max_row * (page - 1)))):
|
|
|
|
|
box.addnstr(i - (max_row * (page - 1)), 2, "".join(
|
|
|
|
|
exec_string), width - 6, highlight_text)
|
|
|
|
@@ -529,7 +560,12 @@ def CursesMain():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
conf = ConfigParser.RawConfigParser()
|
|
|
|
|
try:
|
|
|
|
|
input = raw_input # Fix for Python 2.x
|
|
|
|
|
except NameError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
conf = configparser.RawConfigParser()
|
|
|
|
|
if not path.exists(conf_file):
|
|
|
|
|
open(conf_file, 'w')
|
|
|
|
|
conf.read(conf_file)
|
|
|
|
@@ -538,7 +574,7 @@ if __name__ == "__main__":
|
|
|
|
|
CMDOptions()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
exit()
|
|
|
|
|
except ConfigParser.Error:
|
|
|
|
|
except configparser.Error:
|
|
|
|
|
print("Error: can't parse your config file, please check it manually or make new one")
|
|
|
|
|
exit()
|
|
|
|
|
else:
|
|
|
|
@@ -546,7 +582,7 @@ if __name__ == "__main__":
|
|
|
|
|
CursesMain()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
CursesExit()
|
|
|
|
|
except ConfigParser.NoOptionError:
|
|
|
|
|
except configparser.NoOptionError:
|
|
|
|
|
CursesExit("".join(["Error: can't parse your config file, please ",
|
|
|
|
|
"check it manually or make new one"]))
|
|
|
|
|
except curses.error:
|
|
|
|
|