Permalink
Browse files

support for ssh password

the networkservice resource has a new password property - if it is set the
sshdriver will login with sshpass (needs to be installed) instead of the key
file

Signed-off-by: Tobi Gschwendtner <tg@bloks.de>
[r.czerwinski@pengutronix.de: fix commit message, documentation and rebase]
Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
  • Loading branch information...
tgbloks authored and Emantor committed Dec 10, 2017
1 parent 7169c96 commit 8770d78e750fc5c0653a738eed6f0b8564e1c07d
Showing with 21 additions and 6 deletions.
  1. +4 −0 doc/configuration.rst
  2. +16 −6 labgrid/driver/sshdriver.py
  3. +1 −0 labgrid/resource/networkservice.py
View
@@ -160,9 +160,12 @@ A NetworkService describes a remote SSH connection.
The example describes a remote SSH connection to the computer `example.computer`
with the username `root`.
Set the optional password password property to make SSH login with a password
instead of the key file (needs sshpass to be installed)
- address (str): hostname of the remote system
- username (str): username used by SSH
- password (str): password used by SSH
Used by:
- `SSHDriver`_
@@ -531,6 +534,7 @@ Implements:
Arguments:
- keyfile (str): filename of private key to login into the remote system
(only used if password is not set)
InfoDriver
~~~~~~~~~~
@@ -30,9 +30,11 @@ def __attrs_post_init__(self):
self.logger = logging.getLogger("{}({})".format(self, self.target))
def on_activate(self):
self.ssh_prefix = "-i {}".format(os.path.abspath(self.keyfile)
self.ssh_prefix = "-o LogLevel=ERROR"
self.ssh_prefix += " -i {}".format(os.path.abspath(self.keyfile)
) if self.keyfile else ""
self.ssh_prefix += " -o LogLevel=ERROR"
self.ssh_prefix += " -o PasswordAuthentication=no" if (
not self.networkservice.password) else ""
self.control = self._check_master()
self.ssh_prefix += " -F /dev/null"
self.ssh_prefix += " -o ControlPath={}".format(
@@ -48,11 +50,19 @@ def _start_own_master(self):
control = os.path.join(
self.tmpdir, 'control-{}'.format(self.networkservice.address)
)
args = "ssh -f {} -x -o ConnectTimeout=30 -o ControlPersist=300 -o PasswordAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -MN -S {} {}@{}".format(
self.ssh_prefix, control, self.networkservice.username,
self.networkservice.address
# use sshpass if we have a password
sshpass = "sshpass -e " if self.networkservice.password else ""
args = ("{}ssh -f {} -x -o ConnectTimeout=30 -o ControlPersist=300 "
"-o UserKnownHostsFile=/dev/null "
"-o StrictHostKeyChecking=no -MN -S {} {}@{}").format(
sshpass, self.ssh_prefix, control,
self.networkservice.username, self.networkservice.address
).split(" ")
self.process = subprocess.Popen(args, )
env = os.environ.copy()
if self.networkservice.password:
env['SSHPASS'] = self.networkservice.password
self.process = subprocess.Popen(args, env=env)
try:
if self.process.wait(timeout=30) is not 0:
@@ -9,3 +9,4 @@
class NetworkService(Resource):
address = attr.ib(validator=attr.validators.instance_of(str))
username = attr.ib(validator=attr.validators.instance_of(str))
password = attr.ib(default='', validator=attr.validators.instance_of(str))

0 comments on commit 8770d78

Please sign in to comment.