Skip to content

Commit

Permalink
client-cli: Add passphrase option
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Buesch <m@bues.ch>
  • Loading branch information
mbuesch committed Jan 18, 2019
1 parent c0639fb commit 14472d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
10 changes: 8 additions & 2 deletions awlsim-client
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def usage():
print(" 5: Extremely verbose logging")
print("")
print(" -s|--ssh-tunnel Establish the connection via SSH tunnel.")
print(" -P|--ssh-passphrase XYZ Use XYZ as SSH passphrase.")
print(" Default: Ask user.")
print(" --ssh-user %s SSH user. Default: %s" % (
SSHTunnel.SSH_DEFAULT_USER, SSHTunnel.SSH_DEFAULT_USER))
print(" --ssh-port %s SSH port. Default: %s" % (
Expand All @@ -103,6 +105,7 @@ def main():
opt_timeout = 10.0
opt_loglevel = Logging.LOG_WARNING
opt_sshTunnel = False
opt_sshPassphrase = None
opt_sshUser = SSHTunnel.SSH_DEFAULT_USER
opt_sshPort = SSHTunnel.SSH_PORT
opt_sshLocalPort = None
Expand All @@ -111,9 +114,9 @@ def main():

try:
(opts, args) = getopt.getopt(sys.argv[1:],
"hc:t:L:sr:S",
"hc:t:L:sP:r:S",
[ "help", "connect=", "timeout=", "loglevel=",
"ssh-tunnel", "ssh-user=", "ssh-port=", "ssh-localport=", "ssh-exe=",
"ssh-tunnel", "ssh-passphrase=", "ssh-user=", "ssh-port=", "ssh-localport=", "ssh-exe=",
"runstate=", "stats", ])
except getopt.GetoptError as e:
printError(str(e))
Expand Down Expand Up @@ -146,6 +149,8 @@ def main():
sys.exit(1)
if o in ("-s", "--ssh-tunnel"):
opt_sshTunnel = True
if o in ("-P", "--ssh-passphrase"):
opt_sshPassphrase = v
if o == "--ssh-user":
opt_sshUser = v
if o == "--ssh-port":
Expand Down Expand Up @@ -197,6 +202,7 @@ def main():
sshUser=opt_sshUser,
sshPort=opt_sshPort,
sshExecutable=opt_sshExe,
sshPassphrase=opt_sshPassphrase
)
host, port = tunnel.connect()

Expand Down
26 changes: 20 additions & 6 deletions awlsim/coreclient/sshtunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from awlsim.common.util import *
from awlsim.common.subprocess_wrapper import *
from awlsim.common.monotonic import * #+cimport
from awlsim.common.datatypehelpers import * #+cimport

if not osIsWindows:
import pty
Expand All @@ -53,7 +54,8 @@ def __init__(self, remoteHost, remotePort,
sshUser=SSH_DEFAULT_USER,
localPort=None,
sshExecutable=SSH_DEFAULT_EXECUTABLE,
sshPort=SSH_PORT):
sshPort=SSH_PORT,
sshPassphrase=None):
"""Create an SSH tunnel.
"""
if osIsWindows:
Expand All @@ -66,6 +68,7 @@ def __init__(self, remoteHost, remotePort,
self.localPort = localPort
self.sshExecutable = sshExecutable
self.sshPort = sshPort
self.sshPassphrase = sshPassphrase
self.__sshPid = None
self.__sshProc = None

Expand Down Expand Up @@ -93,7 +96,7 @@ def connect(self, timeout=10.0):
env = AwlSimEnv.clearLang(AwlSimEnv.getEnv())
if osIsWindows and "plink" in self.sshExecutable.lower():
# Run plink.exe (PuTTY)
pw = self.getPassphrase("%s's Password:" % self.remoteHost)
pw = self.__getPassphrase("%s's Password:" % self.remoteHost)
argv = [ self.sshExecutable,
"-ssh",
"-pw", None,
Expand Down Expand Up @@ -232,7 +235,7 @@ def __handshake(self, ptyMasterFd, timeout):
# Second try.
raise AwlSimError("SSH tunnel passphrase "
"was not accepted.")
passphrase = self.getPassphrase(line)
passphrase = self.__getPassphrase(line)
if passphrase is None:
raise AwlSimError("SSH tunnel connection "
"requires a passphrase, but "
Expand Down Expand Up @@ -271,14 +274,25 @@ def sshMessage(self, message, isDebug):
if not isDebug or Logging.getLogLevel() > Logging.LOG_INFO:
printInfo("[SSH]: %s" % message)

def getPassphrase(self, prompt):
"""Get a password from the user.
def __getPassphrase(self, prompt):
"""Get a password.
"""
try:
return getpass.getpass(prompt).encode("UTF-8", "ignore")
if self.sshPassphrase is None:
passphrase = self.getPassphrase(prompt)
else:
passphrase = self.sshPassphrase
if isString(passphrase):
passphrase = passphrase.encode("UTF-8", "ignore")
return passphrase
except UnicodeError:
return b""

def getPassphrase(self, prompt):
"""Get a password from the user.
"""
return getpass.getpass(prompt)

def hostAuth(self, prompt):
"""Get the user answer to the host authentication question.
This function returns a boolean.
Expand Down

0 comments on commit 14472d3

Please sign in to comment.