python中paramiko模块的使用
paramiko是python一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接
1、可以远程操作服务器文件
例如:
df:查看磁盘使用情况
mkdir:创建目录
mv/cp/mkdir/rmdir:对文件或目录进行操作
/sbin/service/ xxxservice start/stop/restart:启动、停止、重启某服务
netstat -ntl |grep 8080:查看8080端口的使用情况
或者 nc -zv localhost :查看所有端口的使用情况
find / -name XXX:查找某文件 等等
2、可以实现远程文件的上传,下载(类似于ssh的scp功能)
首先安装paramiko
pip install paramiko
但是安装paramiko需要先安装一个依赖包叫PyCrypto的模块。PyCrypto是python编写的加密工具包,支持的各种加密算法(主要有:MD2 128 bits;MD4 128 bits;MD5 128 bits;RIPEMD 160 bits;SHA1 160 bits;SHA256 256 bits;AES 16, 24, or 32 bytes/16 bytes;ARC2 Variable/8 bytes;Blowfish Variable/8 bytes;CAST Variable/8 bytes;DES 8 bytes/8 bytes ;DES3 (Triple DES) 16 bytes/8 bytes;IDEA 16 bytes/8 bytes ;RC5 Variable/8 bytes等等。)
具体实例代码如下:
1、实现简单的命令操作
#!/usr/bin/env python
# -*- coding:utf-8 -*- import paramiko #创建ssh对象
ssh = paramiko.SSHClient() #允许连接不在know_host中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #连接服务器
ssh.connect(hostname='192.168.1.1',port=22,username='user',password='pwd') #执行命令
stdin, stdout, stderr = ssh.exec_command('cd home;ls -l') #执行结果
#result = stderr.read() #如果有错误则打印
result = stdout.read()
print result
#关闭连接
ssh.close()
2、实现远程上传
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Eric.yue import paramiko,os class Paramiko_put(object):
def __init__(self,local_dir,remote_dir):
self.host = '192.168.1.1'
self.username = 'user'
self.passwd = 'pwd'
self.port = 22
self.local_dir = local_dir
self.remote_dir = remote_dir
self.tt = None def pk_connect(self):
self.tt = paramiko.Transport((self.host, self.port))
self.tt.connect(username = self.username, password = self.passwd)
try:
return paramiko.SFTPClient.from_transport(self.tt)
except Exception as e:
print 'Connect error:',e
exit() def put_file(self):
sftp = self.pk_connect()
files = os.listdir(self.local_dir)
cnt = 0
for file in files:
sftp.put(os.path.join(self.local_dir, file), os.path.join(self.remote_dir, file))
cnt += 1 if cnt == len(files):
print str(cnt) +' files put successful'
else:
print 'put failure' def __del__(self):
self.tt.close() pk = Paramiko_put('/home/mywork/test/day8','/home/mywork/test/day8')
pk.put_file()
3、实现远程下载
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Eric.yue import paramiko,os class Paramiko_get(object):
def __init__(self,remote_dir,local_dir):
self.host = '192.168.1.1'
self.username = 'user'
self.passwd = 'pwd'
self.port = 22
self.local_dir = local_dir
self.remote_dir = remote_dir
self.tt = None def pk_connect(self):
self.tt = paramiko.Transport((self.host, self.port))
self.tt.connect(username = self.username, password = self.passwd)
try:
return paramiko.SFTPClient.from_transport(self.tt)
except Exception as e:
print 'Connect error:',e
exit() def get_file(self):
sftp = self.pk_connect()
files = sftp.listdir(self.remote_dir)
cnt = 0
for file in files:
sftp.get(os.path.join(self.remote_dir, file),os.path.join(self.local_dir, file))
cnt += 1 if cnt == len(files):
print str(cnt) +' files get successful'
else:
print 'get failure' def __del__(self):
self.tt.close() pk = Paramiko_get('/home/inf/mywork/day8','/home/mywork/day8')
pk.get_file()
温馨提示:实现上传下载功能时要保证目录存在,有可操作权限哦!
python中paramiko模块的使用的更多相关文章
- Python之paramiko模块
今天我们来了解一下python的paramiko模块 paramiko是python基于SSH用于远程服务器并执行相应的操作. 我们先在windows下安装paramiko 1.cmd下用pip安装p ...
- Python之paramiko模块和SQL连接API
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...
- Python中optionParser模块的使用方法[转]
本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- 【转】关于python中re模块split方法的使用
注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...
- Python中的模块介绍和使用
在Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个模块,下面就来了解一 ...
- python中导入模块的本质, 无法导入手写模块的解决办法
最近身边一些朋友发生在项目当中编写自己模块,导入的时候无法导入的问题. 下面我来分享一下关于python中导入模块的一些基本知识. 1 导入模块时寻找路径 在每一个运行的python程序当中,都维护了 ...
- Python中time模块详解
Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
随机推荐
- [UCSD白板题] Least Common Multiple
Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...
- P1906联合权值
描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的最短距离. ...
- (01背包 排序+特判)饭卡(hdu 2546)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额 ...
- 'MAMapKit/MAMapKit.h' file not found
1.应该是derived data没清导致的.在Window -> Organizer -> Projects,找到你这个项目,然后点下右边derived data后边的delete按钮. ...
- Windows下为MySQL做定时备份
第一种:新建批处理文件 backup.dat,里面输入以下代码: 代码如下 复制代码 net stop mysqlxcopy "C:/Program Files/MySQL/MySQL S ...
- PHP中实现MySQL嵌套事务的两种解决方案
PHP中实现MySQL嵌套事务的两种解决方案 一.问题起源 在MySQL的官方文档中有明确的说明不支持嵌套事务: Transactions cannot be nested. This is a co ...
- jquery 获取 scrollHeight
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:匿名用户链接:http://www.zhihu.com/question/20985674/answer/16807177来源 ...
- Bullet物理引擎在OpenGL中的应用
Bullet物理引擎在OpenGL中的应用 在开发OpenGL的应用之时, 难免要遇到使用物理来模拟OpenGL中的场景内容. 由于OpenGL仅仅是一个关于图形的开发接口, 因此需要通过第三方库来实 ...
- iBeacon行为分析
研究iBeacon也有段时间了, 总结一下这段时间对于ibeaacon行为的分析. iOS 7.0及以后的版本开始支持iBeacon. 硬件方面, iPhone4S 及以后, ipad 3代及以后, ...
- 记AbpSession扩展实现过程
AbpSession只给了userId和TenantId,这次实际项目中并不够用,网上找了很久也没找到好的实现方法.项目初期没有时间进行研究,最近空了试了一下,大致实现添加额外字段并读取相应值的功能. ...