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. windows误人子弟啊2. php传对应的id值为什么传不了啊有木有大神会的看我下方截图3. 如何用笔记本上的apache做微信开发的服务器4. python - linux 下用wsgifunc 运行web.py该如何修改代码5. 关于mysql联合查询一对多的显示结果问题6. 实现bing搜索工具urlAPI提交7. 冒昧问一下,我这php代码哪里出错了???8. mysql优化 - MySQL如何为配置表建立索引?9. MySQL主键冲突时的更新操作和替换操作在功能上有什么差别(如图)10. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。
