Python使用winrm遠端連線另一台PC並控制

2021-10-25 21:17:41 字數 2466 閱讀 8725

首先需要在windows上配置winrm來保證可以進行連線和控制:

參考鏈結

import winrm

)

執行結果為:

**並且在win7的pc上,該也會被刪除。

如果想要執行多條命令,使用&分隔即可:

t=s.run_cmd(

'dir & cd / & dir'

)

則會輸出兩個列表,執行結果不太方便放出。

提一下run_cmd()和run_ps():

前者是執行命令的,後者是執行指令碼的。

def run_cmd(self, command, args=()):

# todo optimize perf. do not call open/close shell every time

shell_id = self.protocol.open_shell()

command_id = self.protocol.run_command(shell_id, command, args)

rs = response(self.protocol.get_command_output(shell_id, command_id))

self.protocol.cleanup_command(shell_id, command_id)

self.protocol.close_shell(shell_id)

return rs

def run_ps(self, script):

"""base64 encodes a powershell script and executes the powershell

encoded script command

"""# must use utf16 little endian on windows

encoded_ps = b64encode(script.encode('utf_16_le')).decode('ascii')

rs = self.run_cmd('powershell -encodedcommand '.format(encoded_ps))

if len(rs.std_err):

# if there was an error message, clean it it up and make it human

# readable

rs.std_err = self._clean_error_msg(rs.std_err)

return rs

但是我也遇到了乙個問題,那就是想開啟網頁或者應用的時候就失敗了,但是這個命令在win7的命令列卻是可以執行的,燒腦殼。

#comm='start '+r''

#print(comm)

# s.run_cmd(comm)

# comm="start c:\gtja\richez\newver\loading.exe"

# t=s.run_cmd(comm)

這個問題還沒有解決,路過的大佬可以戳我一下,萬分感謝!

據說還有另一種方法,我還沒有嘗試,先記錄一下

import winrm

conn = winrm.protocol(endpoint='', transport='plaintext', username='administrator', password='password')

shell_id=conn.open_shell()

com=conn.run_command(shell_id,"dir")

stdout, stderr, return_code = conn.get_command_output(shell_id, com)

print "stdout: %s" % (stdout)

print "stderr: %s" % (stderr)

橫向遠端命令執行 WINRM

winrm windows遠端管理 是microsoft 在windows中對ws management的實現,它使系統可以跨通用網路訪問或交換管理資訊。利用指令碼物件或內建的命令列工具,winrm可以與可能具有基板管理控制器 bmc 的任何遠端計算機一起使用,以獲取資料。也可以獲取基於window...

使用PvCharm進行Python遠端除錯

python遠端除錯 一 介紹 python遠端除錯,即在遠端機器上執行python 在本地進行除錯。除錯環境 pycharm 需要依賴 遠端和本地需要在python路徑安裝pycharm debug.egg 注 安裝egg包需要使用先安裝setuptools 二 安裝setuptools url ...

如何使用SFTP刪除遠端目錄 Python

注意 sftp的rmdir只能刪除空目錄,所以如果目錄中存在檔案或子目錄,需要先刪除這些內容再刪除該目錄。如下所示 可以參考 1 importos2 import paramiko 3from stat import s isdir 45 server any.sftpserver 6 userna...