linux 远程控制windows系统下的程序(三种方法)
有时候我们需要通过在linux上远程运行windows系统上的程序。
方法一:
通过python中的winrm模块,前提条件,先提前设置好winrm服务。如何设置请自行百度,winRM服务是windowsserver下PowerShell的远程管理服务。Python脚本通过连接winRM模块操作windows命令行。
importwinrm
defcmd_views(ip,cmd_comand):
win=winrm.Session('http://'+ip+':5985/wsman',auth=('user','password'))#参数为用户名和密码
r=win.run_cmd(cmd_comand)#执行cmd命令=
returnr.std_out#打印获取到的信息
ip="xxx.xxx.xx.xx"
cmd_comand=r"ipconfig"#运行命令
a=cmd_views(ip,cmd_comand)
print(cmd_comand)
print(type(a))
print(a)
经过本人测试这个模块只能执行一些简单的命令,就是那种基本上能一输入就能立马响应结果的命令。碰到一些稍微复杂的,进程就挂掉了。
方法二:
通过python中的telnetlib库进行执行操作,前提设置windows系统中的telnet设置,1,安装telnet客户端和服务器端。2配置telnet用户权限,不会就自行百度设置。
#--coding:utf-8--
importtelnetlib,time
deftelnetlib_views(ipaddress,user,password,cmdname):
tn=telnetlib.Telnet(ipaddress)
a=tn.read_until(b'login:')
tn.write(user.encode('ascii')+b'\r\n')
tn.read_until(b'password:')
time.sleep(5)
tn.write(password.encode('ascii')+b'\r\n')
time.sleep(2)
tn.write(cmdname.encode('ascii')+b'\r\n')
tn.close()
cmdname=r'ifconfig'#运行命令
telnetlib_views(ipaddress="xxx.xxx.xxx.xxx",user="xxx",password="xxxx",cmdname=cmdname)
等待命令调用完成,程序结束。
方法三
利用wmi模块,缺陷只能通过windows-windows,linux-windows行不通,linux相关模块无法安装。
importwmi
defsys_version(ipaddress,user,password,cmdname):
conn=wmi.WMI(computer=ipaddress,user=user,password=password)
try:
cmd_callbat=r"cmd/ccall%s"%cmdname
print("当前执行bat命令为:",cmd_callbat)
conn.Win32_Process.Create(CommandLine=cmd_callbat)
exceptExceptionase:
print(e)
cmdname=r"xxx.bat"#运行命令
sys_version(ipaddress="xxx.xx.xx.xx",user="xx",password="xxx",cmdname=cmdname)#
命令被调用,程序结束,无需等待,区别于方法二,缺陷无法在linux上安装库
应用场景实用,现在我需要在linux上执行程序,将windows系统上的一个PDF格式的文件,传到linux上?
#--coding:utf-8--
importwinrm
defjob():
#获得连接
t=winrm.Session("xxx.xx.xx.xx",auth=("xx","xxx"))
#获得a.pdf内容
r=t.run_cmd(r'typeC:\xxx\xxx\Desktop\test\a.pdf')
#将内容转化成字符串
s0=str(r.std_out)
#print(s0)
withopen("c.pdf","wb")asf:
f.write(s0)
print("写入完成")
job()
总结
以上所述是小编给大家介绍的linux远程控制windows系统下的程序(三种方法),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。