python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python操作远程服务器

Python基于paramiko库操作远程服务器的实现

作者:懒大王爱吃狼

本文主要介绍了使用Python的Paramiko库来操作远程服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Paramiko 是一个用于在 Python 中实现 SSHv2 协议的模块,可以用于连接到远程服务器并执行各种操作,如执行命令、上传和下载文件等。以下是一个基于 Paramiko 库操作远程服务器的示例。

首先,确保你已经安装了 Paramiko 库。如果还没有安装,可以使用以下命令进行安装:

pip install paramiko

以下是一个示例脚本,展示如何使用 Paramiko 连接到远程服务器并执行一些基本操作:

import paramiko

def ssh_connect(hostname, port, username, password):
    # 创建一个SSH客户端对象
    ssh_client = paramiko.SSHClient()
    
    # 自动添加远程服务器的主机名和密钥到本地known_hosts文件中
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    try:
        # 连接到远程服务器
        ssh_client.connect(hostname=hostname, port=port, username=username, password=password)
        print(f"Successfully connected to {hostname}")
        return ssh_client
    except Exception as e:
        print(f"Failed to connect to {hostname}: {e}")
        return None

def execute_command(ssh_client, command):
    try:
        # 执行命令
        stdin, stdout, stderr = ssh_client.exec_command(command)
        
        # 读取命令的标准输出和标准错误
        output = stdout.read().decode()
        error = stderr.read().decode()
        
        return output, error
    except Exception as e:
        print(f"Failed to execute command: {e}")
        return None, str(e)

def sftp_upload_file(ssh_client, local_file_path, remote_file_path):
    try:
        # 使用SFTP上传文件
        sftp_client = ssh_client.open_sftp()
        sftp_client.put(local_file_path, remote_file_path)
        sftp_client.close()
        print(f"Successfully uploaded {local_file_path} to {remote_file_path}")
    except Exception as e:
        print(f"Failed to upload file: {e}")

def sftp_download_file(ssh_client, remote_file_path, local_file_path):
    try:
        # 使用SFTP下载文件
        sftp_client = ssh_client.open_sftp()
        sftp_client.get(remote_file_path, local_file_path)
        sftp_client.close()
        print(f"Successfully downloaded {remote_file_path} to {local_file_path}")
    except Exception as e:
        print(f"Failed to download file: {e}")

def main():
    hostname = 'your_remote_server_ip'
    port = 22
    username = 'your_username'
    password = 'your_password'
    
    # 连接到远程服务器
    ssh_client = ssh_connect(hostname, port, username, password)
    
    if ssh_client:
        # 执行命令
        command = 'ls -l'
        output, error = execute_command(ssh_client, command)
        print(f"Command Output:\n{output}")
        if error:
            print(f"Command Error:\n{error}")
        
        # 上传文件
        local_file_path = '/path/to/local/file.txt'
        remote_file_path = '/path/to/remote/file.txt'
        sftp_upload_file(ssh_client, local_file_path, remote_file_path)
        
        # 下载文件
        remote_file_to_download = '/path/to/remote/another_file.txt'
        local_file_to_save = '/path/to/local/another_file.txt'
        sftp_download_file(ssh_client, remote_file_to_download, local_file_to_save)
        
        # 关闭SSH连接
        ssh_client.close()

if __name__ == "__main__":
    main()

说明:

注意事项:

到此这篇关于Python基于paramiko库操作远程服务器的实现的文章就介绍到这了,更多相关Python操作远程服务器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文