32
29
class Connection(object):
34
def __init__(self, host, username, password, port=22, keyfile=None):
31
def __init__(self, host, username, password, port=22):
36
33
self.username = username
37
34
self.password = password
39
self.keyfile = keyfile
42
38
def ssh_connect(connection):
160
155
final_path = path_one + '/' + path_two
161
156
return final_path
164
@contextlib.contextmanager
165
def vios_to_vios_auth(source, dest, conn_info):
166
"""Context allowing for SSH between VIOS partitions
168
This context will build an SSH key on the source host, put the key
169
into the authorized_keys on the destination host, and make the
170
private key file name available within the context.
171
The key files and key inserted into authorized_keys will be
172
removed when the context exits.
174
:param source: source IP or DNS name
175
:param dest: destination IP or DNS name
176
:param conn_info: dictionary object with SSH connection
177
information for both hosts
179
KEY_BASE_NAME = "os-%s" % uuid.uuid4().hex
180
keypair_uuid = uuid.uuid4()
181
src_conn_obj = ssh_connect(conn_info)
183
dest_conn_info = Connection(dest, conn_info.username,
185
dest_conn_obj = ssh_connect(dest_conn_info)
187
def run_command(conn_obj, cmd):
188
stdout, stderr = utils.ssh_execute(conn_obj, cmd)
189
return stdout.strip().splitlines()
191
def build_keypair_on_source():
192
mkkey = ('ssh-keygen -f %s -N "" -C %s' %
193
(KEY_BASE_NAME, keypair_uuid.hex))
194
ssh_command_as_root(src_conn_obj, mkkey)
196
chown_key = ('chown %s %s*' % (conn_info.username, KEY_BASE_NAME))
197
ssh_command_as_root(src_conn_obj, chown_key)
199
cat_key = ('cat %s.pub' % KEY_BASE_NAME)
200
pubkey = run_command(src_conn_obj, cat_key)
204
def cleanup_key_on_source():
205
rmkey = 'rm %s*' % KEY_BASE_NAME
206
run_command(src_conn_obj, rmkey)
208
def insert_into_authorized_keys(public_key):
209
echo_key = 'echo "%s" >> .ssh/authorized_keys' % public_key
210
ssh_command_as_root(dest_conn_obj, echo_key)
212
def remove_from_authorized_keys():
213
rmkey = ('sed /%s/d .ssh/authorized_keys > .ssh/authorized_keys' %
215
ssh_command_as_root(dest_conn_obj, rmkey)
217
public_key = build_keypair_on_source()
218
insert_into_authorized_keys(public_key)
223
remove_from_authorized_keys()
224
cleanup_key_on_source()