首页 > 系统相关 >python-远程连接windows机器

python-远程连接windows机器

时间:2023-01-31 11:19:36浏览次数:44  
标签:fe80 false python WinRM winrm windows Address true 远程

python-远程连接windows机器

1. python-远程连接windows机器

  • 安装远程连接windows机器库

    pip install pywinrm
    
  • 在远程连接windows机器开启winrm用于远程管理

    • 查看winrm服务状态,默认没有启动

      winrm enumerate winrm/config/listener
      
    • 执行开启winrm服务

       winrm quickconfig
      
      • 执行报错

        C:\WINDOWS\system32>winrm quickconfig
        已在此计算机上运行 WinRM 服务。
        WSManFault
            Message
                ProviderFault
                    WSManFault
                        Message = 由于此计算机上的网络连接类型之一设置为公用,因此 WinRM 防火墙例外将不运行。 将网络连接类型更改为域或专用,然后再次尝试。
        
        错误编号: -2144108183 0x80338169
        由于此计算机上的网络连接类型之一设置为公用,因此 WinRM 防火墙例外将不运行。 将网络连接类型更改为域或专用,然后再次尝试。
        
      • 我们需要打开网络和Internet设置
        image
        image

      • 再次运行

        C:\WINDOWS\system32>winrm quickconfig
        已在此计算机上运行 WinRM 服务。
        WinRM 没有设置成为了管理此计算机而允许对其进行远程访问。
        必须进行以下更改:
        
        启用 WinRM 防火墙异常。
        配置 LocalAccountTokenFilterPolicy 以远程向本地用户授予管理权限。
        
        执行这些更改吗[y/n]? y
        
        WinRM 已经进行了更新,以用于远程管理。
        
        WinRM 防火墙异常已启用。
        已配置 LocalAccountTokenFilterPolicy 以远程向本地用户授予管理权限。
        
    • 查看winrm service listener(分为http和https):

      C:\WINDOWS\system32>winrm enumerate winrm/config/listener
      Listener
          Address = *
          Transport = HTTP
          Port = 5985
          Hostname
          Enabled = true
          URLPrefix = wsman
          CertificateThumbprint
          ListeningOn = 10.0.0.4, 127.0.0.1, 172.16.128.98, 192.168.18.1, 192.168.176.1, ::1, fe80::3323:cbd2:b3ef:3e84%7, fe80::5976:b173:5426:9980%10, fe80::8b57:92c0:d677:6851%16, fe80::f5ab:4ac7:ee2e:9c20%8
      
    • 为winrm service 配置auth:

      C:\WINDOWS\system32>winrm set winrm/config/service/auth @{Basic="true"}
      Auth
          Basic = true
          Kerberos = true
          Negotiate = true
          Certificate = false
          CredSSP = false
          CbtHardeningLevel = Relaxed
      
    • 为winrm service 配置加密方式为允许非加密:

      C:\WINDOWS\system32>winrm set winrm/config/service @{AllowUnencrypted="true"}
      Service
          RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
          MaxConcurrentOperations = 4294967295
          MaxConcurrentOperationsPerUser = 1500
          EnumerationTimeoutms = 240000
          MaxConnections = 300
          MaxPacketRetrievalTimeSeconds = 120
          AllowUnencrypted = true
          Auth
              Basic = true
              Kerberos = true
              Negotiate = true
              Certificate = false
              CredSSP = false
              CbtHardeningLevel = Relaxed
          DefaultPorts
              HTTP = 5985
              HTTPS = 5986
          IPv4Filter = *
          IPv6Filter = *
          EnableCompatibilityHttpListener = false
          EnableCompatibilityHttpsListener = false
          CertificateThumbprint
          AllowRemoteAccess = true
      
    • 查看winrm服务的配置:

      C:\WINDOWS\system32>winrm get winrm/config
      Config
          MaxEnvelopeSizekb = 500
          MaxTimeoutms = 60000
          MaxBatchItems = 32000
          MaxProviderRequests = 4294967295
          Client
              NetworkDelayms = 5000
              URLPrefix = wsman
              AllowUnencrypted = true
              Auth
                  Basic = true
                  Digest = true
                  Kerberos = true
                  Negotiate = true
                  Certificate = true
                  CredSSP = false
              DefaultPorts
                  HTTP = 5985
                  HTTPS = 5986
              TrustedHosts = *
          Service
              RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
              MaxConcurrentOperations = 4294967295
              MaxConcurrentOperationsPerUser = 1500
              EnumerationTimeoutms = 240000
              MaxConnections = 300
              MaxPacketRetrievalTimeSeconds = 120
              AllowUnencrypted = true
              Auth
                  Basic = true
                  Kerberos = true
                  Negotiate = true
                  Certificate = false
                  CredSSP = false
                  CbtHardeningLevel = Relaxed
              DefaultPorts
                  HTTP = 5985
                  HTTPS = 5986
              IPv4Filter = *
              IPv6Filter = *
              EnableCompatibilityHttpListener = false
              EnableCompatibilityHttpsListener = false
              CertificateThumbprint
              AllowRemoteAccess = true
          Winrs
              AllowRemoteShellAccess = true
              IdleTimeout = 7200000
              MaxConcurrentUsers = 2147483647
              MaxShellRunTime = 2147483647
              MaxProcessesPerShell = 2147483647
              MaxMemoryPerShellMB = 2147483647
              MaxShellsPerUser = 2147483647
      
  • 编写封装远程连接测试脚本test_winrm.py

    #!/usr/bin/env python3
    # _*_ coding: utf-8 _*_
    # Author:shichao
    # File: .py
    
    import winrm
    
    
    def winCMD(hostname, username, password, cmd):
        '''
        在 windows 下执行命令
        '''
        wintest = winrm.Session('http://' + hostname + ':5985/wsman', auth=(username, password))
        ret = wintest.run_cmd(cmd)
    
        return ret.std_out.decode()
    
    
    if __name__ == '__main__':
        result = winCMD("172.16.128.98", "admin", "sobey2021", "ipconfig")
        print(result)
    
    
  • 运行反馈后结果

    /Users/admin/virtualenvs/python_3.8_base/bin/python test_winrm.py
    
    Windows IP Configuration
    
    
    Ethernet adapter Ethernet0:
    
       Connection-specific DNS Suffix  . : 
       Link-local IPv6 Address . . . . . : fe80::5976:b173:5426:9980%10
       IPv4 Address. . . . . . . . . . . : 172.16.128.98
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : fe80::aab4:56ff:fec4:6a81%10
                                           172.16.128.1
    
    Ethernet adapter Ethernet1:
    
       Connection-specific DNS Suffix  . : 
       Link-local IPv6 Address . . . . . : fe80::8b57:92c0:d677:6851%16
       IPv4 Address. . . . . . . . . . . : 10.0.0.4
       Subnet Mask . . . . . . . . . . . : 255.255.0.0
       Default Gateway . . . . . . . . . : 10.0.0.1
    
    Ethernet adapter VMware Network Adapter VMnet1:
    
       Connection-specific DNS Suffix  . : 
       Link-local IPv6 Address . . . . . : fe80::3323:cbd2:b3ef:3e84%7
       IPv4 Address. . . . . . . . . . . : 192.168.18.1
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 
    
    Ethernet adapter VMware Network Adapter VMnet8:
    
       Connection-specific DNS Suffix  . : 
       Link-local IPv6 Address . . . . . : fe80::f5ab:4ac7:ee2e:9c20%8
       IPv4 Address. . . . . . . . . . . : 192.168.176.1
       Subnet Mask . . . . . . . . . . . : 255.255.255.0
       Default Gateway . . . . . . . . . : 
    

