最近有需求需要从linux上对windows sever进行命令行调用。
windows server 2019环境配置
10.100.100.101
首先在windows sever上启用远程调用的功能。
打开powershell,输入Enable-PSRemoting -Force
如果远程服务器上开启了防火墙,您需要确保允许 PowerShell 的端口(默认为 TCP 5985 和 UDP 5985)
New-NetFirewallRule -Name "Allow PS Remoting" -DisplayName "Allow PowerShell Remoting" -Protocol TCP -LocalPort 5985 -Action Allow
centos 7环境配置
安装好winrm,目前centos 7直接安装的python版本是3.6,用于调用。
yum install -y epel-release
yum -y install python3
yum -y install python3-pip
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
pip3 install setuptools-rust -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install cryptography -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install pywinrm -i https://pypi.tuna.tsinghua.edu.cn/simple
测试winrm是否可用
import winrm
session = winrm.Session('http://10.100.100.101:5985', auth=('administrator', '123456'), transport='ntlm')
session.run_ps("route print").std_out
如果返回内容,就说明调用成功了。
如果是windows server想要调用centos上的shell
设置ssh无密码登录
ssh-keygen -t rsa -b 4096 -C "123456@qq.com"
Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa.
Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa.pub.
scp C:\Users\Administrator\.ssh\id_rsa.pub root@10.100.100.100:~/
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
cat id_rsa.pub >> ~/.ssh/authorized_keys
ssh root@10.100.100.100
ssh root@10.100.100.100 "cat /etc/passwd"