python自動化運維六:paramiko


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


免責聲明!

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



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