Using Python and ssh (paramiko)

Good afternoon! How do I write code to connect to a remote host with an embedded paramiko module? You must run the command using a script (see note ls-al) and write the output to the log. In this question, I'm still green, but I would really like to understand how such code works. The script should be executed as follows: ssh [email protected].** > ls-al (on the remote host) > write to log.

1 answers

# -*- coding: UTF-8 -*-
import paramiko

class SSH:
    def __init__(self, **kwargs):
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.kwargs = kwargs

    def __enter__(self):
        '''Как написать код для подключения к удаленному хосту с импрортируемым модулем paramiko'''
        kw = self.kwargs
        self.client.connect(hostname=kw.get('hostname'), username=kw.get('username'),
                            password=kw.get('password'), port=int(kw.get('port', 22)))
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.client.close()

    def exec_cmd(self, cmd):
        ''' Необходимо выполнить команду с помощью скрипта (к прим. ls -al)'''
        stdin, stdout, stderr = self.client.exec_command(cmd)
        data = stdout.read()
        if stderr:
            raise stderr
        return data.decode()

if __name__ == '__main__':
    with SSH(hostname='vs-...', username='lo...', password='l...', port=22) as ssh:  # [email protected].**
        out = ssh.exec_cmd('ls -l')
        print(out)
        print(out, file=open('log.log', 'a'))  # и записью вывода в лог
 3
Author: vadim vaduxa, 2016-11-24 14:36:43