標籤:sftp private hosts 指定 bubuko file 自己 建立秘鑰 app
Python的paramiko模組能夠串連遠程主機,並在該主機上執行命令,和該主機之間進行檔案傳輸。paramiko支援用純文字密碼登入遠程主機和秘鑰登入。使用之前要安裝一下這個模組哈,pip install paramiko,不會安裝可以百度哈。
(1)密碼登入
#! /usr/bin/env python# -*- coding: utf-8 -*-# Author: "Liuyouyuan"# Date: 2018/1/23import paramikodef run_cmd(host, port, user, passwd, cmd): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=host, port=port, username=user, password=passwd) stdin, stdout, stderr = ssh.exec_command(cmd) stdout = stdout.read() stderr = stderr.read() ssh.close() if not stderr: print(stdout.decode()) else: print(stderr.decode())if __name__ == ‘__main__‘: HOST = "10.129.205.151" PORT = 22 USER = "root" PASSWD = "******" cmd = "df -h" run_cmd(HOST, PORT, USER, PASSWD, cmd)
來看執行結果:
Filesystem Size Used Avail Use% Mounted on/dev/mapper/cl-root 17G 6.8G 11G 40% /devtmpfs 897M 0 897M 0% /devtmpfs 912M 84K 912M 1% /dev/shmtmpfs 912M 9.0M 903M 1% /runtmpfs 912M 0 912M 0% /sys/fs/cgroup/dev/sda1 1014M 173M 842M 18% /boottmpfs 183M 16K 183M 1% /run/user/42tmpfs 183M 0 183M 0% /run/user/0
(2)秘鑰登入
def run_cmd_pkey(host, port, user, rsa_file): """ linux 端建立秘鑰 ssh-keygen 把要連的機子的公開金鑰改名 mv id_rsa.pub authorized_keys 想要串連哪台機器就直接把公開金鑰copy到該機器的使用者home下 格式:/root/.ssh """ private_key = paramiko.RSAKey.from_private_key_file(rsa_file) # 指定私密金鑰所在檔案 ssh = paramiko.SSHClient() # 建立ssh對象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允許串連不在know_hosts檔案中的主機 ssh.connect(hostname=host, port=port, username=user, pkey=private_key) # 串連伺服器 stdin, stdout, stderr = ssh.exec_command("df") # 執行命令 stdout = stdout.read() stderr = stderr.read() ssh.close() # 關閉串連 if not stderr: print(stdout.decode()) else: print(stderr.decode())
(3)上傳下載檔案純文字密碼版本
import paramiko
def ssh_transfer_file(host, port, user, passwd, local_file, remote_file): transport = paramiko.Transport(host, port) transport.connect(username=user, password=passwd) sftp = paramiko.SFTPClient.from_transport(transport) # sftp.put(local_file, remote_file) # 從本地上傳檔案到遠程主機 sftp.get(remote_file, local_file) # 從遠程主機下載到本地 transport.close()if __name__ == ‘__main__‘: HOST = "10.129.205.151" PORT = 22 USER = "root" PASSWD = "******" cmd = "df -h" remote_file = "/data/download/nginx-1.12.0.tar.gz" local_file = "nginx-1.12.0.tar.gz" ssh_transfer_file(HOST, PORT, USER, PASSWD, local_file, remote_file)
先測試下下載功能。
首先看下遠程主機上的檔案:
[[email protected] download]# ll /data/downloadtotal 17444drwxr-xr-x. 9 lyy lyy 186 Jul 19 2017 nginx-1.12.0-rw-r--r--. 1 root root 980831 Apr 12 2017 nginx-1.12.0.tar.gzdrwxr-xr-x. 18 501 501 4096 Jul 19 2017 Python-3.6.1-rw-r--r--. 1 root root 16872064 Mar 21 2017 Python-3.6.1.tar.xz
View Code
然後運行代碼後看看本地:
這就已經把nginx壓縮包下載到本地了。上傳功能就不做示範了,有興趣的可以自己測試一下哈。
(4)上傳下載檔案秘鑰版
def ssh_transfer_file_pkey(host, port, user, rsa_file, local_file, remote_file): private_key = paramiko.RSAKey.from_private_key_file(rsa_file) # 指定私密金鑰所在檔案 transport = paramiko.Transport(host, port) transport.connect(username=user, pkey=private_key) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(local_file, remote_file) # 從本地上傳檔案到遠程主機 # sftp.get(remote_file, local_file) # 從遠程主機下載到本地 transport.close()
是不是比較簡單呢?
Python遠端連線主機之paramiko模組