Unable to fetch and display files using snakebite - python

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.

Related

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.

Reproduce smb:// protocol syntax using pysmb

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.

Microsoft Ajax Minifier output path access error when called from Python

I'm calling Microsoft Ajax Minifier from Python like so:
minifyArguments = ["C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\AjaxMin.exe"]
for f in filesToMinify:
minifyArguments.append(f)
minifyArguments.append("–out")
minifyArguments.append("C:\\Users\\XXX\\Desktop\\TestFolder") #minifyJSDestinationPath
minifyArguments.append("–clobber")
ajaxMinProcess = subprocess.Popen(minifyArguments, shell=False)
stdout, stderr = ajaxMinProcess.communicate()
This works fine, and I see that it's starting etc. but when it wants to write the output file it gives an error:
AjaxMinifier.exe: error AM-AUTH: Access to the path 'C:\Users\XXX\Desktop\TestFolder' is denied.
I have tried different folders, the issue is not exclusive to the one in the code. It can't write to any folder.
When I don't call it from Python but directly from the commandline it works without problems.
Why does this happen and how can I fix it?
Thanks.
I found the solution to my problem:
This line:
minifyArguments.append("C:\\Users\\XXX\\Desktop\\TestFolder")
Should include the filename, like this:
minifyArguments.append("C:\\Users\\XXX\\Desktop\\TestFolder\\script.min.js")

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 3 FTPLIB, NoneType Errors, and Uploads/Downloads

In my script I want to be able, in the end, to be able to download all files in a directory and all sub-directories... So I am trying FTPLIB. I'm trying to call dir of my ftp server and put it into a variable, but I get NONETYPE?! I can connect to the server and when I call directory = session.dir() It displays a kind of matrix style output in the console with files, read/write perms, dates, etc.... But when I then try to print Directory all I seem to get is "None". My initial idea was to for each item in the directory download them to my computer, but I can't seem to get a list of the directory!
directory = session.dir()
print(str(directory))
Sorry for the long and probably trivial explanation, but I have become a little bit too frustrated.
Any help would be very much appreciated!
-Clem
First, read this. http://docs.python.org/library/ftplib.html#ftplib.FTP.nlst
Then, try this:
directory = session.nlst()
print(directory)
Note.
You don't need to do print(str(...)). The print function gets the string representation for you.
In the official docs, the very first example shows how to do what you need: use .retrlines('LIST') to read the output of LIST command.
Another way is to use .nlst().

Categories