Reproduce smb:// protocol syntax using pysmb - python

I'm trying to use pysmb to save a file to a network share. In osx finder, I can log into the share by pressing cmd+k and then typing smb://my.smb.url.com/folder/subfolder and filling in the username/password.
I'm trying to list the contents of this same location using pysmb. This is what I have so far:
from smb.SMBConnection import SMBConnection
conn = SMBConnection('username', 'password', 'name', 'my.smb.url.com',
sign_options=SMBConnection.SIGN_WHEN_REQUIRED, is_direct_tcp=True)
conn.connect('my.smb.url.com', 445)
shares = conn.listShares()
for s in shares:
print(s.name)
I would expect 'folder' to be printed as one of the shares, since I am able to log into that folder with finder. However, it isn't one of the listed shares. The shares listed are:
ADMIN$
C$
D$
IPC$
NETLOGON
SYSVOL
And the ones ending with $ don't seem to be readable (calling conn.listPath('C$', '/') throws an exception). Trying to list /folder/subfolder using conn.listPath('folder', '/') also throws an exception.
I'm trying to figure out how to map the /folder/subfolder in the smb connection string to the pysmb api. I have tried including the folder name directly in the SMBConnection constructor and within the call to connect, but the former doesn't change anything and the latter results in a "Name or service not known" exception.

Related

Unable to generate a share link via a script and Synology API

I'm hitting a wall with the python script at https://stackoverflow.com/a/60946539/1641112. I've added the script to a file called test.py3.
I'm running this command directly on my Synology drive with an admin account: python test.py3 /volume2/dir/key-basic-preferred.pdf --debug
The relevant part of the debug output:
Get(): "request = entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf""
GET: "http://127.0.0.1:5000/webapi/entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf"&_sid=011o6xaJaxvLsKCJ4N91278"
GET: "<Response [200]>"
ERROR: Get (entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf"&_sid=011o6xaJaxvLsKCJ4N91278):
Error: 408: Unknown error
The 408 error is an unknown file according to the API docs.
However, I know that file exists. I've tried URL encoding the path. I've tried with and without quotes. I've tried noodling with the python code (don't know python) to change the quotations and api version to 1 instead of 3 but I'm not having any luck.
Finally figured it out. The file paths are relative to the File Station. So if you have a folder in file station called "backup" and a file in "backup" call "blah.txt", the path to the file is /backup/blah.txt

How to fix: No file output from remote wmic called via Python WMI module through win32_Process.Create

I'm attempting to execute WMIC on a remote host through the WMI module via Win32_Process.Create() function and save the output on said remote host via /output, more or > but neither of these is generating the expected output. Instead, I am receiving empty files.
I have tried using /output, more and > to achieve proper output but the resultant files are always blank. I know that I am creating processes with expected output otherwise because I have tried using 'ipconfig >' into a file on the remote host and it gives the expected output with network configuration data, it is only WMIC that is giving me trouble. I have also tried removing the 'cmd.exe /c' portion and just running directly with wmic.exe with no luck as well as specifying the exact executable location.
I've also tried using /Output:FILENAME alone and /Output:STDOUT with the redirector (>) to a file. None seem to work properly.
import wmi
session = wmi.WMI(computer="192.168.8.132", user="testing", password="testing")
process_startup = session.Win32_ProcessStartup.new()
command = r"wmic.exe /output:C:\Users\testing\WMIUsers0.txt /namespace:\\root\cimv2 UserAccount get * /value"
process_id, return_value = session.Win32_Process.Create(CommandLine=command, ProcessStartupInformation=process_startup)
print("Process Executed: "+command)
print("Process ID: "+str(process_id)+" , Return Value (0 = Success): "+str(return_value)+"\n")
The above code generates an empty file on the host at the expected location rather than the actual WMI returned data. I copy pasted the 'command' given above into the remote host and it works as expected for stdout as well as generating the data within the file.
This is only an approximation of my code but represents the issue well - what do I have to modify to have actual results populate in the destination file rather than a blank file when running remotely over the WMI module?
edit: Using ProcessHacker on the target, I can see my process started with the parameters 'cmd.exe /c wmic /output:"C:\Users\testing\WMIUsers0.txt" /namespace:\root\cimv2 UserAccount get * /value' - If I copy paste this into a separate cmd window, it works as expected and generates the file but when I run it remotely, even though it has the proper parameters, it generates an empty file. I am going insane trying to figure out why this isn't working..
Edit2: I've tried to many different variations utilizing subprocess.call, check_output and other things and am having no luck trying to do this in Python. Today is not my day.
Edit3: Decided to just use subprocess.getoutput and store the data locally because getting it to execute remotely was driving me insane - No idea why it won't work after inspecting what was executing - same exact cmdline window parameters but one generates blank file, other was dataful. Crazy, has to be some functionality of the WMI module preventing proper transfer..or something...no idea.

