使用python控制exe_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工具即可。

ubuntu安装:

配置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 \t')

_runCommand("q")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值