linux運(yùn)維 - python遠(yuǎn)程控制windows如何實(shí)現(xiàn)
問題描述
在windows沒有開啟ssh只開啟了mstsc的前提下python有沒有遠(yuǎn)程控制win服務(wù)器的方案?想實(shí)現(xiàn)遠(yuǎn)程關(guān)機(jī)的功能。
問題解答
回答1:(1)從Linux遠(yuǎn)程關(guān)閉windows:
import osos.system('net rpc -S <ip address> -U <username>%<password> shutdown -t 1 -f')
(2)從windows遠(yuǎn)程關(guān)閉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)
相關(guān)文章:
1. php - 淘寶訂單拆單表設(shè)計(jì)2. 實(shí)現(xiàn)bing搜索工具urlAPI提交3. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器4. mysql優(yōu)化 - MySQL如何為配置表建立索引?5. 冒昧問一下,我這php代碼哪里出錯了???6. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)7. 關(guān)于mysql聯(lián)合查詢一對多的顯示結(jié)果問題8. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實(shí)在是找不到哪里的問題了。9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. windows誤人子弟啊
