异步python winrm客户端
txwinrm的Python项目详细描述
在Zenoss,我们正在进行一个项目来提高可靠性和性能 以及本机windows监视的功能。这个项目的核心是 用于使用winrm和winrs异步管理窗口的python库 服务。然后,Zenoss将使用这个库进行自动发现 以及监视Zenoss用户习惯的方式和一些新的可能性。
现在我们正试图通过图书馆获得更多的真实世界体验 尽可能证明我们的可靠性和性能改进 希望实现。如果您可以访问Windows服务器,您可以提供帮助!它 甚至不需要安装Zenoss Core,因为这个工具是独立的 现在。
要使用kerberos连接,需要从https://github.com/zenoss/pykerberos下载并构建pythonkerberos模块。
查看Zenoss Windows论坛以获取项目的更新,然后 反馈。
http://community.zenoss.org/community/forums/zenoss-windows
安装
ubuntu服务器12.04.1 LTS(python 2.7.3)
sudo apt-get update sudo apt-get -y install gcc python-dev libkrb5-dev krb5-user python-setuptools # if prompted for your kerberos realm, leave it blank and choose OK sudo easy_install txwinrm # if you want to use a Windows domain sudo genkrb5conf <windows domain> <domain controller IP address> # now you can run the txwinrm commands (winrm, winrs, typeperf, and wecutil)
CentOS 6.3 x86_64(python 2.6.6)
# as root yum -y install gcc python-devel krb5-devel krb5-workstation python-setuptools easy_install txwinrm # if you want to use a Windows domain genkrb5conf <windows domain> <domain controller IP address> # now you can run the txwinrm commands (winrm, winrs, typeperf, and wecutil) as a normal user
Zenoss Core或Resource Manager 4.2.x安装在CentOS 6上(Zenoss Python 2.7)
# as root yum -y install gcc krb5-devel krb5-workstation # as the zenoss user easy_install txwinrm # as root (if you want to use a Windows domain) genkrb5conf <windows domain> <domain controller IP address> # now you can run the txwinrm commands (winrm, winrs, typeperf, and wecutil) as the zenoss user
当前功能支持
- http
- 基本身份验证
- WQL查询
- Winrs
- 类型perf
- 订阅Windows事件日志
- kerberos身份验证(域帐户)
- 有效负载加密(kerberos连接)
未来功能支持
- https
- NTlm身份验证(本地帐户)
配置目标Windows计算机
您可以在Windows Server 2003、2008和2012上启用WinRM服务。跑步 命令提示符作为管理员并执行以下命令
winrm quickconfig winrm s winrm/config/service @{AllowUnencrypted="true";MaxConcurrentOperationsPerUser="4294967295"} winrm s winrm/config/service/auth @{Basic="true"} winrm s winrm/config/winrs @{MaxShellsPerUser="2147483647"}
WQL查询
您可以通过命令行向单个主机传递查询…
$ winrm -r host -u user -f "select * from Win32_NetworkAdapter"
另一个选项是创建一个ini样式的配置文件并命中多个目标 有多个查询。示例配置位于examples/config.ini
$ winrm -c path/to/config.ini
这将向config.ini中列出的主机发送winrm枚举请求。它 将为该文件中列出的每个wql查询发送请求。输出将 看起来像
<hostname> ==> <WQL query> <property-name> = <value> ... ---- (indicates start of next item) <property-name> = <value> ... ...
下面是一个示例…
cupertino ==> Select name,caption,pathName,serviceType,startMode,startName,state From Win32_Service Caption = Application Experience Name = AeLookupSvc PathName = C:\Windows\system32\svchost.exe -k netsvcs ServiceType = Share Process StartMode = Manual StartName = localSystem State = Stopped ---- Caption = Application Layer Gateway Service Name = ALG ...
失败次数(如果有)和处理的XML元素数的摘要 出现在结尾。摘要和任何错误都会写入stderr,因此 如果需要简洁的输出,请将stdin重定向到/dev/null。
$ winrm -c path/to/config.ini >/dev/null Summary: Connected to 3 of 3 hosts Processed 13975 elements Failed to process 0 responses Peak virtual memory useage: 529060 kB Remote CPU utilization: campbell 0.00% of CPU time used by WmiPrvSE process with pid 1544 4.00% of CPU time used by WmiPrvSE#1 process with pid 1684 4.00% of CPU time used by WmiPrvSE#2 process with pid 3048 cupertino 0.00% of CPU time used by WmiPrvSE process with pid 1608 3.12% of CPU time used by WmiPrvSE#1 process with pid 1764 9.38% of CPU time used by WmiPrvSE#2 process with pid 2608 gilroy 1.08% of CPU time used by WmiPrvSE process with pid 1428 5.38% of CPU time used by WmiPrvSE#1 process with pid 1760 4.30% of CPU time used by WmiPrvSE#2 process with pid 1268
“-a”选项指定身份验证方法。当前支持的值 是“基本”和“kerberos”。“基本”是默认值。
'-d'选项增加了日志记录,打印出所有请求的xml 响应,以及http状态代码。
Winrs
WinRS程序有四种操作模式:
- 交互式(默认):在交互式命令中执行许多命令 在远程主机上提示
- single:执行单个命令并返回其输出
- long:执行一个长时间运行的命令,比如 “typeperf-si 1”并定期检查输出
- 批处理:在远程系统上打开命令提示符,然后 执行命令列表(实际上现在它执行一个 两倍于概念证明的命令)
交互式模式的示例
$ winrs interactive -u Administrator -x 'typeperf "\Memory\Pages/sec" "\PhysicalDisk(_Total)\Avg. Disk Queue Length" "\Processor(_Total)\% Processor Time" -si 1' -r oakland Microsoft Windows [Version 6.2.9200] (c) 2012 Microsoft Corporation. All rights reserved. C:\Users\Default>dir Volume in drive C has no label. Volume Serial Number is 5E71-6BA3 Directory of C:\Users\Default 02/22/2013 03:42 AM <DIR> Contacts 02/22/2013 03:42 AM <DIR> Desktop 02/22/2013 03:42 AM <DIR> Documents 02/22/2013 03:42 AM <DIR> Downloads 02/22/2013 03:42 AM <DIR> Favorites 02/22/2013 03:42 AM <DIR> Links 02/22/2013 03:42 AM <DIR> Music 02/22/2013 03:42 AM <DIR> Pictures 02/22/2013 03:42 AM <DIR> Saved Games 02/22/2013 03:42 AM <DIR> Searches 02/22/2013 03:42 AM <DIR> Videos 0 File(s) 0 bytes 11 Dir(s) 7,905,038,336 bytes free C:\Users\Default>exit
单模式示例
$ winrs single -u Administrator -x 'typeperf "\Memory\Pages/sec" "\PhysicalDisk(_Total)\Avg. Disk Queue Length" "\Processor(_Total)\% Processor Time" -sc 1' -r oakland {'exit_code': 0, 'stderr': [], 'stdout': ['"(PDH-CSV 4.0)","\\\\AMAZONA-SDFU7B1\\Memory\\Pages/sec","\\\\AMAZONA-SDFU7B1\\PhysicalDisk(_Total)\\Avg. Disk Queue Length","\\\\AMAZONA-SDFU7B1\\Processor(_Total)\\% Processor Time"', '"04/19/2013 21:43:48.823","0.000000","0.000000","0.005660"', 'Exiting, please wait...', 'The command completed successfully.']}
长模式示例
$ winrs long -u Administrator -x 'typeperf "\Memory\Pages/sec" "\PhysicalDisk(_Total)\Avg. Disk Queue Length" "\Processor(_Total)\% Processor Time" -si 1' -r oakland "(PDH-CSV 4.0)","\\AMAZONA-SDFU7B1\Memory\Pages/sec","\\AMAZONA-SDFU7B1\PhysicalDisk(_Total)\Avg. Disk Queue Length","\\AMAZONA-SDFU7B1\Processor(_Total)\% Processor Time" "04/19/2013 21:43:10.603","0.000000","0.000000","18.462005" "04/19/2013 21:43:11.617","0.000000","0.000000","0.000464" "04/19/2013 21:43:12.631","0.000000","0.000000","1.538423" "04/19/2013 21:43:13.645","0.000000","0.000000","0.000197"
批处理示例
$ winrs batch -u Administrator -x 'typeperf "\Memory\Pages/sec" "\PhysicalDisk(_Total)\Avg. Disk Queue Length" "\Processor(_Total)\% Processor Time" -sc 1' -r oakland Creating shell on oakland. Sending to oakland: typeperf "\Memory\Pages/sec" "\PhysicalDisk(_Total)\Avg. Disk Queue Length" "\Processor(_Total)\% Processor Time" -sc 1 Received from oakland: "(PDH-CSV 4.0)","\\AMAZONA-SDFU7B1\Memory\Pages/sec","\\AMAZONA-SDFU7B1\PhysicalDisk(_Total)\Avg. Disk Queue Length","\\AMAZONA-SDFU7B1\Processor(_Total)\% Processor Time" "04/19/2013 21:43:39.198","0.000000","0.000000","0.000483" Exiting, please wait... The command completed successfully. Sending to oakland: typeperf "\Memory\Pages/sec" "\PhysicalDisk(_Total)\Avg. Disk Queue Length" "\Processor(_Total)\% Processor Time" -sc 1 Received from oakland: "(PDH-CSV 4.0)","\\AMAZONA-SDFU7B1\Memory\Pages/sec","\\AMAZONA-SDFU7B1\PhysicalDisk(_Total)\Avg. Disk Queue Length","\\AMAZONA-SDFU7B1\Processor(_Total)\% Processor Time" "04/19/2013 21:43:41.054","0.000000","0.000000","0.000700" Exiting, please wait... The command completed successfully. Deleted shell on oakland. Exit code of shell on oakland: 0
类型性能
txwinrm的typeperf命令允许您运行远程typeperf命令,检查 定期输出,解析它,并将其打印到stdout。它支持-si 选项和多个计数器。下面是一个示例:
$ typeperf -r gilroy -u Administrator '\Processor(_Total)\% Processor Time' '\memory\Available Bytes' '\paging file(_Total)\% Usage' \memory\Available Bytes 00:54:27: 193130496.0 \paging file(_Total)\% Usage 00:54:27: 0.012207 \Processor(_Total)\% Processor Time 00:54:27: 0.004487 \memory\Available Bytes 00:54:28: 193216512.0 00:54:29: 193982464.0 \paging file(_Total)\% Usage 00:54:28: 0.012207 00:54:29: 0.012207 \Processor(_Total)\% Processor Time 00:54:28: 1.542879 00:54:29: 0.004487 \memory\Available Bytes 00:54:30: 193933312.0 00:54:31: 193941504.0 \paging file(_Total)\% Usage 00:54:30: 0.012207
订阅Windows事件日志
以下命令显示订阅Windows事件日志的示例:
$ wecutil -r saratoga -u Administrator Pull #1 Event(system=System(provider='Microsoft-Windows-EventForwarder', event_id=111, event_id_qualifiers=None, level=None, task=None, keywords=None, time_created=datetime.datetime(2013, 5, 8, 20, 29, 31, 132000), event_record_id=None, channel=None, computer='saratoga.solutions.loc', user_id=None), data=None, rendering_info=None) Pull #2
您可以使用配置文件对主机和事件查询矩阵运行wecutil。
$ wecutil -c examples/config.ini milpitas System/'*' pull #1 of 2 milpitas Application/'*' pull #1 of 2 gilroy System/'*' pull #1 of 2 ... milpitas System/'*' Event(system=System(provider='Microsoft-Windows-... ... milpitas Application/'*' pull #2 of 2 ... Summary: Connected to 4 of 4 hosts Processed 12 events Peak virtual memory useage: 361060 kB Remote CPU utilization: saratoga 0.15% of CPU time used by WmiPrvSE process with pid 1640 0.96% of CPU time used by WmiPrvSE#1 process with pid 2000 0.00% of CPU time used by WmiApSrv process with pid 604 0.07% of CPU time used by WmiPrvSE#2 process with pid 1604 gilroy 0.00% of CPU time used by WmiPrvSE process with pid 1384 0.00% of CPU time used by WmiPrvSE#1 process with pid 1684 0.00% of CPU time used by WmiApSrv process with pid 1924 0.15% of CPU time used by WmiPrvSE#2 process with pid 1348 milpitas 0.36% of CPU time used by wmiprvse process with pid 1924 1.01% of CPU time used by wmiprvse process with pid 816 berkeley 0.00% of CPU time used by WmiPrvSE process with pid 1624 0.00% of CPU time used by WmiPrvSE#1 process with pid 1744 0.00% of CPU time used by WmiApSrv process with pid 1620 0.07% of CPU time used by WmiPrvSE#2 process with pid 1280
反馈
要提供有关txwinrm的反馈,请在zenoss windows论坛上开始讨论 在community.zenoss.org上: http://community.zenoss.org/community/forums/zenoss-windows
泽诺斯用吉拉追踪虫子。创建帐户并提交错误,或浏览 报告的错误:http://jira.zenoss.com/jira/secure/Dashboard.jspa
单元测试覆盖率
截至2013年4月16日……
$ txwinrm/test/cover ........................ ---------------------------------------------------------------------- Ran 24 tests in 7.910s OK Name Stmts Miss Cover --------------------------------------- txwinrm/__init__ 0 0 100% txwinrm/constants 18 0 100% txwinrm/enumerate 259 46 82% txwinrm/shell 114 34 70% txwinrm/util 89 24 73% --------------------------------------- TOTAL 480 104 78%
开发
在合并到master之前运行txwinrm/test/precommit。这需要您…
easy_install flake8 easy_install coverage git clone https://github.com/dgladkov/cyclic_complexity