Python远程执行命令和拷贝

Python远程执行命令和拷贝

使用paramiko库和scp库结合

import paramiko
from scp import SCPClient


class LinuxSSHSCP(object):

    def __init__(self, ip, username, password, port=22):
        self.ip = ip
        self.username = username
        self.password = password
        self.port = port
        self.ssh_client = None
        self.scp_client = None
        self.sftp_client = None

    def ssh_connect(self):
        print("ssh connect")
        if self.ssh_client is None:
            self.ssh_client = paramiko.SSHClient()
            # set_missing_host_key_policy(paramiko.AutoAddPolicy()) 允许连接不在know_hosts文件中的主机
            self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.ssh_client.connect(self.ip, self.port, self.username, self.password, timeout=10)

    def ssh_disconnect(self):
        print("ssh disconnect")
        self.ssh_client.close()
        self.ssh_client = None

    def ssh_exec_command(self, command):
        print("exec command on remote")
        std_in, std_out, std_err = self.ssh_client.exec_command(command)
        result = std_out.read().decode()
        return result

    def scp_connect(self):
        print("scp connect")
        if self.ssh_client is None:
            print("ssh not connect, start connect ssh")
            self.ssh_connect()

        if self.scp_client is None:
            self.scp_client = SCPClient(self.ssh_client.get_transport(), socket_timeout=15.0)

    def scp_disconnect(self):
        print("scp disconnect")
        self.scp_client.close()
        self.scp_client = None

    def scp_copy_from_remote(self, local_path, remote_path):
        print("start copy remote to local")
        self.scp_client.get(remote_path, local_path, True)

    def scp_copy_to_remote(self, local_path, remote_path):
        print("start copy local to remote")
        self.scp_client.put(local_path, remote_path, True)

    def sftp_connect(self):
        print("sftp connect")
        if self.ssh_client is None:
            self.ssh_connect()
        self.sftp_client = self.ssh_client.open_sftp()
    
    def sftp_copy_from_remote(self, local_path, remote_path):
        print("sftp copy from remote")
        self.sftp_client.get(remote_path, local_path)

    def sftp_copy_to_remote(self, local_path, remote_path):
        print("sftp copy to remote")
        self.sftp_client.put(local_path, remote_path)

    def sftp_disconnect(self):
        print("sftp connect")
        self.sftp_client.close()
        self.sftp_client = None


if __name__ == "__main__":
    # 使用scp远程拷贝 拷贝整个目录只要提供路径就可以
    # s = LinuxSSHSCP("ip", "tester", "password", 22)
    # s.ssh_connect()
    # s.scp_connect()
    # print(s.ssh_exec_command("ls /home/tester/ |grep test"))
    # print(s.ssh_exec_command("touch /home/tester/scp_test.text"))
    # s.scp_copy_from_remote("/home/test/demo/ssh_demo/", "/home/tester/scp_test.text")
    # print(s.ssh_exec_command("ls /home/tester/ |grep test"))
    # s.scp_copy_to_remote("/home/test/demo/ssh_demo/tester_password", "/home/tester/")
    # print(s.ssh_exec_command("ls /home/tester/ |grep test"))
    # s.scp_disconnect()
    # s.ssh_disconnect()
    
    # 使用sftp远程拷贝 拷贝整个目录需要先获取目录下所有的文件名字,然后依次拷贝
    s = LinuxSSHSCP("ip", "tester", "password", 22)
    s.ssh_connect()
    s.sftp_connect()
    print(s.ssh_exec_command("ls /home/tester/ |grep test"))
    print(s.ssh_exec_command("touch /home/tester/scp_test.txt"))
    s.sftp_copy_from_remote("/home/test/demo/ssh_demo/scp_test.txt", "/home/tester/scp_test.txt")
    print(s.ssh_exec_command("ls /home/tester/ |grep test"))
    s.sftp_copy_to_remote("/home/test/demo/ssh_demo/tester_password", "/home/tester/tester_password")
    print(s.ssh_exec_command("ls /home/tester/ |grep test"))
    s.sftp_disconnect()
    s.ssh_disconnect()


发布了19 篇原创文章 · 获赞 3 · 访问量 4946

猜你喜欢

转载自blog.csdn.net/xiaojian0907/article/details/103548210