python免密遠程執行shell


使用paramiko庫:https://github.com/paramiko/paramiko

簡單封裝SSH類

import paramiko


class SSH:

    def __init__(self, host, port, user, ssh_key_path, timeout=1800):
        self.host = host
        self.port = port
        self.user = user
        self.ssh_key_path = ssh_key_path
        self.timeout = timeout
        self.connect()

    def connect(self):
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        pkey = paramiko.RSAKey.from_private_key_file(self.ssh_key_path)
        client.connect(hostname=self.host, username=self.user, port=self.port, pkey=pkey, timeout=self.timeout)
        self.client = client

    def exec(self, shell, timeout=1800):
        stdin, stdout, stderr = self.client.exec_command(command=shell, bufsize=1, timeout=timeout)
        while True:
            line = stdout.readline()
            if not line:
                break
            print(line)
        print(stderr.read())
        code = stdout.channel.recv_exit_status()
        return code

    def close(self):
        self.client.close()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM