操作系统

Ubuntu 15.10

IDE & editor

JetBrains PyCharm 5.0.2

ipython3

Python版本

python-3.4.3

安装paramiko

pip3 install paramiko
jim@jim-virtual-machine:~$ pip3 install paramiko
Collecting paramiko
Downloading paramiko-1.16.0-py2.py3-none-any.whl (169kB)
100% |████████████████████████████████| 172kB 1.0MB/s
Collecting ecdsa>=0.11 (from paramiko)
Downloading ecdsa-0.13-py2.py3-none-any.whl (86kB)
100% |████████████████████████████████| 90kB 2.3MB/s
Collecting pycrypto!=2.4,>=2.1 (from paramiko)
Downloading pycrypto-2.6.1.tar.gz (446kB)
100% |████████████████████████████████| 446kB 162kB/s
Installing collected packages: ecdsa, pycrypto, paramiko
Running setup.py install for pycrypto ... done
Successfully installed ecdsa-0.13 paramiko-1.16.0 pycrypto-2.6.1

导入paramiko模块

import paramiko

创建ssh连接

ssh = paramiko.SSHClient()

ssh.connect("192.168.20.144",port=22,username="jim",password="love19791125") #ssh连接主机
ssh.exec_command("ifconfig") #查看ipconfig
Out[130]:
(<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x980338d0 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>,
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x980338d0 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>,
<paramiko.ChannelFile from <paramiko.Channel 0 (open) window=2097152 -> <paramiko.Transport at 0x980338d0 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>) ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #先设定接收或处理对方主机发来秘钥的方式 stdin,stdout,stderr = ssh.exec_command("ifconfig |grep inet") #分割数据 stdout.readline()
Out[137]: ' inet 地址:192.168.20.144 广播:192.168.20.255 掩码:255.255.255.0\n' stdout.readlines()
Out[146]:
[' inet 地址:192.168.20.144 广播:192.168.20.255 掩码:255.255.255.0\n',
' inet6 地址: fe80::20c:29ff:fece:3621/64 Scope:Link\n',
' inet 地址:127.0.0.1 掩码:255.0.0.0\n',
' inet6 地址: ::1/128 Scope:Host\n'] stdout.read() #原始数据格式
Out[161]: b'eno16777736 Link encap:\xe4\xbb\xa5\xe5\xa4\xaa\xe7\xbd\x91 \xe7\xa1\xac\xe4\xbb\xb6\xe5\x9c\xb0\xe5\x9d\x80 00:0c:29:ce:36:21 \n
inet \xe5\x9c\xb0\xe5\x9d\x80:192.168.20.144 \xe5\xb9\xbf\xe6\x92\xad:192.168.20.255 \xe6\x8e\xa9\xe7\xa0\x81:255.255.255.0\n
inet6 \xe5\x9c\xb0\xe5\x9d\x80: fe80::20c:29ff:fece:3621/64 Scope:Link\n UP BROADCAST RUNNING MULTICAST MTU:1500
\xe8\xb7\x83\xe7\x82\xb9\xe6\x95\xb0:1\n \xe6\x8e\xa5\xe6\x94\xb6\xe6\x95\xb0\xe6\x8d\xae\xe5\x8c\x85:701920
\xe9\x94\x99\xe8\xaf\xaf:0 \xe4\xb8\xa2\xe5\xbc\x83:0 \xe8\xbf\x87\xe8\xbd\xbd:0 \xe5\xb8\xa7\xe6\x95\xb0:0\n
\xe5\x8f\x91\xe9\x80\x81\xe6\x95\xb0\xe6\x8d\xae\xe5\x8c\x85:40151 \xe9\x94\x99\xe8\xaf\xaf:0 \xe4\xb8\xa2\xe5\xbc\x83:0
\xe8\xbf\x87\xe8\xbd\xbd:0 \xe8\xbd\xbd\xe6\xb3\xa2:0\n \xe7\xa2\xb0\xe6\x92\x9e:0
\xe5\x8f\x91\xe9\x80\x81\xe9\x98\x9f\xe5\x88\x97\xe9\x95\xbf\xe5\xba\xa6:1000 \n
\xe6\x8e\xa5\xe6\x94\xb6\xe5\xad\x97\xe8\x8a\x82:57283741 (57.2 MB) \xe5\x8f\x91\xe9\x80\x81\xe5\xad\x97\xe8\x8a\x82:14262542 (14.2 MB)\n\nlo
Link encap:\xe6\x9c\xac\xe5\x9c\xb0\xe7\x8e\xaf\xe5\x9b\x9e \n inet \xe5\x9c\xb0\xe5\x9d\x80:127.0.0.1 \xe6\x8e\xa9\xe7\xa0\x81:255.0.0.0\n
inet6 \xe5\x9c\xb0\xe5\x9d\x80: ::1/128 Scope:Host\n UP LOOPBACK RUNNING MTU:65536 \xe8\xb7\x83\xe7\x82\xb9\xe6\x95\xb0:1\n
\xe6\x8e\xa5\xe6\x94\xb6\xe6\x95\xb0\xe6\x8d\xae\xe5\x8c\x85:2578 \xe9\x94\x99\xe8\xaf\xaf:0 \xe4\xb8\xa2\xe5\xbc\x83:0 \xe8\xbf\x87\xe8\xbd\xbd:0
\xe5\xb8\xa7\xe6\x95\xb0:0\n \xe5\x8f\x91\xe9\x80\x81\xe6\x95\xb0\xe6\x8d\xae\xe5\x8c\x85:2578 \xe9\x94\x99\xe8\xaf\xaf:0
\xe4\xb8\xa2\xe5\xbc\x83:0 \xe8\xbf\x87\xe8\xbd\xbd:0 \xe8\xbd\xbd\xe6\xb3\xa2:0\n
\xe7\xa2\xb0\xe6\x92\x9e:0 \xe5\x8f\x91\xe9\x80\x81\xe9\x98\x9f\xe5\x88\x97\xe9\x95\xbf\xe5\xba\xa6:0 \n
\xe6\x8e\xa5\xe6\x94\xb6\xe5\xad\x97\xe8\x8a\x82:497170 (497.1 KB) \xe5\x8f\x91\xe9\x80\x81\xe5\xad\x97\xe8\x8a\x82:497170 (497.1 KB)\n\n' stdout.read().decode() #解码输出
Out[169]: 'eno16777736 Link encap:以太网 硬件地址 00:0c:29:ce:36:21 \n
inet 地址:192.168.20.144 广播:192.168.20.255 掩码:255.255.255.0\n
inet6 地址: fe80::20c:29ff:fece:3621/64 Scope:Link\n
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1\n
接收数据包:702687 错误:0 丢弃:0 过载:0 帧数:0\n
发送数据包:40793 错误:0 丢弃:0 过载:0 载波:0\n
碰撞:0 发送队列长度:1000 \n
接收字节:57343581 (57.3 MB)
发送字节:14402696 (14.4 MB)\n\nlo
Link encap:本地环回 \n
inet 地址:127.0.0.1 掩码:255.0.0.0\n
inet6 地址: ::1/128 Scope:Host\n
UP LOOPBACK RUNNING MTU:65536 跃点数:1\n
接收数据包:2606 错误:0 丢弃:0 过载:0 帧数:0\n
发送数据包:2606 错误:0 丢弃:0 过载:0 载波:0\n
碰撞:0 发送队列长度:0 \n
接收字节:501890 (501.8 KB)
发送字节:501890 (501.8 KB)