标签:fe80,false,python,WinRM,winrm,windows,Address,true,远程
From: https://www.cnblogs.com/scajy/p/17078298.html

相关文章

  • windows安装openssh实现paramiko文件上传功能
    windows安装openssh实现paramiko文件上传功能1.windows安装openssh实现paramiko文件上传功能需求分析:需要从python端本地将某个文件上传至远端windows服务器的某个指定......
  • python-实现远程windows机器上传文件和远程执行命令
    python-实现远程windows机器上传文件和远程执行命令1.python-实现远程windows机器上传文件和远程执行命令编写上传测试文件#!/usr/bin/envpython3#_*_coding:ut......
  • 配置远程仓库
    方式一:在项目pom.xml文件中配置:<repositories>    <repository>        <id>CustomCentralRepository</id>        <url>https://repo.maven.apach......
  • Python3.7采用CMD自动安装Pygame1.9.4
    ​​Python全栈工程师核心面试300问深入解析(2020版)----全文预览​​​​​​Python3.7采用CMD自动安装Pygame1.9.4,一步即可最近正在学习python开发游戏,需要安装Pygam......
  • Python 反爬虫——文本混淆反爬虫
    文中案例参考GitHub项目4文本混淆反爬虫4.1图片伪装为文字反爬虫有些文字内容实际是图片伪装的提取图片的内容(图片请求响应结果res.content就是图片的字节数据,可以直接......
  • Python 通用爬虫思路
    文章目录​​通用爬虫思路​​​​1.准备URL​​​​2.发送请求,获取响应​​​​3.提取数据​​​​4.保存​​通用爬虫思路1.准备URL准备start_urlurl地址规律不......
  • Windows10中macOS10.14虚拟机性能优化教程
    ​​Python全栈工程师核心面试300问深入解析(2020版)----全文预览​​Windows10中采用VMware15安装安装macOS10.14教程虚拟机中masOS运行并不是完美流畅,需要进行性能......
  • PYTHON基础
    PYTHON基础字面量指在代码中,被写下来的固定的值常用的值类型类型描述数字(Number)支持-整数(int)-浮点数(float)-复数(complex)(-复数:如4+3j,以j结尾......
  • Python-​​pprint的简单使用
    ​​Dataprettyprinter 一、简介​​​print()​和​​pprint()​都是python的打印模块,功能基本一样,唯一的区别就是​​pprint()​模块打印出来的数据结构更加完整,每......
  • 合宙 esp32c3 烧录 MicroPython
    首先安装USB串口驱动(win10以上会自动安装,CH343串口驱动)安装Python安装esptool->pipinstallesptool下载MicroPython固件固件地址名称为:esp32c3-20220618-v1.......