Paramiko 模塊

什么是 Paramiko?
Paramiko 是一個(gè) Python 實(shí)現(xiàn)的 SSH 協(xié)議庫苏章,提供了 SSH 客戶端和 SSH 服務(wù)器的 API。它允許你通過 SSH 協(xié)議遠(yuǎn)程控制服務(wù)器驴党,進(jìn)行數(shù)據(jù)傳輸或在 Shell 中執(zhí)行命令等操作跪楞。
如何安裝 Paramiko?
Paramiko 可以使用 pip 安裝,命令如下:
pip install paramiko
如何使用 Paramiko 連接 SSH 服務(wù)器?
使用 Paramiko 連接 SSH 服務(wù)器可以通過如下代碼實(shí)現(xiàn):

import paramiko
# SSH credentials
ssh_host = 'your_ssh_host'
ssh_user = 'your_ssh_username'
ssh_password = 'your_ssh_password'
# Establish SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ssh_host, username=ssh_user, password=ssh_password)
# Execute command
command = 'your_ssh_command'
stdin, stdout, stderr = ssh.exec_command(command)
# Print output
print(stdout.read().decode())
# Close SSH connection
ssh.close()

如何使用 Paramiko 上傳和下載文件?
可以使用 Paramiko 的 SFTP API 上傳和下載文件:

import paramiko
# SFTP credentials
sftp_host = 'your_sftp_host'
sftp_user = 'your_sftp_username'
sftp_password = 'your_sftp_password'
# Establish SFTP connection
transport = paramiko.Transport((sftp_host, 22))
transport.connect(username=sftp_user, password=sftp_password)
sftp = transport.open_sftp()
# Download a remote file
remote_file_path = '/path/to/remote/file'
local_file_path = '/path/to/local/file'
sftp.get(remote_file_path, local_file_path)
# Upload a local file
local_file_path = '/path/to/local/file'
remote_file_path = '/path/to/remote/file'
sftp.put(local_file_path, remote_file_path)
# Close SFTP connection
sftp.close()
transport.close()

如何使用 Paramiko 執(zhí)行 sudo 命令?
可以使用 Paramiko 的 invoke_shell() 方法來模擬一個(gè)終端會(huì)話堡赔,然后執(zhí)行 sudo 命令:

import paramiko
# SSH credentials
ssh_host = 'your_ssh_host'
ssh_user = 'your_ssh_username'
ssh_password = 'your_ssh_password'
# Establish SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ssh_host, username=ssh_user, password=ssh_password)
# Start a shell session
shell = ssh.invoke_shell()
shell.send('sudo your_command\n')
# Wait for the password prompt
while not shell.recv_ready():
    pass
shell.send('your_password\n')
# Execute command
output = shell.recv(1024)
# Print output
print(output.decode())
 # Close SSH connection
ssh.close()

** 增加異常處理 **

import paramiko
import json


# SSH連接信息
hostname = "1.1.1.1"
username = "root"
password = "123456"
# 命令
command = "kubectl get cm -n test xxx-configmap -o json"
# 創(chuàng)建SSH客戶端
client = paramiko.SSHClient()
# 自動(dòng)添加主機(jī)密鑰
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())


# 連接SSH服務(wù)器
try:
    client.connect(hostname, username=username, password=password)
except paramiko.AuthenticationException:
    print("Authentication failed, please verify your password.")
except paramiko.SSHException as sshException:
    print("Unable to establish SSH connection: %s" % sshException)
except paramiko.SSHException as e:
    print(e)


# 執(zhí)行命令
try:
    stdin, stdout, stderr = client.exec_command(command, timeout=10)
    output = stdout.read().decode()
    print(output)
    result = json.loads(output)
    print(result["kind"])
except paramiko.SSHException as sshException:
    print("Unable to execute command: %s" % sshException)

# 關(guān)閉連接
client.close()

Paramiko 的 SSH 隧道功能

import paramiko
# SSH credentials
ssh_host = 'your_ssh_host'
ssh_user = 'your_ssh_username'
ssh_password = 'your_ssh_password'
# Tunnel credentials
tunnel_host = 'your_tunnel_host'
tunnel_port = your_tunnel_port
tunnel_user = 'your_tunnel_username'
tunnel_password = 'your_tunnel_password'
# Establish SSH tunnel
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(tunnel_host, username=tunnel_user, password=tunnel_password)
transport = ssh.get_transport()
local_port = 12345
remote_port = 54321
transport.request_port_forward('', local_port, remote_port)
# Establish SSH connection through the tunnel
ssh_tunnel = paramiko.SSHClient()
ssh_tunnel.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_tunnel.connect(ssh_host, username=ssh_user, password=ssh_password, sock=transport.open_channel('direct-tcpip', ('127.0.0.1', remote_port), ('127.0.0.1', local_port)))
# Execute command
command = 'your_ssh_command'
stdin, stdout, stderr = ssh_tunnel.exec_command(command)
# Print output
print(stdout.read().decode())
# Close SSH connection
ssh_tunnel.close()
transport.close()
ssh.close()

以上就是關(guān)于 Python Paramiko 模塊的文章總結(jié)识脆。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市善已,隨后出現(xiàn)的幾起案子灼捂,更是在濱河造成了極大的恐慌,老刑警劉巖换团,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件悉稠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡艘包,警方通過查閱死者的電腦和手機(jī)的猛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門耀盗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人卦尊,你說我怎么就攤上這事叛拷。” “怎么了猫牡?”我有些...
    開封第一講書人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵胡诗,是天一觀的道長。 經(jīng)常有香客問我淌友,道長煌恢,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任震庭,我火速辦了婚禮瑰抵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘器联。我一直安慰自己二汛,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開白布拨拓。 她就那樣靜靜地躺著肴颊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪渣磷。 梳的紋絲不亂的頭發(fā)上婿着,一...
    開封第一講書人閱讀 49,985評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音醋界,去河邊找鬼竟宋。 笑死,一個(gè)胖子當(dāng)著我的面吹牛形纺,可吹牛的內(nèi)容都是我干的丘侠。 我是一名探鬼主播,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼逐样,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼蜗字!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起脂新,我...
    開封第一講書人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤秽澳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后戏羽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡楼吃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年始花,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了妄讯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡酷宵,死狀恐怖亥贸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情浇垦,我是刑警寧澤炕置,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站男韧,受9級(jí)特大地震影響朴摊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜此虑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一甚纲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧朦前,春花似錦介杆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至恩伺,卻和暖如春赴背,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背莫其。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來泰國打工癞尚, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人乱陡。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓浇揩,卻偏偏與公主長得像,于是被迫代替她去往敵國和親憨颠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胳徽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容