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 \t')
_runCommand("q")
最新文章
- urlMappings与URL映射
- 【解决方案】VS2013外部工具中添加ildasm.exe
- Python核心编程--学习笔记--6--序列(下)列表、元组
- android开发 自定义图文混排控件
- Sea.js入门
- __I、__O 、__IO volatile是什么?怎么用? .
- Block 进阶
- user-modify属性,让html标签可以编辑
- 浏览器输入url回车后,会发生什么?
- javaIO缓冲区
- bootstrap 警告框多个删除
- (原创)c++中的类型擦除
- FPGA千兆位收发器选择指南
- How to return plain text from AWS Lambda &; API Gateway
- git忽略已添加版本控制的文件
- 《Lua程序设计》9.2 管道(pipe)与过滤器(filter) 包含使用协同函数实现“生产者——消费者”问题的实例代码
- NSScanner
- 04-Tomcat体系结构与插件配置
- 通过代码注册COM、DLL组件
- BZOJ2120 数颜色 【带修莫队】
热门文章
- 介绍axios和promise
- 使用在react hooks+antd ListView简单实现移动端长列表功能
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A	 Math Problem
- ping和tracert
- mybatis-plus QueryWrapper自定义查询条件
- 己亥清爽恢复系列之数据文件4篇:DROP表后如何恢复(非闪回技术)
- SpringBoot集成flowable碰见DMN不能初始化
- 第四篇,JavaScript面试题汇总
- mssql 数据库 基本知识
- Mysql使用事务