linux远程登录windows执行bat脚本

参考来自:http://aodi.paic.com.cn/forum.php?mod=viewthread&tid=4306


总的来说,可以利用ansible远程调用windows,也可以用这个pywinrm开源框架,两者都是利用了windows的winrm服务的,但是具体实现细节,还未知!


1 windows环境搭建

    1.1 需要开启5986这个端口(ansible利用5986这个端口与windows通信, 在linux做到"telnet ip 5986"正常 即可)

    1.2 windows服务器需要powershell的版本为3或者以上,版本查看命令:$PSVersionTable.PSVersion.Major。将powershell策略置为remotesigned,查看当前策略: get-executionpolicy 更改策略:set-executionpolicy remotesigned 

    1.3 开启WRM(Windows Remote Management)服务,可以运行附件ConfigureRemotingForAnsible.zip里的ps脚本(不懂这个脚本,抄过来的,建议管理员权限下执行)来配置。 之后再执行以下命令:

winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;
winrm enumerate winrm/config/listener
 
针对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"}'

如果以上操作都正常。现在最终还是在Linux来测试方可判断。在Linux中使用ansible windows -m win_ping 如果成功,则才能说明windows环境已经配好。


2 linux 搭建ansible环境需要

 目前发现ansible2.2是缺少winrm_shell这中模块的(故抛弃)。ansible2.4可以实现。

 安装ansible:

 yum源有six这个python包,直接就用这个了: yum -y install python-six
 一些非Python的依赖:yum -y install python-devel krb5-devel krb5-libs krb5-workstation

 ansible包及其依赖见附件

安装好依赖后,首先看是否能import winrm,如果没有,则表示没有安装成功

ansible装好了,可是报错:

[root@SHB-L0088006 ansible]# ansible-playbook winrm.yml -i hosts
ERROR! Unexpected Exception, this is probably a bug: 'module' object has no attribute 'HTTPSHandler'

于是深究下去,最后定位出一个问题:

20180312321700501299.png


后来才发现,安装Python(我用的是Python2.7.11)的时候,就已经提示了缺少_ssl模块,关于oracle linux安装Python的确网上会提示这个概念。现在import ssl就已经出错了,我估计是这个问题,后来网上各种搜也找不到解决办法!后来是找到另外一台Oracle Linux,它能import ssl ,于是把它的Python(2.7.9)安装目录打了个包,附件有,转移到该台环境,然后奇迹般的的实现了import ssl。至于为什么?能力有限,无法解释其原因!


 安装好ansible后,需要编写hosts和yml文件:

[root@SZB-L0023010 ansible]# ls
hosts  winrm.yml

查看hosts和winrm.yml文件如下:

[root@SZB-L0023010 ansible]# cat hosts
[windows]
#30.4.91.100
10.25.80.196
[windows:vars]
ansible_connection=winrm
#ansible_user=SHB-W0066163\\[email protected]
ansible_user=administrator
ansible_ssh_pass=****
ansible_ssh_port=5986
ansible_winrm_server_cert_validation=ignore
ansible_connection=winrm
ansible_winrm_transport=ntlm
[root@SZB-L0023010 ansible]# cat winrm.yml 
- name: save
  hosts: windows
  tasks:
     - name: run a shell 
       #script: C:\Users\Administrator\Desktop\demo.bat
       #register: out
       win_command: C:\Users\Administrator\Desktop\demo.bat >> C:\Users\Administrator\Desktop\temp.sh
       register: whoami_out

其中为啥transport不是kerberos?
winrm文档显示kerberos的认证方式似乎需要client跟server在同一个域,果断用NTLM的方式。。

20180308591058483013.png

上述是抄自谭毅的。我也查过pywirnm开源软件中也提到这个概念,但是我觉得其根本原因还需要去追究源码!

 执行命令:

[root@SZB-L0023010 ansible]# ansible-playbook winrm.yml -i hosts
PLAY [save] *********************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [10.25.80.196]
TASK [run a shell] **************************************************************************************************************************************************************************
changed: [10.25.80.196]
PLAY RECAP **********************************************************************************************************************************************************************************
10.25.80.196               : ok=2    changed=1    unreachable=0    failed=0


这个时候,会发现已经执行了windows中的bat脚本,并且其结果保存在temp.sh脚本中


20180308521030549578.png


3  由于测试环境是Oracle Linux,其在安装Python2.7的时候,会提示你的_ssl模块没有安装,可是在ansible中会用到urllib2这个模块,导致报错!这个问题,我始终没有解决掉!不过,后来发现pywinrm却可以实现(注意,它依赖windows端口5985,所以windows机器5985端口需要开启),后来深入两者比较发现,pywinrm利用的是requests这个模块,而ansible利用的是urllib2模块。至于差异,我不是很清楚。

pywinrm所依赖包如附件

代码如下:

[root@SHB-L0088006 dispatch]# cat demo.py 
import winrm

s=winrm.Session('30.4.91.100', auth=('administrator','******'))
try:
        r=s.run_cmd('Desktop\demo.bat')
except Exception,e:
        print e
print r.std_out
[root@SHB-L0088006 dispatch]# python demo.py 

C:\Users\Administrator>echo hello world 
hello world

[root@SHB-L0088006 dispatch]#



附件如下: 这个博客有bug。

20180308121018777847.zip    ConfigureRemotingForAnsible.zip    

20180312351429935020.zip    ansible.zip 

20180313331426341682.zip  pywinrm_dependencies.zip          

20180314551315623670.gz   Python_can_import_ssl.zip



猜你喜欢

转载自blog.csdn.net/feixiaohuijava/article/details/79567773
今日推荐