Introduce an utility for ssh commands
This is to introduce a new utility which can be used by other scripts to setup ssh connection to remote hosts and run commands remotely or copy files to remote hosts via scp. Change-Id: Ia363afc1fc932bf44a7ac956a5bc27978bb47868
This commit is contained in:
parent
56bf6b040b
commit
99ea3ef3fa
0
os_xenapi/tests/utils/__init__.py
Normal file
0
os_xenapi/tests/utils/__init__.py
Normal file
94
os_xenapi/tests/utils/test_sshclient.py
Normal file
94
os_xenapi/tests/utils/test_sshclient.py
Normal file
@ -0,0 +1,94 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
import paramiko
|
||||
|
||||
from os_xenapi.tests import base
|
||||
from os_xenapi.utils import sshclient
|
||||
|
||||
|
||||
class fake_channel_file(object):
|
||||
def __init__(self, lines, channel=None):
|
||||
self.buf = iter(lines)
|
||||
self.channel = channel
|
||||
|
||||
def __iter__(self):
|
||||
return self.buf
|
||||
|
||||
|
||||
class SshClientTestCase(base.TestCase):
|
||||
@mock.patch.object(paramiko.SSHClient, 'set_missing_host_key_policy')
|
||||
@mock.patch.object(paramiko.SSHClient, 'connect')
|
||||
def test_init(self, mock_conn, mock_set):
|
||||
sshclient.SSHClient('ip', 'username', 'password')
|
||||
|
||||
mock_conn.assert_called_with(
|
||||
'ip', username='username', password='password', pkey=None,
|
||||
key_filename=None, look_for_keys=False, allow_agent=False)
|
||||
|
||||
@mock.patch.object(paramiko.SSHClient, 'set_missing_host_key_policy')
|
||||
@mock.patch.object(paramiko.SSHClient, 'connect')
|
||||
@mock.patch.object(paramiko.SSHClient, 'exec_command')
|
||||
def test_ssh(self, mock_exec, mock_conn, mock_set):
|
||||
mock_log = mock.Mock()
|
||||
mock_channel = mock.Mock()
|
||||
mock_exec.return_value = (fake_channel_file(['input']),
|
||||
fake_channel_file(['out_line1',
|
||||
'out_line2'],
|
||||
mock_channel),
|
||||
fake_channel_file(['err_line1',
|
||||
'err_line2']))
|
||||
mock_channel.recv_exit_status.return_value = 0
|
||||
|
||||
client = sshclient.SSHClient('ip', 'username', password='password',
|
||||
log=mock_log)
|
||||
out, err = client.ssh('fake_command', output=True)
|
||||
|
||||
mock_log.debug.assert_called()
|
||||
mock_exec.assert_called()
|
||||
mock_log.info.assert_called_with('out_line1\nout_line2')
|
||||
mock_log.error.assert_called_with('err_line1\nerr_line2')
|
||||
mock_channel.recv_exit_status.assert_called_with()
|
||||
self.assertEqual(out, 'out_line1\nout_line2')
|
||||
self.assertEqual(err, 'err_line1\nerr_line2')
|
||||
|
||||
@mock.patch.object(paramiko.SSHClient, 'set_missing_host_key_policy')
|
||||
@mock.patch.object(paramiko.SSHClient, 'connect')
|
||||
@mock.patch.object(paramiko.SSHClient, 'exec_command')
|
||||
def test_ssh_except(self, mock_exec, mock_conn, mock_set):
|
||||
mock_log = mock.Mock()
|
||||
mock_channel = mock.Mock()
|
||||
mock_exec.return_value = (fake_channel_file(['input']),
|
||||
fake_channel_file(['info'], mock_channel),
|
||||
fake_channel_file(['err']))
|
||||
mock_channel.recv_exit_status.return_value = -1
|
||||
|
||||
client = sshclient.SSHClient('ip', 'username', password='password',
|
||||
log=mock_log)
|
||||
self.assertRaises(sshclient.SshExecCmdFailure, client.ssh,
|
||||
'fake_command', output=True)
|
||||
|
||||
@mock.patch.object(paramiko.SSHClient, 'set_missing_host_key_policy')
|
||||
@mock.patch.object(paramiko.SSHClient, 'connect')
|
||||
@mock.patch.object(paramiko.SSHClient, 'open_sftp')
|
||||
def test_scp(self, mock_open, mock_conn, mock_set):
|
||||
mock_log = mock.Mock()
|
||||
mock_sftp = mock.Mock()
|
||||
mock_open.return_value = mock_sftp
|
||||
|
||||
client = sshclient.SSHClient('ip', 'username', password='password',
|
||||
log=mock_log)
|
||||
client.scp('source_file', 'dest_file')
|
||||
|
||||
mock_log.info.assert_called()
|
||||
mock_sftp.put.assert_called_with('source_file', 'dest_file')
|
0
os_xenapi/utils/__init__.py
Normal file
0
os_xenapi/utils/__init__.py
Normal file
72
os_xenapi/utils/sshclient.py
Normal file
72
os_xenapi/utils/sshclient.py
Normal file
@ -0,0 +1,72 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""SSH client.
|
||||
|
||||
This defines a class for SSH client which can be used to scp files to
|
||||
remote hosts or execute commands in remote hosts.
|
||||
"""
|
||||
|
||||
import paramiko
|
||||
|
||||
from os_xenapi.client.exception import OsXenApiException
|
||||
from os_xenapi.client.i18n import _
|
||||
|
||||
|
||||
class SshExecCmdFailure(OsXenApiException):
|
||||
msg_fmt = _("Failed to execute: %(command)s\n"
|
||||
"stdout: %(stdout)s\n"
|
||||
"stderr: %(stderr)s")
|
||||
|
||||
|
||||
class SSHClient(object):
|
||||
def __init__(self, ip, username, password=None, pkey=None,
|
||||
key_filename=None, log=None, look_for_keys=False,
|
||||
allow_agent=False):
|
||||
self.client = paramiko.SSHClient()
|
||||
self.client.set_missing_host_key_policy(paramiko.WarningPolicy())
|
||||
self.client.connect(ip, username=username, password=password,
|
||||
pkey=pkey, key_filename=key_filename,
|
||||
look_for_keys=look_for_keys,
|
||||
allow_agent=allow_agent)
|
||||
self.ip = ip
|
||||
self.log = log
|
||||
|
||||
def __del__(self):
|
||||
self.client.close()
|
||||
|
||||
def ssh(self, command, get_pty=True, output=False):
|
||||
if self.log:
|
||||
self.log.debug("Executing command: [%s]" % command)
|
||||
stdin, stdout, stderr = self.client.exec_command(
|
||||
command, get_pty=get_pty)
|
||||
out = '\n'.join(stdout)
|
||||
err = '\n'.join(stderr)
|
||||
if self.log:
|
||||
if out:
|
||||
self.log.info(out)
|
||||
if err:
|
||||
self.log.error(err)
|
||||
ret = stdout.channel.recv_exit_status()
|
||||
if ret:
|
||||
if self.log:
|
||||
self.log.debug("FAILED executing command: [%s]"
|
||||
"-(ret=%s)" % (command, ret))
|
||||
raise SshExecCmdFailure(command=command,
|
||||
stdout=out, stderr=err)
|
||||
return out, err
|
||||
|
||||
def scp(self, source, dest):
|
||||
if self.log:
|
||||
self.log.info("Copy %s -> %s:%s" % (source, self.ip, dest))
|
||||
sftp = self.client.open_sftp()
|
||||
sftp.put(source, dest)
|
||||
sftp.close()
|
@ -10,4 +10,5 @@ oslo.concurrency>=3.20.0 # Apache-2.0
|
||||
oslo.log>=3.30.0 # Apache-2.0
|
||||
oslo.utils>=3.31.0 # Apache-2.0
|
||||
oslo.i18n>=3.15.3 # Apache-2.0
|
||||
paramiko>=2.0.0 # LGPLv2.1+
|
||||
six>=1.10.0 # MIT
|
||||
|
Loading…
Reference in New Issue
Block a user