python自动化运维六:paramiko
paramiko是基于python实现的SSH2远程安全连接,支持认证以及密钥方式,可以实现远程命令执行,文件传输,中间SSH代理等功能。也就是采用SSH的方式进行远程访问。SSH登陆的方式可以参考之前的一片帖子:http://www.cnblogs.com/zhanghongfeng/p/7749489.html
下面来看一个远程登陆的例子如下:
def paramiko_function_try():
hostname="192.168.0.9"
username='root'
password='root'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname=hostname,username=username,password=password)
stdin,stdout,stderr=ssh.exec_command('ls -al')
print stdout.read()
ssh.close()
执行结果:
total 64
drwx------ 10 root root 4096 Oct 29 10:02 .
drwxr-xr-x 22 root root 4096 Jul 9 16:59 ..
-rw------- 1 root root 4143 Oct 29 10:14 .bash_history
-rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc
drwx------ 5 root root 4096 Aug 31 21:47 .cache
drwx------ 4 root root 4096 Jul 26 10:47 .config
drwx------ 3 root root 4096 Jul 9 15:59 .dbus
drwx------ 2 root root 4096 Jul 9 16:23 .gvfs
drwxr-xr-x 3 root root 4096 Jul 26 11:23 .local
drwxr-xr-x 2 root root 4096 Jul 26 11:33 .pip
-rw-r--r-- 1 root root 140 Feb 20 2014 .profile
drwxr-xr-x 3 root root 4096 Jul 26 11:41 .python-eggs
drwx------ 2 root root 4096 Oct 27 23:11 .ssh
-rw-r--r-- 1 root root 0 Oct 29 10:02 test.txt
-rw------- 1 root root 5436 Oct 27 22:11 .viminfo
从syslogin.log中也可以查看整个建链的过程。
DEB
[20171029-20:41:29.564] thr=1 paramiko.transport: Kex agreed:
ecdh-sha2-nistp256
DEB
[20171029-20:41:29.564] thr=1 paramiko.transport: HostKey agreed:
ecdsa-sha2-nistp256
DEB
[20171029-20:41:29.565] thr=1 paramiko.transport: Cipher agreed:
aes128-ctr
DEB
[20171029-20:41:29.565] thr=1 paramiko.transport: MAC agreed:
hmac-sha2-256
DEB
[20171029-20:41:29.565] thr=1 paramiko.transport: Compression
agreed: none
DEB
[20171029-20:41:29.627] thr=1 paramiko.transport: kex engine
KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEB
[20171029-20:41:29.628] thr=1 paramiko.transport: Switch to new
keys ...
DEB
[20171029-20:41:29.639] thr=2 paramiko.transport: Trying discovered
key 267fb51feeeaf45abbf324467ee574d8 in /root/.ssh/id_rsa
DEB
[20171029-20:41:29.676] thr=1 paramiko.transport: userauth is OK
INF
[20171029-20:41:29.808] thr=1 paramiko.transport: Authentication
(publickey) successful!
DEB
[20171029-20:41:29.841] thr=2 paramiko.transport: [chan 0] Max
packet in: 32768 bytes
DEB
[20171029-20:41:30.281] thr=1 paramiko.transport: [chan 0] Max
packet out: 32768 bytes
DEB
[20171029-20:41:30.281] thr=1 paramiko.transport: Secsh channel 0
opened.
DEB
[20171029-20:41:30.330] thr=1 paramiko.transport: [chan 0] Sesch
channel 0 request ok
DEB
[20171029-20:41:30.356] thr=1 paramiko.transport: [chan 0] EOF
received (0)
DEB
[20171029-20:41:30.357] thr=1 paramiko.transport: EOF in transport
thread
接下来介绍下connect方法中的参数:
hostname:
连接的目标主机地址
port:端口,默认为22
username:校验的用户名
password:
密码用于身份校验或解锁私钥
pkey:私钥方式用于身份验证
key_filename:一个文件名或文件名的列表,用于私钥的身份验证
timeout:
可选的超时时间的TCP连接
allow_agent:
设置为false用于禁用连接到SSH代理
look_for_keys:设置为False用来禁用在~/.ssh中搜索私钥文件
compress:设置为True时打开压缩。
前面只是连接到了远程电脑并执行命令,如果要上传下载文件的话还是要采用SFTP的方法。
示例代码如下,首先要创造一个已连通的SFTP客户端通道。然后采用put上传get下载的方法进行文件的上传和下载。注意的是put的时候本地路径为第一个参数,远端路径为第二个参数。get的时候远端路径为第一个参数,本地路径为第二个参数。
def SFTP_function_try():
t=paramiko.Transport(("192.168.0.9",22))
t.connect(username='root',password='root')
sftp=paramiko.SFTPClient.from_transport(t)
localpath='/home/zhf/zhf/python_prj/auto_manintance/syslogin.log'
remotepath='/home/zhf/syslogin.log'
sftp.put(localpath,remotepath)
localpath='/home/zhf/zhf/python_prj/auto_manintance/log.log'
remotepath='/home/root'
sftp.get(remotepath,localpath)
在前面介绍SSH的时候讲过免密码登陆的方式,现在我们来看下通过paramiko如何免密码,通过证书登陆
示例代码如下:
def paramiko_function_auto():
hostname="192.168.0.9"
username='root'
password='root'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
privatekey=os.path.expanduser('~/.ssh/id_rsa')
key=paramiko.RSAKey.from_private_key_file(privatekey)
ssh.connect(hostname=hostname,username=username,pkey=key)
stdin,stdout,stderr=ssh.exec_command('ls -al')
print stdout.read()
ssh.close()
python自动化运维六:paramiko的更多相关文章
- Python自动化运维的职业发展道路(暂定)
Python职业发展之路 Python自动化运维工程 Python基础 Linux Shell Fabric Ansible Playbook Zabbix Saltstack Puppet Dock ...
- Python自动化运维:技术与最佳实践 PDF高清完整版|网盘下载内附地址提取码|
内容简介: <Python自动化运维:技术与最佳实践>一书在中国运维领域将有“划时代”的重要意义:一方面,这是国内第一本从纵.深和实践角度探讨Python在运维领域应用的著作:一方面本书的 ...
- Python自动化运维 技术与最佳实践PDF高清完整版免费下载|百度云盘|Python基础教程免费电子书
点击获取提取码:7bl4 一.内容简介 <python自动化运维:技术与最佳实践>一书在中国运维领域将有"划时代"的重要意义:一方面,这是国内第一本从纵.深和实践角度探 ...
- python自动化运维之CMDB篇-大米哥
python自动化运维之CMDB篇 视频地址:复制这段内容后打开百度网盘手机App,操作更方便哦 链接:https://pan.baidu.com/s/1Oj_sglTi2P1CMjfMkYKwCQ ...
- Day1 老男孩python自动化运维课程学习笔记
2017年1月7日老男孩python自动化运维课程正式开课 第一天学习内容: 上午 1.python语言的基本介绍 python语言是一门解释型的语言,与1989年的圣诞节期间,吉多·范罗苏姆为了在阿 ...
- python自动化运维学习第一天--day1
学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...
- 【目录】Python自动化运维
目录:Python自动化运维笔记 Python自动化运维 - day2 - 数据类型 Python自动化运维 - day3 - 函数part1 Python自动化运维 - day4 - 函数Part2 ...
- python自动化运维篇
1-1 Python运维-课程简介及基础 1-2 Python运维-自动化运维脚本编写 2-1 Python自动化运维-Ansible教程-Ansible介绍 2-2 Python自动化运维-Ansi ...
- python自动化运维之路~DAY5
python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...
随机推荐
- Codeforces 535B Tavas and SaDDas 水题一枚
题目链接:Tavas and SaDDas Once again Tavas started eating coffee mix without water! Keione told him that ...
- iptables 一些有用的规则
-A INPUT -i lo -j ACCEPT #允许本机内部访问,即回环 -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #允许 ...
- Guru's Guide to SQL Server Architecture and Internals
Guru's Guide to SQL Server Architecture and Internals
- Scut游戏服务器引擎之Unity3d接入
Scut提供Unity3d Sdk包,方便开发人员快速与Scut游戏服务器对接: 先看Unity3d示例如下: 启动Unity3d项目 打开Scutc.svn\SDK\Unity3d\Assets目录 ...
- ylbtech-czgfh(规范化)-数据库设计
ylbtech-DatabaseDesgin:ylbtech-czgfh(规范化)-数据库设计 DatabaseName:czgfh(财政规范化) Model:账户模块.系统时间设计模块.上报自评和审 ...
- winsock 收发广播包 【转】
winsock 收发广播包 ☛广播包的概念 广播包通常为了如下两个原因使用:1 一个应用程序希望在本地网络中找到一个资源,而应用程序对于该资源的地址又没有任何先验的知识. 2 一些重要的功能,例如路由 ...
- API接口管理工具postman等
国外 postman Swagger:国外比较流行的一款管理工具,英文配置,需要一定的英文基础和服务器搭建基础,学习成本较高. 国内 Apizza: 风格类似postman,熟悉postman的会比较 ...
- 软件业的发展方向:云、Web以及App
随着行业互联网的发展,未来的软件发展方向是云技术.Web软件以及基于移动设备的Apps. 桌面软件主要负责大型的计算.渲染和消耗非常大CPU和内存的图形软件,以及基于这些软件的二次开发软件如Revit ...
- 微信小程序 - 考试倒计时
源码如下(csdn提供了思路 , 多谢 ,第二个小程序项目,有惊无险(_._): Page({ /** * 页面的初始数据 */ data: { timer: '', //定时器名字 countDow ...
- google 搜索结果在新标签页打开
google->setting->search setting->Where results open->勾选