通过ssh下载一个文件到另一个目录下去

ftp.get('/etc/pam.conf','/tmp/pam.paramiko_bak') #下载
ls /tmp
-rw-rw-r-- 1 jim jim 552 2月 25 18:00 pam.paramiko_bak

通过ssh推送一个文件到一个目录去

ftp.put('/etc/hosts','/tmp/hosts.paramiko_bak')  #推送
Out[175]: <SFTPAttributes: [ size=234 uid=1000 gid=1000 mode=0o100664 atime=1456394669 mtime=1456394669 ]>   

定义

res = ftp.put('/etc/hosts','/tmp/hosts.paramiko_bak')
if res:
print(上传成功)

关闭连接

#关闭连接
ftp.close()
ssh.close()

最新文章

  1. svn利用钩子实现代码同步到web目录
  2. iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区
  3. Codeforces Round #389 Div.2 C. Santa Claus and Robot
  4. 【iCore3 双核心板】例程五:SYSTICK定时器实验——定时点亮LED
  5. Windows程序==&gt;&gt;使用ListView控件展示数据
  6. python 自定义排序函数
  7. php连接函数implode()和分割explode()
  8. ASP.NET MVC 中使用 AjaxFileUpload 插件时,上传图片后不能显示(预览)
  9. js 中&amp;&amp; 与 ||
  10. Android开发了解——Dalvik
  11. 移动平台作业——天气预报——天气数据的获得——为应用申请百度ak码
  12. CSS3 box-sizing 属性
  13. COJ 0248 HDNOIP201408生成树
  14. 几个BCB例子
  15. JavaScript高级编程
  16. Grunt和Gulp构建工具在Visual Studio 2015中的高效的应用
  17. js脚本都可以放在哪些地方
  18. JVM(五)内存(Heap)分配
  19. JavaScript(jquery)实现二级菜单联动
  20. Maven项目POM文件错误,提示“Plugin execution not covered by lifecycle configuration”的解决方案

热门文章

  1. ant使用
  2. Swift 求余运算
  3. 【Android Developers Training】 79. 连接到网络
  4. jq-animate
  5. crm管理系统
  6. pouchdb-find( pouchdb查询扩展插件 ,便于查询)
  7. Windows PowerShell 默认颜色
  8. SQL数据库操作(CURD)
  9. 使用Jenkins进行持续集成ionic3项目
  10. Java缓存框架使用EhCache结合Spring AOP