Set executable permission from Windows host on Linux filesystem - python

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.

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/

Copy folder from server(Linux) to local machine(windows) in python

How to copy a folder from Server (linux) to a local machine (windows) in python.
I tried with the given code but it did not work
from distutils.dir_util import copy_tree
copy_tree("source_path ","destination_path")
I used copy_tree command to copy a folder on my local machine but when I used the same command to copy a folder from server to local machine then it did not work.
Any other method is there? Or any changes needed?
You need to use SSH, SCP, or SFTP to transfer files from host to host.
I do this a lot and like to use SSH and SCP. You can run and SSH server on your windows machine using OpenSSH. Here is a good set of instructions from WinSCP: https://winscp.net/eng/docs/guide_windows_openssh_server.
I recommend using Paramiko for SSH with Python. Here is a good answer showing how this works with python: https://stackoverflow.com/a/38556344/634627.
If you set up OpenSSH, you could also do this with SFTP, sometimes I find this is more suitable that SCP. Here is a good answer showing how that works: https://stackoverflow.com/a/33752662/634627
The trick is getting OpenSSH running on your Windows host and setting up SSH keys so your server can authenticate to your localhost.
Using copytree should work if:
the folder on the server is made available to the windows machine as a client.
you have sufficient access permissions.
you use a raw string for the windows path to prevent string interpretation.
Ad 3: try print('c:\test\robot'):
In [1]: print('c:\test\robot')
obot est

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

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)

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