美文网首页
Paramiko 模块

Paramiko 模块

作者: mini鱼 | 来源:发表于2023-05-30 20:26 被阅读0次

什么是 Paramiko?
Paramiko 是一个 Python 实现的 SSH 协议库,提供了 SSH 客户端和 SSH 服务器的 API。它允许你通过 SSH 协议远程控制服务器,进行数据传输或在 Shell 中执行命令等操作。
如何安装 Paramiko?
Paramiko 可以使用 pip 安装,命令如下:
pip install paramiko
如何使用 Paramiko 连接 SSH 服务器?
使用 Paramiko 连接 SSH 服务器可以通过如下代码实现:

import paramiko
# SSH credentials
ssh_host = 'your_ssh_host'
ssh_user = 'your_ssh_username'
ssh_password = 'your_ssh_password'
# Establish SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ssh_host, username=ssh_user, password=ssh_password)
# Execute command
command = 'your_ssh_command'
stdin, stdout, stderr = ssh.exec_command(command)
# Print output
print(stdout.read().decode())
# Close SSH connection
ssh.close()

如何使用 Paramiko 上传和下载文件?
可以使用 Paramiko 的 SFTP API 上传和下载文件:

import paramiko
# SFTP credentials
sftp_host = 'your_sftp_host'
sftp_user = 'your_sftp_username'
sftp_password = 'your_sftp_password'
# Establish SFTP connection
transport = paramiko.Transport((sftp_host, 22))
transport.connect(username=sftp_user, password=sftp_password)
sftp = transport.open_sftp()
# Download a remote file
remote_file_path = '/path/to/remote/file'
local_file_path = '/path/to/local/file'
sftp.get(remote_file_path, local_file_path)
# Upload a local file
local_file_path = '/path/to/local/file'
remote_file_path = '/path/to/remote/file'
sftp.put(local_file_path, remote_file_path)
# Close SFTP connection
sftp.close()
transport.close()

如何使用 Paramiko 执行 sudo 命令?
可以使用 Paramiko 的 invoke_shell() 方法来模拟一个终端会话,然后执行 sudo 命令:

import paramiko
# SSH credentials
ssh_host = 'your_ssh_host'
ssh_user = 'your_ssh_username'
ssh_password = 'your_ssh_password'
# Establish SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ssh_host, username=ssh_user, password=ssh_password)
# Start a shell session
shell = ssh.invoke_shell()
shell.send('sudo your_command\n')
# Wait for the password prompt
while not shell.recv_ready():
    pass
shell.send('your_password\n')
# Execute command
output = shell.recv(1024)
# Print output
print(output.decode())
 # Close SSH connection
ssh.close()

** 增加异常处理 **

import paramiko
import json


# SSH连接信息
hostname = "1.1.1.1"
username = "root"
password = "123456"
# 命令
command = "kubectl get cm -n test xxx-configmap -o json"
# 创建SSH客户端
client = paramiko.SSHClient()
# 自动添加主机密钥
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())


# 连接SSH服务器
try:
    client.connect(hostname, username=username, password=password)
except paramiko.AuthenticationException:
    print("Authentication failed, please verify your password.")
except paramiko.SSHException as sshException:
    print("Unable to establish SSH connection: %s" % sshException)
except paramiko.SSHException as e:
    print(e)


# 执行命令
try:
    stdin, stdout, stderr = client.exec_command(command, timeout=10)
    output = stdout.read().decode()
    print(output)
    result = json.loads(output)
    print(result["kind"])
except paramiko.SSHException as sshException:
    print("Unable to execute command: %s" % sshException)

# 关闭连接
client.close()

Paramiko 的 SSH 隧道功能

import paramiko
# SSH credentials
ssh_host = 'your_ssh_host'
ssh_user = 'your_ssh_username'
ssh_password = 'your_ssh_password'
# Tunnel credentials
tunnel_host = 'your_tunnel_host'
tunnel_port = your_tunnel_port
tunnel_user = 'your_tunnel_username'
tunnel_password = 'your_tunnel_password'
# Establish SSH tunnel
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(tunnel_host, username=tunnel_user, password=tunnel_password)
transport = ssh.get_transport()
local_port = 12345
remote_port = 54321
transport.request_port_forward('', local_port, remote_port)
# Establish SSH connection through the tunnel
ssh_tunnel = paramiko.SSHClient()
ssh_tunnel.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_tunnel.connect(ssh_host, username=ssh_user, password=ssh_password, sock=transport.open_channel('direct-tcpip', ('127.0.0.1', remote_port), ('127.0.0.1', local_port)))
# Execute command
command = 'your_ssh_command'
stdin, stdout, stderr = ssh_tunnel.exec_command(command)
# Print output
print(stdout.read().decode())
# Close SSH connection
ssh_tunnel.close()
transport.close()
ssh.close()

以上就是关于 Python Paramiko 模块的文章总结。

相关文章

  • python | 执行shell脚本

    python执行shell脚本1.远程:paramiko2.本地:subprocess 一、paramiko模块 ...

  • paramiko模块

    该模块基于SSH服务用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 SFT...

  • python如何上传或下载ftp文件

    需要借助Paramiko,Paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令、...

  • python中paramiko模块的使用

    paramiko paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一...

  • Mac环境 Python安装Paramiko模块

    最近在研究Python的时候需要用到paramiko模块,希望这篇博客能让大家少走弯路。 Paramiko简介 P...

  • python 学习笔记(paramiko模块)

    paramiko模块 paramiko是一个基于SSH用于连接远程服务器并执行相关操作(SSHClient和SFT...

  • python模块: paramiko

    paramiko模块提供了ssh及sft进行远程登录服务器执行命令和上传下载文件的功能。这是一个第三方的软件包,使...

  • Python paramiko 模块

    本文参考 http://www.361way.com/python-paramiko-ssh/3984.html ...

  • python paramiko模块

    paramiko模块 1. 介绍: 2. 下载安装 3. 使用 SSHClient 用于连接远程服务器并执行基本命...

  • python paramiko模块

    1.介绍: ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SS...

网友评论

      本文标题:Paramiko 模块

      本文链接:https://www.haomeiwen.com/subject/bpeeedtx.html