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\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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM