Paramiko模块使用
实现目的:192.168.0.61通过Paramiko模块登录192.168.0.63
一、下载安装
由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto
pip install pycrypto
pip install paramiko
二、模块使用
1、SSHClient 用于连接远程服务器并执行基本命令
(1)基于用户名密码连接:
#!/usr/bin/python
#-*- codinig: UTF-8 -*-
import paramiko
transport = paramiko.Transport(('192.168.0.63', 22))
transport.connect(username='root', password='123')
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('sed -i s/hello/hi/g /home/b.sh')
print (stdout.read())
transport.close()
(2)基于公钥密钥连接:
前提已经生产公钥,而且公钥拷贝到了192.168.0.63这台上
#!/usr/bin/python
# -*- coding:UTF-8 -*-
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
transport = paramiko.Transport(('192.168.0.63', 22))
transport.connect(username='root', pkey=private_key)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('ifconfig')
print (stdout.read())
transport.close()
2、SFTPClient用于连接远程服务器并执行上传下载
(1)基于用户名密码上传下载:
import paramiko
transport = paramiko.Transport(('192.168.0.63',22))
transport.connect(username='root',password='123')
sftp = paramiko.SFTPClient.from_transport(transport)
# 将/home/b.sh 上传至服务器 /home/b.sh
sftp.put('/home/b.sh', '/home/b.sh')
# 将remove_path 下载到本地 local_path
sftp.get('/home/b.sh', '/home/b.sh')
transport.close()
(2)基于公钥密钥上传下载:
# -*- coding:UTF-8 -*-
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
transport = paramiko.Transport(('192.168.0.63', 22))
transport.connect(username='root', pkey=private_key )
sftp = paramiko.SFTPClient.from_transport(transport)
# 将/home/b.sh 上传至服务器 /home/b.sh
sftp.put('/home/b.sh', '/home/b.sh')
# 将remove_path 下载到本地 local_path
sftp.get('/home/b.sh', '/home/b.sh')
transport.close()
3、实例:通过transport实现远程执行命令和上传文件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import paramiko
class SSHConnection(object):
def __init__(self, host='192.168.0.63', port=22, username='root',pwd='123'):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None
def run(self):
self.connect()
pass
self.close()
def connect(self):
transport = paramiko.Transport((self.host,self.port))
transport.connect(username=self.username,password=self.pwd)
self.__transport = transport
def close(self):
self.__transport.close()
def cmd(self, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
return result
def upload(self,local_path, target_path):
# 连接,上传
sftp = paramiko.SFTPClient.from_transport(self.__transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(local_path, local_path)
ssh = SSHConnection()
ssh.connect()
r1 = ssh.cmd('df')
print(r1.decode())
#ssh.upload('test.py', "/root/test.py")
ssh.upload("/home/b.sh", "/home/b.sh")
ssh.close()
参照:
https://www.cnblogs.com/xiaozhiqi/p/5814564.html
https://www.2cto.com/kf/201805/746279.html
https://blog.csdn.net/ywq935/article/details/78816860
https://blog.csdn.net/opera95/article/details/72824006