winrm 설정과 pywinrm을 이용한 테스트 코드 실행.
* 작업 환경.
winrm은 windows10에 설정.
pywinrm은 ubuntu 18.04, python 3.6.9에서 실행.
1. 윈도우 네트워크 프로필 설정.
네트워크프로필을 개인네트워크로 설정한다. 공용네트워크로 설정되어 있으면 quick config가 진행되지 않는다.
시작 -> 네트워크 상태 -> 이더넷 -> 네트워크(연결됨) -> 네트워크 프로필 -> 개인
2. winrm 설정
설정은 파워쉘이나, 명령프롬프트에서 모두 가능하다. 아래는 명령프롬프트에서 실행했다. 파워쉘의 경우는 오류가 발생할 수 있다.
명령프롬프트 (관리자권한) 실행 후 아래 명령 실행.
C:\WINDOWS\system32>winrm qc 이 컴퓨터에서 요청을 수신하도록 WinRM이 설정되지 않았습니다. 다음 사항을 변경해야 합니다. WinRM 서비스를 시작하십시오. 자동 시작을 지연하려면 WinRM 서비스 유형을 설정하십시오. 변경하시겠습니까[y/n]? y 요청을 수신하도록 WinRM이 업데이트되었습니다. WinRM 서비스 종류가 변경되었습니다. WinRM 서비스를 시작했습니다. WinRM이 이 컴퓨터에 관리를 위해 원격으로 액세스할 수 있도록 설정되지 않았습니다. 다음 사항을 변경해야 합니다. WinRM 방화벽 예외를 사용합니다. 로컬 사용자에게 원격으로 관리자 권한을 부여하도록 LocalAccountTokenFilterPolicy를 구성합니다. 변경하시겠습니까[y/n]? y 원격 관리를 위한 WinRM이 업데이트되었습니다. WinRM 방화벽 예외를 사용합니다. 로컬 사용자에게 원격으로 관리자 권한을 부여하도록 LocalAccountTokenFilterPolicy를 구성했습니다.
3. winrm 설정 보기
C:\WINDOWS\system32>winrm get winrm/config
Config
    MaxEnvelopeSizekb = 500
    MaxTimeoutms = 60000
    MaxBatchItems = 32000
    MaxProviderRequests = 4294967295
    Client
        NetworkDelayms = 5000
        URLPrefix = wsman
        AllowUnencrypted = false
        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 = false
        Auth
            Basic = false
            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
4. 몇 가지 설정을 변경한다.
* 인증방식 설정
C:\WINDOWS\system32>winrm set winrm/config/service/Auth @{Basic="true"}
Auth
    Basic = true
    Kerberos = true
    Negotiate = true
    Certificate = false
    CredSSP = false
    CbtHardeningLevel = Relaxed
* 암호화되지 않은 비밀번호 허용
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
* 쉘당 최대 메모리 설정.
C:\WINDOWS\system32>winrm set winrm/config/winrs @{MaxMemoryPerShellMB="1024"}
Winrs
    AllowRemoteShellAccess = true
    IdleTimeout = 7200000
    MaxConcurrentUsers = 2147483647
    MaxShellRunTime = 2147483647
    MaxProcessesPerShell = 2147483647
    MaxMemoryPerShellMB = 1024
    MaxShellsPerUser = 2147483647
* winrm (windows remote management) 서비스 확인

5. pywinrm 스크립트를 작성하여 윈도우 PC의 IP 주소 확인하는 명령 실행. (pywinrm 페이지에서 가장 기본적인 예제 스크립트)
리눅스에서 파이썬 스크립트로 winrm 연결 확인.
(PythonTest) snowfox@fox:~/code$ cat remote.py
import winrm
s = winrm.Session('http://172.30.192.200:5985', auth=('snowfox', 'xyzxyz'))
r = s.run_cmd('ipconfig', ['/all'])
print(r.std_err)
print(r.std_out)
실행결과는 아래와 같다.
STATUS: 0 b'\r\nWindows IP Configuration\r\n\r\n Host Name . . . . . . . . . . . . : DESKTOP-7AJ21VP\r\n Primary Dns Suffix . . . . . . . : \r\n Node Type . . . . . . . . . . . . : Hybrid\r\n IP Routing Enabled. . . . . . . . : No\r\n WINS Proxy Enabled. . . . . . . . : No\r\n\r\nEthernet adapter \xc0\xcc\xb4\xf5\xb3\xdd:\r\n\r\n Connection-specific DNS Suffix . : \r\n Description . . . . . . . . . . . : Intel(R) 82579V Gigabit Network Connection\r\n Physical Address. . . . . . . . . : E8-03-9A-6E-03-F2\r\n DHCP Enabled. . . . . . . . . . . : No\r\n Autoconfiguration Enabled . . . . : Yes\r\n IPv4 Address. . . . . . . . . . . : 172.30.192.200(Preferred) \r\n Subnet Mask . . . . . . . . . . . : 255.255.255.0\r\n Default Gateway . . . . . . . . . : 172.30.192.1\r\n DNS Servers . . . . . . . . . . . : 172.30.96.21\r\n 168.126.63.1\r\n NetBIOS over Tcpip. . . . . . . . : Enabled\r\n'
* 참고문서
https://pypi.org/project/pywinrm2/
https://learn.microsoft.com/en-us/windows/win32/winrm/portal
https://kochuns.blogspot.com/2018/01/powershell-remote.html
 
                
                                                                



