如何在 Windows 上使用 Python 访问远程文件系统? [英] How do I access a remote filesystem using Python on Windows?

查看:81
本文介绍了如何在 Windows 上使用 Python 访问远程文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Python 脚本来访问网络上的所有计算机,登录它们并读取一些日志文件.我不想使用像 socket 这样低级的东西,但我可以如果我必须.我意识到我的问题类似于 这个问题,但不一样.

I'm writing a Python script to access all computers on the network, log in to them and read some log files. I don't want to use something as low-level as socket, but I can if I must. I realize that my problem is similar to this question, but not the same.

是否有任何用于访问外部 Windows 机器的模块?以前有人做过这样的事情吗?

Are there any modules for accessing external Windows machines? Has anyone done anything like this before?

我特别想登录 Windows 7 机器,不是 unix.

I'm specifically looking to log into Windows 7 machines, not unix.

我们还假设我要登录的每台计算机都安装并启用了远程桌面.我也不担心网络安全或加密,因为这些文件不是机密的.Windows 机器默认没有安装 SSH,是吗?

Let's also assume that each computer I want to log into has Remote Desktop installed and enabled. I'm also not worried about network security or encryption because these files are not confidential. Windows machines don't have SSH installed on the by default do they?

推荐答案

另一边必须有一些东西可以让你交谈.这限制了您在每台机器上设置服务器"、安装真实服务器(即 sshd)、自己构建服务器"并安装它,或者使用操作系统的内置和活动功能.

There has to be something on the other side for you to talk to. This limits you to either setting up a "server" on each machine, installing a real server (i.e. sshd), building a "server" yourself and installing it, or using a built in and active feature of the OS.

基于此,您想在这些机器上设置什么样的系统?它需要做什么?只是读取预先指定的文件列表的内容?这份名单会改变吗?

Based upon this, what kind of system do you want to set up on these machines? What does it need to do? Just read the contents of a prespecified file list? Will that list change?

  • 一种解决方案是打开telnet,并使用paramiko或twisted来谈论它.这当然不是很安全
  • 接下来,设置一个 samba 共享,并远程访问该文件夹.这个也不安全,虽然不如 telnet
  • 如果你愿意,你可以找到一个 ssh 守护进程端口并运行它
  • 来自 sysinternals 的 Psexec 可能会起作用
  • 使用twisted 构建具有您需要的功能的服务器应用
  • 使用 ncat 侦听端口并生成 cmd 提示符

请注意,大多数远程访问 Windows 的解决方案都...很差.最好的解决方案可能是自己动手,但这是一项艰苦的工作,您可能会犯错误.

Be aware that most of the solutions for accessing windows remotely are... poor. The best solution is probably to roll your own, but that is hard work and you will probably make mistakes.

此外,Windows 7 完全是多用户友好的.单个进程可以作为单独的用户运行,但操作系统不支持多个用户同时登录.有人将成为用户",而其他所有人都只是一个拥有不同凭证集的进程.

Also, Windows 7 is not exactly multi-user friendly. Individual processes can run as separate users, but the OS does not support having multiple users logged in at the same time. Someone is going to be the "user" and everyone else is just a process with a different credential set.

这对 M$ 来说更像是一种人为的限制,而不是任何技术性的限制.要查看此操作,请尝试在用户本地登录时使用 RDP 登录.欢乐时光.

This is more an artificial limitation on M$'s part than anything technical. To see this in action, try to log in with RDP while a user is logged in locally. Fun times.

根据您的编辑,最简单的方法就是在盒子上设置一个 samba 共享.

Per your edit, the easiest thing to do is just set up a samba share on the box.

设置此共享后:

with open(r'\\myCompNameOrIP\C\windows\logs\logfile.txt','rb') as logfile:
    loglines = logfile.readlines()

或者您可以使用 gencat 示例/www.dabeaz.com/generators/" rel="nofollow">此处.只需将其 r'\\myCompNameOrIP\C\windows\logs\*.txt' 作为搜索路径并观看魔术.

Or you can use the gencat sample found here. Just give it r'\\myCompNameOrIP\C\windows\logs\*.txt' as the search path and watch the magic.

这篇关于如何在 Windows 上使用 Python 访问远程文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