Python os.walk on a different computer on the same network - python

I would like to do an os.walk in python 2.7 in windows 7 but on a computer that is on the same network. Something like os.walk('\192.168.0.2') but this doesn't work. Is this possible?

Try net use or a UNC path.
Or try Linux, you'd probably be better off with it than Windows.
http://pcsupport.about.com/od/commandlinereference/p/net-use-command.htm
http://www.uwplatt.edu/oit/terms/uncpath.html
In python, using Windows paths is often easier if you use, for example, r'\\hostname\share' rather than '\\\\hostname\\share'.

My thought is also to go for Linux, or at least, Windows with SSH service.
Set up public key authentication over SSH, and make sure you can ssh to that Windows machine (with SSH service on, of course) without password and password phrase.
Make use of Fabric, a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

For Windows, what I did was opened File Explorer, and navigated to the (sub)folder of interest on the network drive, and right clicked on Properties. Copy the Location information, and paste that into your Python file.
Ex.
for root, dirs, files in os.walk("//<network name>/<folder>/<sub-folder1>/<sub-folder2>/.../<sub-folderN>"):
for file in files:
print(file)

Related

Python ssh program not working due to different path separator on linux and windows

I am using MacOS to develop a Python2.7 application that uses paramiko to connect to a remote linux machine. Now, I gave my early code to a Microsoft Windows user to test, and immediately ran into the problem that os.path.join by default uses \ as a separator in the path, when the application calls a command to be executed on the remote machine. So, I have to make sure to use the correct separator when doing operations on linux, while using the default separator on the local machine.
I'm thinking the easiest solution here is to just define my own joinpath function that always uses /as separator, and just call this function whenever the operation is being done remotely.
def joinpath(*args):
return "/".join(args)
Or is there a better and more general of dealing with such a situation?
when the application is operating on the linux machine
This sentence basically means that Python is running on Linux. While this:
This is being called from a Windows computer, but the sftp client opens a file on a remote linux machine
was actually what this is all about.
So:
os module operates on your OS (where the Python is running), giving you OS-specific and correct results
SSH (including SFTP) servers do not provide what CLI they use, that includes separators. They just give you a common interface to connect (and in case of SFTP, handle file transfer) but the rest is basically your problem. - I've dealt with network devices over ssh using Python myself, I had to e.g. manually deal with recognising errors etc because the output is text only.
That means, if you know that you'll be connecting to Linux machines only, use that join of yours (probably add a bit more, in case you have a dir path that ends in "/" - in your case, this will result in "//" in the path).
If you will connect both to Windows and Linux, do a fallback - catch the error (or manually find it in the output, I haven't worked with paramiko so I don't know how it dealt with it), and use another separator as a fallback and remember it for that session.

Copy files from a remote windows virtual machine

I want to do a python script that is able to copy log files from a remote windows 10 virtual machine to the script's machine (Windows) as well as deleting files. A developer in my work place uses WMI with C# to do these kind of stuff but I haven't been able to find anything for Python regarding this topic.
You can use SSH for that.
Paramiko is an awesome library that can run SSH in python: http://www.paramiko.org/

Set executable permission from Windows host on Linux filesystem

I have a python script which runs on a Windows machine. On this machine I have mounted a Samba filesystem (on a Linux host).
When I now try to change file permissions on the filesystem with os.chmod(S_IXUSR) it doesn't set the excutable permission, since it is hardcoded in Windows to not do anything as some research I did suggests.
Do I have any chance to change unix file permissions from a Windows host using python?
Short answer: No.
Slightly longer answer: It would perhaps not be impossible to write a Windows Samba driver which supports this, but you seem to be asking for an existing solution.

download/upload files to remote windows server using python

I want to download/upload file from remote windows 2008 R2 servers using my python script. The problem is I do not want to install anything extra on my windows server box. I want to achieve this just using my normal login credentials.
Below are the different methods I heard of:
Use paramiko SSH : But to use this we have to install an SSH service on the remote box, which i do not want to do.
Use python wmi module: But I guess it does not have the functionality to download files from the remote servers.
Mount drives on your local box: Also do not want to do this as there will be lot of machines i want to connect to.
Use winscp: I guess it will also require SSH ?
Fabric: Heard of this, not sure what are its prerequisites.
are there any other methods with which i can achieve this?
When in windows do as the windows-users do.
If you can't install additional software on the server, you need to mount the drive, and interact with the remote files like they are local files.
You mention that you have too many remote servers to connect to. Why not pick one drive letter, and reuse it for each server you need to connect to?
With net use you can mount from the command line.
Syntax for net use
net use p: /delete:yes
net use p: \\remote_host\share_name password /user:domain\user
Use Python's subprocess package to run the mount commands. Subprocess tutor.
import subprocess
# Make sure the drive isn't mounted.
try:
subprocess.call('net use p: /delete:yes', shell=True)
except:
# This might fail if the drive wasn't in use.
# As long as the next net use works, we're good.
pass
for host in ("host1", "host2"):
# mount(map) the remote share.
subprocess.call('net use p: \\%s\share_name password /user:domain\user' % host, shell=True)
with open("p:\path\remote-file.txt", "r") as remote_file:
# do stuff
# dismount(map) the drive
subprocess.call('net use p: /delete:yes', shell=True)
(Don't have a windows box and network to test this on.)
Use the win_unc library: http://covenanteyes.github.io/py_win_unc/
This will allow you to do normal modifications to file paths as well as log in as a different user.

How do I access a remote filesystem using Python on Windows?

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.
Are there any modules for accessing external Windows machines?
Has anyone done anything like this before?
I'm specifically looking to log into Windows 7 machines, not unix.
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?
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?
One solution is to turn on telnet, and use paramiko or twisted to
talk across it. This isn't very secure of course
Next up, set up a samba share, and access the folder remotely. This
is also insecure, though less so than telnet
You could find a ssh daemon port and run that, if you are so inclined
Psexec from sysinternals might work
Use twisted to build a server app with the features you need
Use ncat to listen on a port and spawn a cmd prompt
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.
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.
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.
Per your edit, the easiest thing to do is just set up a samba share on the box.
After this share is set up:
with open(r'\\myCompNameOrIP\C\windows\logs\logfile.txt','rb') as logfile:
loglines = logfile.readlines()
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.
From Ubuntu I use samba:
In Bash:
gvfs-mount smb://them/folder
Here I give name, domain and password
Then in python:
folder = '/home/me/.gvfs/folder on them'
using the os module I read folders and files inside.
I am working in a small business environment.
Why not have each of the computers send the log file to the central computer?

Categories