Unable to fetch and display files using snakebite

I'm trying out snakebite. I started the following client:
from snakebite.client import Client
client = Client("my.host.com", 8020, effective_user='datascientist')
First, I tried to list the users directory:
for x in client.ls(['/user/datascientist']):
print x
This worked nicely and printed couple of dictionaries; one for each item in the directory. One of the items is a file foobar.txt which I'd like to see. To that end, I believe I should use Client.cat:
for cat in client.cat(['/user/datascientist/da-foobar.txt',]):
print(cat)
for item in cat:
print(item)
However, this didn't work. I got the following error message:
ConnectionFailureException: Failure to connect to data node at (10.XXX.YYY.ZZZ:50010)
What am I doing wrongly?
BTW: using PyWebHdfsClient from pywebhdfs.webhdfs I managed to see the file by starting a client with the same address but with port 50070. I don't know whether this is relevant or not.
Edit 1: I also tried to use snakebite.client.Client.text and got the same error. I guess this is not surprising.
BTW, the file's content is my file is this\ntest file.
I found a/the solution. It seems like the listing operation can be accomplished on the name-node alone. In contrast, the printing of the text file needs to access the data-nodes! By instantiating the client as follows
client = Client("stage-gap-namenode-2.srv.glispa.com", 8020, effective_user='datascientist',
use_datanode_hostname=True)
the cat operation works as it is not using the internal IP, but the hostname. I summarized a minimal example.

Disable registry redirection to Wow6432Node in Python

I'm trying to access registry keys under HKEY_LOCAL_MAHINE\SOFTWARE... on a 64 bits system. I have following code but judging by the results it gets redirected to Wow6432Node even though I have _winreg.DisableReflectionKey(_winreg.OpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE")) in my code.
import _winreg
import wmi
c = wmi.WMI(computer="localhost", namespace="root/default").StdRegProv
_winreg.DisableReflectionKey(_winreg.OpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE"))
result, names = c.EnumKey(hDefKey=_winreg.HKEY_LOCAL_MACHINE, sSubKeyName="SOFTWARE")
print names
_winreg.EnableReflectionKey(_winreg.OpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE"))
I have read the following case but it seems that the solution described there doesn't work for either the author or for me:
How can I turn off registry redirection on Python?
Also checked _winreg documentation, but there are no specific examples and I have no idea what I'm doing wrong. Any ideas? Sorry, I didn't have permission to comment in the existing case and had to open a new one.
It looks like "_winreg.KEY_READ | _winreg.KEY_WOW64_64KEY" does the job. To be more precise you need to open the key this way:
_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE", 0, _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY)
I got this working when accessing the localhost registry. However, I still haven't figured out how to connect to remote registry on the domain. Trying to use _winreg.ConnectRegistry, but keep getting access denied error.

Python and Plone help

Im using the plone cms and am having trouble with a python script. I get a name error "the global name 'open' is not defined". When i put the code in a seperate python script it works fine and the information is being passed to the python script becuase i can print the query. Code is below:
#Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
# Insert data that was passed from the form
query=request.query
#print query
f = open("blast_query.txt","w")
for i in query:
f.write(i)
return printed
I also have a second question, can i tell python to open a file in in a certain directory for example, If the script is in a certain loaction i.e. home folder, but i want the script to open a file at home/some_directory/some_directory can it be done?
Python Scripts in Plone are restricted and have no access to the filesystem. The open call is thus not available. You'll have to use an External Method or full python module to have full access to the filesystem.

Categories