paramiko之ssh用法

import threading
import paramiko
import subprocess

def ssh_command(ip,user,passwd,command):
    client=paramiko.SSHClient()
    #client.load_host_keys("filename")使用密钥验证
    #设置自动添加和保存ssh服务器的ssh密钥
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip,username=user,password=passwd)
    ssh_session=client.get_transport().open_session()
    if ssh_session.active:
        #调用ssh_comand函数运行command
        ssh_session.exec_command(command)
        a=ssh_session.recv(1024)
    return a
a=ssh_command("192.168.6.128",'root','123456','ls -lh')
print(a.decode("gbk"))

 

基于用户名密码

import paramiko
  
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', password='123')
  
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
  
# 关闭连接
ssh.close()
View Code

封装transport

import paramiko

transport = paramiko.Transport(('hostname', 22))
transport.connect(username='wupeiqi', password='123')

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command('df')
print stdout.read()

transport.close()
View Code

 

基于公钥密钥

import paramiko
 
private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
 
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key)
 
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
 
# 关闭连接
ssh.close()
View Code

封装

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')

transport = paramiko.Transport(('hostname', 22))
transport.connect(username='wupeiqi', pkey=private_key)

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command('df')

transport.close()
View Code

 

基于用户名密码上传下载

import paramiko
 
transport = paramiko.Transport(('hostname',22))
transport.connect(username='wupeiqi',password='123')
 
sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/tmp/test.py')
# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path')
 
transport.close()
View Code

基于公钥密钥

import paramiko
 
private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
 
transport = paramiko.Transport(('hostname', 22))
transport.connect(username='wupeiqi', pkey=private_key )
 
sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/tmp/test.py')
# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path')
 
transport.close()
View Code

 

 

paramiko封装

 

import paramiko

class SshHelper(object):

    def __init__(self,host,port,username,pwd):
        self.host = host
        self.port = port
        self.username = username
        self.pwd = pwd
        self.transport = None

    def connect(self):
        transport = paramiko.Transport((self.host, self.port,))
        transport.connect(username=self.username, password=self.pwd)
        self.transport = transport

    def upload(self,local,target):
        sftp = paramiko.SFTPClient.from_transport(self.transport)
        # 将location.py 上传至服务器 /tmp/test.py
        sftp.put(local, target)
        # 将remove_path 下载到本地 local_path
        # sftp.get('remove_path', 'local_path')

    def cmd(self,shell):
        ssh = paramiko.SSHClient()
        ssh._transport = self.transport
        stdin, stdout, stderr = ssh.exec_command(shell)
        stdout.read()

    def close(self):
        self.transport.close()

if __name__ == '__main__':

    obj = SshHelper('...')
    obj.connect()
    obj.close()

 

转载于:https://www.cnblogs.com/arthas-zht/p/6569131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用paramiko库连接到SSH服务器,您可以按照以下步骤进行操作: 1. 首先,确保已安装paramiko库。可以使用以下命令进行安装: ``` pip install paramiko ``` 2. 导入paramiko库: ```python import paramiko ``` 3. 创建SSHClient对象并建立连接: ```python client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname='your_hostname', username='your_username', password='your_password') ``` 在上面的代码中,将`your_hostname`替换为SSH服务器的主机名或IP地址,`your_username`替换为您的用户名,`your_password`替换为您的密码。如果您使用的是SSH密钥对身份验证,请参考paramiko文档以使用密钥对进行连接。 4. 执行命令或传输文件: - 执行命令: ```python stdin, stdout, stderr = client.exec_command('your_command') ``` 将`your_command`替换为您要执行的命令。然后,您可以使用`stdout.readlines()`来读取命令的输出。 - 传输文件: ```python sftp = client.open_sftp() sftp.put('local_file_path', 'remote_file_path') ``` 将`local_file_path`替换为本地文件的路径,`remote_file_path`替换为远程服务器上目标文件的路径。您还可以使用`sftp.get()`方法从远程服务器下载文件。 5. 关闭连接: ```python client.close() ``` 这样,您就完成了使用paramiko库连接SSH服务器和执行操作的基本步骤。请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值