思路
通过winrm登录远程桌面,在cmd中执行pscp命令将Windows server中的文件传输至指定服务器中
pscp
在此下载并拷贝到远程服务器的路径下面中:
C:\Windows\System32
打开cmd,即可执行,将本地目录 C:\www\cloudconfig\App_Data\android-config.txt的文件传输至指定服务器的/home/ubuntu/tools/config路径中
D:\>pscp -P 22 -pw "******" C:\www\cloudconfig\App_Data\\android-config.txt ubuntu@198.2.***.***:/home/ubuntu/tools/config
winrm
安装
pip install pywinrm
配置
需要在windows server中上开启以下服务:
针对winrm service 进行基础配置:
winrm quickconfig
查看winrm service listener:
winrm e winrm/config/listener
为winrm service 配置auth:
winrm set winrm/config/service/auth @{Basic="true"}
为winrm service 配置加密方式为允许非加密:
winrm set winrm/config/service @{AllowUnencrypted="true"}
使用
// An highlighted block
import winrm
def send_conf():
wintest = winrm.Session('http://198.2.***.**:5985/wsman', auth=('administrator', '********'))
ret = wintest.run_cmd(
'pscp -P 22 -pw "******" C:\www\cloudconfig\App_Data\\android-config.txt ubuntu@198.2.***.***:/home/ubuntu/tools/config')
print(ret.std_out) # 打印获取到的信息
print(ret.std_err) # 打印错误信息
个人遇到的问题
在初始连接通以后,隔了一段时间测试时,发现出现无法连接的问题,经查询是被防火墙拦截了:
在windows server的防火墙中添加,并确保服务已启动。
PS C:\Windows\system32> netsh advfirewall firewall add rule name="WinRM-HTTP" dir=in localport=5985 protocol=TCP action=allow
PS C:\Windows\system32> Get-Service -ComputerName abc -Name winrm | Select Status
并在powerShell中,输入进行配置:
enable-psremoting
set-item WSMan:\localhost\Client\TrustedHosts * # ('*' is for all hosts, you may specify the host you want)