在實(shí)際的開(kāi)發(fā)中有時(shí)會(huì)遇到需要在ubuntu上遠(yuǎn)程登錄Windows的電腦去執(zhí)行一些比較特殊的命令。這個(gè)時(shí)候就需要使用python的paramiko模塊,首先去遠(yuǎn)程登錄,然后再去執(zhí)行對(duì)應(yīng)的cmd。
1 paramiko模塊簡(jiǎn)介
paramiko
是一個(gè)用于在Python中實(shí)現(xiàn)SSH(Secure Shell)協(xié)議的模塊,它提供了客戶端和服務(wù)器的功能,使得你能夠在網(wǎng)絡(luò)上安全地執(zhí)行命令、傳輸文件等。
1.1 安裝paramiko
你可以使用以下命令使用pip安裝paramiko:
pip3 install paramiko
1.2 paramiko基本用法
1.2.1 創(chuàng)建SSHClient實(shí)例
import paramiko
ssh = paramiko.SSHClient()
1.2.2 設(shè)置主機(jī)密鑰策略
在連接SSH服務(wù)器之前,建議設(shè)置主機(jī)密鑰策略,以便驗(yàn)證遠(yuǎn)程主機(jī)的身份:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
1.2.3 連接SSH服務(wù)器
ssh.connect(hostname='your_host', username='your_username', password='your_password')
1.2.4 執(zhí)行命令
以cmd為echo "Connection test"來(lái)介紹
cmd = 'echo "Connection test"'
stdin, stdout, stderr = ssh.exec_command(cmd)
1.2.5 關(guān)閉SSH連接
ssh.close()
1.2.6 異常處理
import paramiko
try:
# Your paramiko code here
ssh.close()
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
except paramiko.SSHException as e:
print(f"Unable to establish SSH connection: {e}")
except Exception as e:
print(f"An error occurred: {e}")
2 windows的配置
要通過(guò)SSH連接到Windows電腦,你需要使用SSH客戶端,并確保Windows電腦上已啟用了OpenSSH服務(wù)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-758123.html
2.1 啟動(dòng)OpenSSH服務(wù)
- 打開(kāi)服務(wù)管理器。你可以按Win + R打開(kāi)運(yùn)行對(duì)話框,然后輸入services.msc并按Enter。
- 在服務(wù)管理器中找到"OpenSSH SSH Server"服務(wù),確保其狀態(tài)為“已啟動(dòng)”,并將啟動(dòng)類型設(shè)置為“自動(dòng)”。
2.2 配置防火墻
- 如果Windows防火墻啟用,確保允許SSH流量。你可以在“控制面板”中的“系統(tǒng)和安全”下找到“Windows Defender 防火墻”,然后選擇“允許應(yīng)用通過(guò)防火墻”。
- 在列表中找到“OpenSSH服務(wù)器”并確保其允許。
3 Ubuntu配置
3.1 安裝ssh客戶端
sudo apt-get update
sudo apt-get install openssh-client
3.2 測(cè)試是否可以遠(yuǎn)程鏈接到Windows
ssh username@your-windows-ip
4 paramiko使用完整測(cè)試樣例
該測(cè)試用例首先使用echo "Connection test"
的測(cè)試命令去測(cè)試遠(yuǎn)程鏈接是否確實(shí)建立。然后再去執(zhí)行真是的測(cè)試命令,進(jìn)入到Windows D盤的test目錄,去獲取test目錄下的所有文件和目錄,然后輸出結(jié)果。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-758123.html
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
user = 'xxx'
remote_host = 'yyy'
passwd = 'zzz'
cmd = f'powershell.exe cd D:\\test; Get-ChildItem;'
try:
ssh.connect(remote_host, username = user, password = passwd)
print(user, '@', remote_host, ": connected successfully.")
print(" =================== 1 ======================")
# 執(zhí)行一個(gè)簡(jiǎn)單的命令(例如:echo)來(lái)確認(rèn)連接確實(shí)建立
stdin, stdout, stderr = ssh.exec_command('echo "Connection test"')
# 獲取命令輸出(如果有)
output = stdout.read().decode('utf-8', errors='ignore').strip()
error = stderr.read().decode('utf-8', errors='ignore').strip()
print(" =================== 2 ======================")
# 根據(jù)命令執(zhí)行結(jié)果輸出信息
if output:
print("Connection test succeeded:", output)
if error:
print("Connection test had errors:", error)
print(" =================== 3 ======================")
# 執(zhí)行一個(gè)簡(jiǎn)單的命令(例如:echo)來(lái)確認(rèn)連接確實(shí)建立
stdin, stdout, stderr = ssh.exec_command(cmd)
print(" =================== 3 - 1 ======================")
# 獲取命令輸出(如果有)
output = stdout.read().decode('ISO-8859-1', errors='ignore').strip()
print(" =================== 3 - 2 ======================")
error = stderr.read().decode('utf-8', errors='ignore').strip()
print(" =================== 3 - 3 ======================")
print(" =================== 4 ======================")
# 根據(jù)命令執(zhí)行結(jié)果輸出信息
if output:
print("Connection test succeeded:", output)
if error:
print("Connection test had errors:", error)
print(" =================== 5 ======================")
# 關(guān)閉連接
ssh.close()
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
except paramiko.SSHException as sshException:
print("Unable to establish SSH connection: %s" % sshException)
except paramiko.BadHostKeyException as badHostKeyException:
print("Unable to verify server's host key: %s" % badHostKeyException)
except Exception as e:
print(e)
到了這里,關(guān)于ubuntu如何遠(yuǎn)程ssh登錄Windows環(huán)境并執(zhí)行測(cè)試命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!