zoukankan      html  css  js  c++  java
  • linux--远程连接windows并执行cmd命令

    1、在Linux服务器上的处理

    首先需要在Linux中安装python,并且安装pywinm库。安装之前首先需要安装isodate和xmlwitch两个依赖包,在安装pywinrm。下面是安装时用到的命令。

    pip install isodate
    pip install xmlwitch
    pip install pywinrm

    2、在window服务器上的操作

        在window服务器上,要打开winrm服务。下面是操作流程:

        第一步:用管理员权限打开windows powershell

               第二步:首先查看winrm service的运行状态,默认情况是没有开启的;执行命令为空是没有启动。

    winrm enumerate winrm/config/listener

        第三步:对winrm service进行配置

    winrm quickconfig

        第四步:为winrm service 配置auth:

    winrm set winrm/config/service/auth '@{Basic="true"}'
    winrm set winrm/config/service/auth '@{CredSSP="true"}'

        第五步:为winrm service 配置加密方式为允许非加密:

    winrm set winrm/config/service  '@{AllowUnencrypted="true"}'

        检验是否配置成功

    winrm get winrm/config/service/auth

    下面就是在linux可以远程操作Windows的python代码

    import winrm
    
    win = winrm.Session('http://windowsip:5985/wsman',auth=('windows管理员账户','windows管理员密码'))
    r = win.run_cmd('history')  #执行cmd命令
    print(str(r.std_out,encoding='gbk'))  # 打印获取到的信息
    print(str(r.std_err,encoding='gbk')) #打印错误信息

    下面python2的代码,略微一点不一样

    # -*- coding:utf-8 -*-
    import winrm
    
    win = winrm.Session('http://Windowsip:5985/wsman', auth=('windows管理员账户', 'windows管理员密码'))  # 连接windows
    r = win.run_cmd('ipconfig')  # 执行cmd命令
    print(r.std_out)  # 打印获取到的信息
    print(r.std_err) # 打印错误信息
  • 相关阅读:
    Java异常面试题
    Quickhit快速击键
    多态and接口
    Java面向对象编程概述
    学生管理系统--分层开发
    类型转换
    文件上传
    ongl(示例3-6 多值类型的数据处理)
    ongl(原始类型和包装类型)
    Interceptor
  • 原文地址:https://www.cnblogs.com/lutt/p/13579753.html
Copyright © 2011-2022 走看看