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的更多相关文章

  1. Python自动化运维的职业发展道路(暂定)

    Python职业发展之路 Python自动化运维工程 Python基础 Linux Shell Fabric Ansible Playbook Zabbix Saltstack Puppet Dock ...

  2. Python自动化运维:技术与最佳实践 PDF高清完整版|网盘下载内附地址提取码|

    内容简介: <Python自动化运维:技术与最佳实践>一书在中国运维领域将有“划时代”的重要意义:一方面,这是国内第一本从纵.深和实践角度探讨Python在运维领域应用的著作:一方面本书的 ...

  3. Python自动化运维 技术与最佳实践PDF高清完整版免费下载|百度云盘|Python基础教程免费电子书

    点击获取提取码:7bl4 一.内容简介 <python自动化运维:技术与最佳实践>一书在中国运维领域将有"划时代"的重要意义:一方面,这是国内第一本从纵.深和实践角度探 ...

  4. python自动化运维之CMDB篇-大米哥

    python自动化运维之CMDB篇 视频地址:复制这段内容后打开百度网盘手机App,操作更方便哦 链接:https://pan.baidu.com/s/1Oj_sglTi2P1CMjfMkYKwCQ  ...

  5. Day1 老男孩python自动化运维课程学习笔记

    2017年1月7日老男孩python自动化运维课程正式开课 第一天学习内容: 上午 1.python语言的基本介绍 python语言是一门解释型的语言,与1989年的圣诞节期间,吉多·范罗苏姆为了在阿 ...

  6. python自动化运维学习第一天--day1

    学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...

  7. 【目录】Python自动化运维

    目录:Python自动化运维笔记 Python自动化运维 - day2 - 数据类型 Python自动化运维 - day3 - 函数part1 Python自动化运维 - day4 - 函数Part2 ...

  8. python自动化运维篇

    1-1 Python运维-课程简介及基础 1-2 Python运维-自动化运维脚本编写 2-1 Python自动化运维-Ansible教程-Ansible介绍 2-2 Python自动化运维-Ansi ...

  9. python自动化运维之路~DAY5

    python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...

随机推荐

  1. 【hibernate】hibernate不同版本的命名策略

    ===================================================hibernate 4命名策略如下================================ ...

  2. ios 开发keywordIBInspectable

    这个keyword 能够让开发人员省去非常多事情 把部分界面设置 放在ui设计这里 能够让别人分担 特别是 像我如今所呆的小公司老变样 让他们也能够改 这里仅仅点出一下 有兴趣 自行搜索

  3. Linux下快速安装Mysql及使用

    1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查看有没有安装包: yum list mysql* 安装mysql客户端: ...

  4. mysql if()一个奇怪的问题

    看起来一切正常......... 但是当使用不等于时 SELECT IF(1!=NULL,1,2) 居然返回2 SELECT IF(1!=NULL,1,2) >2 -- ------------ ...

  5. 可软件定义的存储逻辑——Efficient and agile storage management in software defined environments

            note:写这个或许算是翻译,又或算是对这个论文[1]的理解,又或者仅仅是我的看法.         这篇论文和IOFlow相比較,更加注重软件定义存储的框架(利用已有的框架来创建新的 ...

  6. 【LeetCode】Partition List ——链表排序问题

    [题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...

  7. bigAutocomplete实现联想

    直接举例说明: //xx联想 var list = $(".js-xxxx").text();//需要联想出的内容的list,该list由后台传入,保存在jsp页面,js取隐藏域值 ...

  8. Linux ps 命令查看进程启动及运行时间

    引言 同事问我怎样看一个进程的启动时间和运行时间,我第一反应当然是说用 ps 命令啦.ps aux或ps -ef不就可以看时间吗? ps aux选项及输出说明 我们来重新复习下ps aux的选项,这是 ...

  9. 应用程序之SingleViewApplication

    理论概念学习 iOS运行原理 代码结构分析 代码初步实现 一.理论学习 1⃣️.每一个应用程序都有属于自己的UIWindow,继承自UIView 2⃣️.每一个满屏的UIView都由一个UIViewC ...

  10. Sping Boot入门到实战之实战篇(二):一些常用功能的Spring Boot Starters

    包含功能 阿里云消息服务MNS 阿里云消息队列服务(即时消息.延迟消息.事务消息) AOP日志 基于MyBatis通用Mapper及DRUID的数据库访问 dubbo支持 错误处理 七牛图片服务 re ...