最近遇到项目需要使用windows 服务器,之前分开部署,先需求linux连接windows,使用winrm即可
一、配置windows winrm
1.运行如下,如果没有返回,则没有开启winrm
PS C:\Users\Administrator> winrm enumerate winrm/config/listener
2.配置winrm基础配置
PS C:\Users\Administrator> winrm quickconfig已在此计算机上运行 WinRM 服务。WinRM 没有设置成为了管理此计算机而允许对其进行远程访问。必须进行以下更改:在 HTTP://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。配置 LocalAccountTokenFilterPolicy 以远程向本地用户授予管理权限。执行这些更改吗[y/n]? yWinRM 已经进行了更新,以用于远程管理。在 HTTP://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。已配置 LocalAccountTokenFilterPolicy 以远程向本地用户授予管理权限。
3. 查看windows的winrm service listener
PS C:\Users\Administrator> winrm e winrm/config/listenerListener Address = * Transport = HTTP Port = 5985 Hostname Enabled = true URLPrefix = wsman CertificateThumbprint ListeningOn = 127.0.0.1, 172.18.232.248, ::1, fe80::5efe:172.18.232.248%12, fe80::4963:ce66:e004:e54%17
4.配置winrm auth
PS C:\Users\Administrator> winrm set winrm/config/service/auth '@{Basic="true"}'Auth Basic = true Kerberos = true Negotiate = true Certificate = false CredSSP = false CbtHardeningLevel = Relaxed
5.配置winrm service 加密方式为允许非加密
PS C:\Users\Administrator> 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
二、在linux连接
1.python 脚
#!/usr/bin/pythonimport winrmwin2008 = winrm.Session('http://11.22.33.44:5985/wsman',auth=('administrator','xxxx'))r = win2008.run_cmd('cd .. & dir')print r.std_out.decode()print r.std_err
2.运行
# python winP.py Volume in drive C has no label. Volume Serial Number is 50E1-E273 Directory of C:\Users2018/07/10 10:10.2018/07/10 10:10 ..2018/07/06 08:57 Administrator2009/07/14 12:57 Public2018/07/11 18:21 seedeer 0 File(s) 0 bytes 5 Dir(s) 252,649,209,856 bytes free