zoukankan      html  css  js  c++  java
  • python应用-使用python控制win2003服务器

    经调研和测试,服务端可通过ansible控制各linux服务器(容器),进行各类操作,且支持远程控制windows服务器,但windows操作系统中,需安装.net及powershell3.0及以上版本。

    高频应用为win2003 暂无法安装powershell 3.0及以上版本,无法通过ansible远程控制。

    win2008及以上操作系统可以。

    经测试可通过使用python 利用winrm远程控制win2003。

    因此结合自动化运维工具ansible(也是基于python)和python脚本,应可实现测试环境所有虚拟机的远程控制,请各位探索学习和实践,组内发布实践成果。

     

    0、ubuntu安装ansible

    可使用任一台docker-pc服务器进行试验(pc2及之后),linux客户端无需任何操作。

    或使用任意linux虚拟机,安装ansible工具即可。

    ansible安装参考http://www.ansible.com.cn/docs/intro_installation.html

    ubuntu安装:

    Ubuntu 编译版可在PPA中获得: ` <https://launchpad.net/~ansible/+archive/ansible>`_.

    配置PPA及安装ansible,执行如下命令:

    $ sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install ansible

    1、win2003需进行的操作为(使用python控制或ansible控制均需)

    安装Microsoft_.NET_Framework_3.5_Service_Pack_1简体中文安装版.exe

    安装powershell2.0 WindowsServer2003-KB968930-x86-ENG.exe (需.net>2.1 当前2.0)

    powershell执行(win2008及之上仅需在powershell中执行)

    set-executionpolicy remotesigned

    get-host

    winrm enumerate winrm/config/listener

    winrm quickconfig

    winrm e winrm/config/listener

    winrm set winrm/config/service/auth '@{Basic="true"}'

    winrm set winrm/config/service '@{AllowUnencrypted="true"}'

    netstat -ano | findstr "5985"

     

    2、调时间相关指令:

    具体步骤

    net stop w32time

    w32tm /unregister

    w32tm /register

    net start w32time

    w32tm /config /manualpeerlist:172.26.41.241 /syncfromflags:manual /update

    w32tm /resync

     

    3、控制端python脚本

    安装包

     

    使用Python的pip安装pywinrm及kerberos(可暂不安装)

    apt-get install python-pip

    pip install pywinrm==0.1.1

     

    远程控制win2003同步时间

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Author : "David J"
    import winrm
    import sys
    import argparse
    import httplib2
    host="172.26.41.36"
    ntpserver="172.26.41.241"
    username="administrator"
    passwd="..."
    s = winrm.Session('http://'+host+':5985/wsman',auth=('administrator','UatTester'))
    def _runCommand(comm):
        if(comm == "q"):
            sys.exit()
        r = s.run_cmd(comm)
        print r.std_out
    while 1:
        cmd1='w32tm /register'
        cmd2='net start w32time'
        cmd3='w32tm /config /manualpeerlist:'+ntpserver+' /syncfromflags:manual /update'
        cmd4='w32tm /resync'
        cmd5="date /t && time /t"
        _runCommand(cmd1)
        _runCommand(cmd2)
        _runCommand(cmd3)
        _runCommand(cmd4)
        _runCommand(cmd5)
        # _runCommand(date 	')
        _runCommand("q")
    

      

  • 相关阅读:
    Json字串转换成Java复杂对象
    [Code Snipper]图片轮换
    将CSDN600W用户及密码帐号存入本地MySql数据库
    【转】一个隐形的java int溢出
    【转】展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
    如何在Android 4.0 ICS中禁用StatusBar | SystemBar | 状态栏 【完美版】
    【转】提问的智慧(How To Ask Questions the Smart)
    商业开发实战之VB篇精彩视频
    我的设计原语
    RAPIDXML 中文手册,根据官方文档完整翻译!
  • 原文地址:https://www.cnblogs.com/DaweiJ/p/8463315.html
Copyright © 2011-2022 走看看