V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tn84
V2EX  ›  Python

请教: Python 怎样远程查看 windows 目录下的日志文件?

  •  
  •   tn84 · 2018-03-06 14:06:14 +08:00 · 3740 次点击
    这是一个创建于 2214 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好,最近需要写一个 windows 的巡检脚本 ,想用 python 远程批量查看 windows 下的日志。请问怎样实现? 查过 wmi 只能查看进程信息,不能操作目录文件。 也不想用 powershell . 请问大家有好的办法吗?一起讨论一下

    8 条回复    2020-04-13 16:29:20 +08:00
    w88975
        1
    w88975  
       2018-03-06 15:47:11 +08:00
    fabric
    tn84
        2
    tn84  
    OP
       2018-03-06 15:50:59 +08:00
    谢谢您的回复。 但 fabric 不能远程登录并操作 windows 吧? 我知道用它操作 linux 是完全没问题
    21212121
        3
    21212121  
       2018-03-06 16:04:08 +08:00
    使用客户端的方式呗,客户端使用 wind 的 API 写功能
    ClutchBear
        4
    ClutchBear  
       2018-03-06 16:05:03 +08:00
    ansible?
    Zeitgeber
        5
    Zeitgeber  
       2018-03-06 16:16:16 +08:00
    像楼上说的用 ansible 和 fabric 之类的比较简单能实现吧。它们会和 powershell 通讯,通过相应模块完成你的需求。
    tn84
        6
    tn84  
    OP
       2018-03-06 16:30:47 +08:00
    谢谢大家的回复。 我研究一下
    tn84
        7
    tn84  
    OP
       2018-03-07 14:23:07 +08:00   ❤️ 1
    谢谢大家的回复哈,我找到办法解决了。 用 winrm 模块就可以了。方法如下:
    1.安装 pip pywinrm
    2.import winrm
    然后:
    winServer = winrm.Session('http://192.168.1.168:5985/wsman',auth=('administrator',xxx')) #url 地址就是服务器地址,auth 里面的就是系统的账号和密码
    checkLog=winServer.run_cmd('type D:\monitorServer\error.log|find /i /c "error") #括号里面就是要执行的 dos 命令。

    用法非常简单, windows server 一般默认都会开启 winrm 服务,如果有开启防火墙记得把 5985 端口开放一下。 这模块远程操控 windows 非常方便。。 更多用法大家百度 winrm 就可以了。 如有疑问,请留言一起探讨
    datty258
        8
    datty258  
       2020-04-13 16:29:20 +08:00
    @tn84 winrm 的方式需要先在 windows 机器上开启 winrm 服务。 有些 zf 或者医院的 windows 设备没法开启服务还有别的方式连接吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5319 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:46 · PVG 13:46 · LAX 22:46 · JFK 01:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.