Python paramiko使用方法代碼匯總
1、用戶名、密碼登陸方式
import paramiko paramiko.util.log_to_file('paramiko.log') # 記錄日志文件 ssh = paramiko.SSHClient() try: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('139.xx.xx.xx', username='work', password='***') cmd = 'ls' # 需要執(zhí)行的Linux命名 stdin, stdout, stderr = ssh.exec_command(cmd) #執(zhí)行命令后的結(jié)構(gòu) print(stdout.readlines()) print(stdout.read().decode()) except Exception as e: print("%s:%s" % (e.__class__, e)) finally: # 關(guān)閉 ssh.close()
2、免密登陸方式
import paramiko ssh = paramiko.SSHClient() SSH_PRIVATE_KEY ='/Users/xueerhuan/.ssh/id_rsa' #本地密鑰文件路徑 try: key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY) # 無解密密碼時(shí) #key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY, password='******') # 有解密密碼時(shí), ssh.load_system_host_keys() #通過known_hosts 方式進(jìn)行認(rèn)證可以用這個(gè),如果known_hosts 文件未定義還需要定義 known_hosts #ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 通過公共方式進(jìn)行認(rèn)證 (不需要在known_hosts 文件中存在) ssh.connect(hostname='139.XX.XX.XX', port=22, username='root', pkey=key) stdin, stdout, stderr = ssh.exec_command("ps") # 獲取命令結(jié)果 result = stdout.read() # 打印輸出 print(result.decode()) except Exception as e: print("%s:%s" % (e.__class__, e)) finally: # 關(guān)閉 ssh.close()
注意:生成密碼的方法
A、進(jìn)入本地 ssh文件夾 cd .ssh/
B、使用ssh-keygen生產(chǎn)本地公鑰和私鑰 ssh-keygen
xueerhuan@ubuntu:~/.ssh$ ls
id_rsa id_rsa.pub
C、將生成的id_rsa.pub文件中的內(nèi)容copy到目標(biāo)機(jī)的.ssh/authorized_keys中就可以了,如果沒有authorized_keys,自己創(chuàng)建。但是要注意authorized_keys的權(quán)限一般是600
或者直接在本地使用一條命令也可以實(shí)現(xiàn)公鑰的復(fù)制,ssh-copy-id后面接入的用戶就是要支持免密登錄的用戶。
morra@ubuntu:~/.ssh$ ssh-copy-id "morra@192.168.1.42" /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/morra/.ssh/id_rsa.pub" The authenticity of host '192.168.1.42 (192.168.1.42)' can't be established. ECDSA key fingerprint is SHA256:/ufx+/OLtdsYy7vsdk4KDu9xJsBp6zHonRAf2jjT0GI. Are you sure you want to continue connecting (yes/no)? n^H Please type 'yes' or 'no': yes /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'morra@192.168.1.42'" and check to make sure that only the key(s) you wanted were added. #去目標(biāo)機(jī)器下,檢查authorized_keys文件 localhost:.ssh morra$ cat authorized_keys
3、密碼上傳文件
import os import paramiko ssh = paramiko.SSHClient() SSH_PRIVATE_KEY ='/Users/xueerhuan/.ssh/id_rsa' #本地密鑰文件路徑 key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY) paramiko.util.log_to_file('paramiko.log') ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('139.XX.XX.XX', username='root', password='***') t = ssh.get_transport() sftp = paramiko.SFTPClient.from_transport(t) d = sftp.put("mm.txt", "/home/work/.ssh/mm.txt") print(d)
4、免密上傳文件
import os import paramiko ssh = paramiko.SSHClient() SSH_PRIVATE_KEY ='/Users/xueerhuan/.ssh/id_rsa' #本地密鑰文件路徑 key = paramiko.RSAKey.from_private_key_file(SSH_PRIVATE_KEY) paramiko.util.log_to_file('paramiko.log') ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='139.XX.XX.XX', port=22, username='root', pkey=key) t = ssh.get_transport() sftp = paramiko.SFTPClient.from_transport(t) d = sftp.put("mm.txt", "/home/work/.ssh/mm.txt") print(d)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python使用Paramiko庫實(shí)現(xiàn)SSH管理詳解
- python的paramiko模塊基本用法詳解
- Python通過paramiko庫實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法
- Python運(yùn)維自動化之paramiko模塊應(yīng)用實(shí)例
- Python中paramiko模塊的基礎(chǔ)操作與排錯(cuò)問題
- Python遠(yuǎn)程SSH庫Paramiko詳細(xì)操作
- python 第三方庫paramiko的常用方式
- Python如何實(shí)現(xiàn)Paramiko的二次封裝
- python 使用paramiko模塊進(jìn)行封裝,遠(yuǎn)程操作linux主機(jī)的示例代碼
- Python Paramiko模塊中exec_command()和invoke_shell()兩種操作區(qū)別
相關(guān)文章
python使用Qt界面以及邏輯實(shí)現(xiàn)方法
這篇文章主要介紹了python使用Qt界面以及邏輯實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07利用Python實(shí)現(xiàn)QQ實(shí)時(shí)到賬免簽支付功能
這篇文章主要介紹了利用Python實(shí)現(xiàn)QQ實(shí)時(shí)到賬免簽支付功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03python動態(tài)參數(shù)用法實(shí)例分析
這篇文章主要介紹了python動態(tài)參數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python中動態(tài)參數(shù)的功能及使用技巧,需要的朋友可以參考下2015-05-05Python3.9.0 a1安裝pygame出錯(cuò)解決全過程(小結(jié))
這篇文章主要介紹了Python3.9.0 a1安裝pygame出錯(cuò)解決全過程(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼
這篇文章主要介紹了python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼,簡單介紹了實(shí)現(xiàn)步驟,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python+django實(shí)現(xiàn)文件上傳
本系列以可操作性為主,介紹如何通過django web框架來實(shí)現(xiàn)一些簡單的功能。每一篇文章都具有完整性和獨(dú)立性。使用新手在動手做的過程中體會web開發(fā)的過程,過程中細(xì)節(jié)請參考相關(guān)文檔。2016-01-01