linux運維 - python遠程控制windows如何實現
問題描述
在windows沒有開啟ssh只開啟了mstsc的前提下python有沒有遠程控制win服務器的方案?想實現遠程關機的功能。
問題解答
回答1:(1)從Linux遠程關閉windows:
import osos.system('net rpc -S <ip address> -U <username>%<password> shutdown -t 1 -f')
(2)從windows遠程關閉windows: (reference)http://code.activestate.com/r...
#!/usr/bin/env python# win32shutdown.pyimport win32apiimport win32conimport win32netconimport win32securityimport win32wnetdef shutdown(host=None, user=None, passwrd=None, msg=None, timeout=0, force=1, reboot=0): ''' Shuts down a remote computer, requires NT-BASED OS. '''# Create an initial connection if a username & password is given. connected = 0 if user and passwrd:try: win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_ANY, None, ’’.join([r’’, host]), None, user, passwrd)# Don’t fail on error, it might just work without the connection.except: passelse: connected = 1 # We need the remote shutdown or shutdown privileges. p1 = win32security.LookupPrivilegeValue(host, win32con.SE_SHUTDOWN_NAME) p2 = win32security.LookupPrivilegeValue(host, win32con.SE_REMOTE_SHUTDOWN_NAME) newstate = [(p1, win32con.SE_PRIVILEGE_ENABLED),(p2, win32con.SE_PRIVILEGE_ENABLED)] # Grab the token and adjust its privileges. htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(), win32con.TOKEN_ALL_ACCESS) win32security.AdjustTokenPrivileges(htoken, False, newstate) win32api.InitiateSystemShutdown(host, msg, timeout, force, reboot) # Release the previous connection. if connected:win32wnet.WNetCancelConnection2(’’.join([r’’, host]), 0, 0)if __name__ == ’__main__’: # Immediate shutdown. shutdown(’salespc1’, ’admin’, ’secret’, None, 0) # Delayed shutdown 30 secs. shutdown(’salespc1’, ’admin’, ’secret’, ’Maintenance Shutdown’, 30) # Reboot shutdown(’salespc1’, ’admin’, ’secret’, None, 0, reboot=1) # Shutdown the local pc shutdown(None, ’admin’, ’secret’, None, 0)
相關文章:
1. android - 請問一下 類似QQ音樂底部播放 在每個頁面都顯示 是怎么做的?2. thinkPHP5中獲取數據庫數據后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙3. mysql主從 - 請教下mysql 主動-被動模式的雙主配置 和 主從配置在應用上有什么區別?4. 求救一下,用新版的phpstudy,數據庫過段時間會消失是什么情況?5. python小白 關于類里面的方法獲取變量失敗的問題6. Python2中code.co_kwonlyargcount的等效寫法7. django - Python error: [Errno 99] Cannot assign requested address8. python小白,關于函數問題9. [python2]local variable referenced before assignment問題10. python - vscode 如何在控制臺輸入
