1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from scp import SCPClient
14 import RelengLogger as rl
32 self.password = password
33 self.use_system_keys = use_system_keys
34 self.private_key = private_key
35 self.use_proxy = use_proxy
36 self.proxy_host = proxy_host
37 self.proxy_user = proxy_user
38 self.proxy_password = proxy_password
39 self.timeout = timeout
40 paramiko.util.log_to_file("paramiko.log")
41 self.logger = rl.Logger("SSHUtils").getLogger()
44 client = paramiko.SSHClient()
45 if self.use_system_keys:
46 client.load_system_host_keys()
47 elif self.private_key:
48 client.load_host_keys(self.private_key)
50 client.load_host_keys('/dev/null')
52 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
57 proxy_command = 'ssh -o UserKnownHostsFile=/dev/null '
58 '-o StrictHostKeyChecking=no %s@%s -W %s:%s' % (self.proxy_user,
61 proxy = paramiko.ProxyCommand(proxy_command)
62 self.logger.debug("Proxy command: %s" % proxy_command)
66 "Trying to stablish ssh connection to %s..." % self.host)
67 client.connect(self.host,
69 password=self.password,
72 pkey=self.private_key,
74 self.logger.debug("Successfully connected to %s!" % self.host)
83 def scp_put(self, local_path, remote_path):
84 client = self.connect()
86 scp = SCPClient(client.get_transport())
88 scp.put(local_path, remote_path)
96 self.logger.error("Cannot stablish ssh connection.")
98 def scp_get(self, local_path, remote_path):
99 client = self.connect()
101 scp = SCPClient(client.get_transport())
103 scp.get(remote_path, local_path)
111 self.logger.error("Cannot stablish ssh connection.")
114 def run_remote_cmd(self, command):
115 client = self.connect()
118 stdin, stdout, stderr = client.exec_command(command)
120 for line in stdout.readlines():
122 err = stderr.readlines()
129 self.logger.error("Cannot stablish ssh connection.")