• python实战第一天-paramiko模块并练习


    操作系统
     
    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
    '
    
     stdout.readlines()
    Out[146]: 
    ['          inet 地址:192.168.20.144  广播:192.168.20.255  掩码:255.255.255.0
    ',
     '          inet6 地址: fe80::20c:29ff:fece:3621/64 Scope:Link
    ',
     '          inet 地址:127.0.0.1  掩码:255.0.0.0
    ',
     '          inet6 地址: ::1/128 Scope:Host
    ']
     
     stdout.read()   #原始数据格式
    Out[161]: b'eno16777736 Link encap:xe4xbbxa5xe5xa4xaaxe7xbdx91  xe7xa1xacxe4xbbxb6xe5x9cxb0xe5x9dx80 00:0c:29:ce:36:21  
    
              inet xe5x9cxb0xe5x9dx80:192.168.20.144  xe5xb9xbfxe6x92xad:192.168.20.255  xe6x8exa9xe7xa0x81:255.255.255.0
    
    		  inet6 xe5x9cxb0xe5x9dx80: fe80::20c:29ff:fece:3621/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  
    		  xe8xb7x83xe7x82xb9xe6x95xb0:1
              xe6x8exa5xe6x94xb6xe6x95xb0xe6x8dxaexe5x8cx85:701920 
    		  xe9x94x99xe8xafxaf:0 xe4xb8xa2xe5xbcx83:0 xe8xbfx87xe8xbdxbd:0 xe5xb8xa7xe6x95xb0:0
              
    		  xe5x8fx91xe9x80x81xe6x95xb0xe6x8dxaexe5x8cx85:40151 xe9x94x99xe8xafxaf:0 xe4xb8xa2xe5xbcx83:0
    		  xe8xbfx87xe8xbdxbd:0 xe8xbdxbdxe6xb3xa2:0
              xe7xa2xb0xe6x92x9e:0
    		  xe5x8fx91xe9x80x81xe9x98x9fxe5x88x97xe9x95xbfxe5xbaxa6:1000 
    
              xe6x8exa5xe6x94xb6xe5xadx97xe8x8ax82:57283741 (57.2 MB)  xe5x8fx91xe9x80x81xe5xadx97xe8x8ax82:14262542 (14.2 MB)
    
    lo
    		  Link encap:xe6x9cxacxe5x9cxb0xe7x8exafxe5x9bx9e  
              inet xe5x9cxb0xe5x9dx80:127.0.0.1  xe6x8exa9xe7xa0x81:255.0.0.0
    
              inet6 xe5x9cxb0xe5x9dx80: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  xe8xb7x83xe7x82xb9xe6x95xb0:1
              
    		  xe6x8exa5xe6x94xb6xe6x95xb0xe6x8dxaexe5x8cx85:2578 xe9x94x99xe8xafxaf:0 xe4xb8xa2xe5xbcx83:0 xe8xbfx87xe8xbdxbd:0 
    		  xe5xb8xa7xe6x95xb0:0
              xe5x8fx91xe9x80x81xe6x95xb0xe6x8dxaexe5x8cx85:2578 xe9x94x99xe8xafxaf:0 
    		  xe4xb8xa2xe5xbcx83:0 xe8xbfx87xe8xbdxbd:0 xe8xbdxbdxe6xb3xa2:0
             
    		  xe7xa2xb0xe6x92x9e:0 xe5x8fx91xe9x80x81xe9x98x9fxe5x88x97xe9x95xbfxe5xbaxa6:0 
              
    		  xe6x8exa5xe6x94xb6xe5xadx97xe8x8ax82:497170 (497.1 KB)  xe5x8fx91xe9x80x81xe5xadx97xe8x8ax82:497170 (497.1 KB)
    
    '
    
    
    stdout.read().decode()      #解码输出                       
    Out[169]: 'eno16777736 Link encap:以太网  硬件地址 00:0c:29:ce:36:21  
    
              inet 地址:192.168.20.144  广播:192.168.20.255  掩码:255.255.255.0
    
              inet6 地址: fe80::20c:29ff:fece:3621/64 Scope:Link
              
    		  UP BROADCAST RUNNING MULTICAST  MTU:1500  跃点数:1
              
    		  接收数据包:702687 错误:0 丢弃:0 过载:0 帧数:0
              
    		  发送数据包:40793 错误:0 丢弃:0 过载:0 载波:0
              
    		  碰撞:0 发送队列长度:1000 
    
    		  接收字节:57343581 (57.3 MB)
    		  发送字节:14402696 (14.4 MB)
    
    lo
    		  Link encap:本地环回  
    
              inet 地址:127.0.0.1  掩码:255.0.0.0
    
    		  inet6 地址: ::1/128 Scope:Host
    
              UP LOOPBACK RUNNING  MTU:65536  跃点数:1
              
    		  接收数据包:2606 错误:0 丢弃:0 过载:0 帧数:0
              
    		  发送数据包:2606 错误:0 丢弃:0 过载:0 载波:0
              
    		  碰撞:0 发送队列长度:0 
              
    		  接收字节: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()
    
  • 相关阅读:
    大道至简第六章-从编程到工程
    Java动手动脑-接口继承
    随机数生成数组元素求和
    大道至简第五章-失败的过程也是过程
    课堂-字符串加密
    字符串加密
    课堂动手动脑-3及字符串加密
    java课堂回答
    读后感
    从编辑懂工程
  • 原文地址:https://www.cnblogs.com/love19791125/p/5217963.html
Copyright © 2020-2023  润新知