• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Create a SshSettings from a dictionary from an ACTS config
15
16Args:
17    config dict instance from an ACTS config
18
19Returns:
20    An instance of SshSettings or None
21"""
22
23
24def from_config(config):
25    if config is None:
26        return None  # Having no settings is not an error
27
28    user = config.get('user', None)
29    host = config.get('host', None)
30    port = config.get('port', 22)
31    identity_file = config.get('identity_file', None)
32    ssh_config = config.get('ssh_config', None)
33    if user is None or host is None:
34        raise ValueError('Malformed SSH config did not include user and '
35                         'host keys: %s' % config)
36
37    return SshSettings(host, user, port=port, identity_file=identity_file,
38                       ssh_config=ssh_config)
39
40
41class SshSettings(object):
42    """Contains settings for ssh.
43
44    Container for ssh connection settings.
45
46    Attributes:
47        username: The name of the user to log in as.
48        hostname: The name of the host to connect to.
49        executable: The ssh executable to use.
50        port: The port to connect through (usually 22).
51        host_file: The known host file to use.
52        connect_timeout: How long to wait on a connection before giving a
53                         timeout.
54        alive_interval: How long between ssh heartbeat signals to keep the
55                        connection alive.
56    """
57
58    def __init__(self,
59                 hostname,
60                 username,
61                 port=22,
62                 host_file='/dev/null',
63                 connect_timeout=30,
64                 alive_interval=300,
65                 executable='/usr/bin/ssh',
66                 identity_file=None,
67                 ssh_config=None):
68        self.username = username
69        self.hostname = hostname
70        self.executable = executable
71        self.port = port
72        self.host_file = host_file
73        self.connect_timeout = connect_timeout
74        self.alive_interval = alive_interval
75        self.identity_file = identity_file
76        self.ssh_config = ssh_config
77
78    def construct_ssh_options(self):
79        """Construct the ssh options.
80
81        Constructs a dictionary of option that should be used with the ssh
82        command.
83
84        Returns:
85            A dictionary of option name to value.
86        """
87        current_options = {}
88        current_options['StrictHostKeyChecking'] = False
89        current_options['UserKnownHostsFile'] = self.host_file
90        current_options['ConnectTimeout'] = self.connect_timeout
91        current_options['ServerAliveInterval'] = self.alive_interval
92        return current_options
93
94    def construct_ssh_flags(self):
95        """Construct the ssh flags.
96
97        Constructs what flags should be used in the ssh connection.
98
99        Returns:
100            A dictonary of flag name to value. If value is none then it is
101            treated as a binary flag.
102        """
103        current_flags = {}
104        current_flags['-a'] = None
105        current_flags['-x'] = None
106        current_flags['-p'] = self.port
107        if self.identity_file:
108            current_flags['-i'] = self.identity_file
109        if self.ssh_config:
110            current_flags['-F'] = self.ssh_config
111        return current_flags
112