Python Combat first day-paramiko module and practice

Source: Internet
Author: User

Operating system Ubuntu 15.10  IDE & editor JetBrains pycharm 5.0.2 ipython3  python version python-3.4.3

Installing Paramiko

PIP3 Install Paramiko
[Email protected]:~$ pip3 Install paramikocollecting 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 ... dones Uccessfully installed ecdsa-0.13 paramiko-1.16.0 pycrypto-2.6.1

Import Paramiko Module

Import Paramiko

Create an SSH connection

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, bits) (active; 1 open channel (s)) >>> <paramiko. Channelfile from <paramiko. Channel 0 (Open) window=2097152-<paramiko. Transport at 0x980338d0 (cipher aes128-ctr, bits) (active; 1 open channel (s)) >>> <paramiko. Channelfile from <paramiko. Channel 0 (Open) window=2097152-<paramiko. Transport at 0x980338d0 (cipher aes128-ctr, bits) (active; 1 open channel (s)) >>>) ssh.set_missing_host_key_p Olicy (Paramiko. Autoaddpolicy ()) #先设定接收或处理对方主机发来秘钥的方式stdin, Stdout,stderr = Ssh.exec_command ("ifconfig |grep inet") #分割数据 Stdout.readline () out[137]: ' inet Address: 192.168.20.144 Broadcast: 192.168.20.255 mask: 255.255.255.0\n ' Stdout.readlines () OUT[146]: [' inet Address: 192.168.20.144 Broadcast: 192.168.20.255 mask: 255.255.255.0\n ', ' Inet6 address: fe80::20c:29ff:f ECE:3621/64 scope:link\n ', ' inet address: 127.0.0.1 Mask: 255.0.0.0\n ', ' Inet6 address::: 1/128 scope:host\n '] Std Out.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 \ 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\x 9e:0 \xe5\x8f\x91\xe9\x80\x81\xe9\x98\x9f\xe5\x88\x97\xe9\x95\xbf\xe5\xba\xa6:1000 \ \xe6\x8e\xa5\xe6\x94\xb6\x  e5\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 \ 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 M tu: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 \ \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 \ stdout.read (). Decode () #解码输出 out[169]: ' E no16777736 Link ENCAP: Ethernet hardware address 00:0c:29:ce:36:21 \ n inet Address: 192.168.20.144 Broadcast: 192.168.20.255 Mask: 255.255.255.0 \ inet6 Address: fe80::20c:29ff:fece:3621/64 scope:link\n up broadcast RUNNING multicast mtu:1500 metric: 1\ N Receive packet: 702687 Error: 0 Discard: 0 Overload: 0 Number of frames: 0\n Send packet: 40793 error: 0 Discard: 0 Overload: 0 Carrier: 0\n Collision: 0 Send Queue Length: $ \ n Bytes Received: 57343581 (57.3 MB) Send bytes: 14402696 (14.4 MB) \n\nlo Link encap: local loopback \ n inet Address: 127.0.0.1 Mask : 255.0.0.0\n Inet6 Address::: 1/128 scope:host\n up LOOPBACK RUNNING mtu:65536 metric: 1\n Receive packet: 2606 Error: 0 lost Discard: 0 Overload: 0 Number of frames: 0\n Send packet: 2606 Error: 0 Discard: 0 Overload: 0 Carrier: 0\n Collision: 0 Send Queue Length: 0 \ n Bytes Received: 501890 (501.8 KB) Send bytes: 501890 (501.8 KB)

Download a file to another directory via SSH

Ftp.get ('/etc/pam.conf ', '/tmp/pam.paramiko_bak ') #下载

ls/tmp-rw-rw-r--  1 Jim  Jim   552  February 18:00 Pam.paramiko_bak

Push a file to a directory via SSH

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

Defined

res = ftp.put ('/etc/hosts ', '/tmp/hosts.paramiko_bak ') if res:   print (upload succeeded)

Close connection

#关闭连接ftp. Close () Ssh.close ()

Python Combat first day-paramiko module and practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.