os.tmpfile() doesn't work under windows? - python

I'm trying to use generateDS under windows, which uses os.tmpfile. Unfortunately, os.tmpfile doesn't work for me:
(oneclickcos) C:\Users\Marcin\Documents\oneclickcos\xsd>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.tmpfile()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 13] Permission denied
>>>
I've got all my temp directories set with full control for everyone, so that shouldn't be the problem.
What could be causing this?

Run the script as administrator (right click on the script and select 'run as administrator'), the script lacks the permissions to execute os.tmpfile().
Edit:
As I see you're using the interpreter, simply run the interpreter as administrator. If you're accessing it though a terminal, running the terminal as administrator should be sufficient.

As Griffin pointed out the problem is that the os.tmpfile() tries to create a file in the root directory. If you don't like to run the script as administrator you can use os.tmpnam() and handle the file yourself.
Warning: Use of tmpnam() is vulnerable to symlink attacks

Related

Executing sudo commands on remote machine via jumpssh in python

I'm working on a python script that can execute commands on a remote host via ssh and jumpbox. I can do this just fine using the jumpssh module as long as the command is unpriviledged. It's when I need to do a sudo commands where I run into problems as the command just hangs, presumably waiting for me to enter my sudo password. I've tried to google this and came up empty.
Here is what I've did to test this out:
c:\siggi>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import jumpssh
>>> gw = jumpssh.SSHSession("jumpbox","siggi").open()
>>> remote = gw.get_remote_session("supergeeks")
>>> remote.get_cmd_output("whoami")
'siggi'
>>> remote.get_cmd_output("hostname")
'supergeeks'
>>> remote.get_cmd_output("ls")
'exp.xlsx agent_3.3.0.10614-10774_amd64.deb Status.py'
>>> remote.get_cmd_output("ls /")
'bin home\t lib32\t media root swapfile var\r\nboot initrd.img lib64\t mnt\t run sys\t vmlinuz\r\ndev initrd.img.old libx32\t opt\t sbin tmp\t vmlinuz.old\r\netc lib\t lost+found proc\t srv usr'
>>> remote.get_cmd_output("ls /root")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\SigurðurGísliBjarnas\AppData\Roaming\Python\Python310\site-packages\jumpssh\session.py", line 431, in get_cmd_output
return self.run_cmd(cmd=cmd, **kwargs).output
File "C:\Users\SigurðurGísliBjarnas\AppData\Roaming\Python\Python310\site-packages\jumpssh\session.py", line 399, in run_cmd
raise exception.RunCmdError(exit_code=exit_code,
jumpssh.exception.RunCmdError: Command (ls /root) returned exit status (2), expected [0]: ls: cannot open directory '/root': Permission denied
>>> remote.get_cmd_output("sudo ls /root")

having issues with pykd (pykd.DbgException: Call IDebugClient::GetOutputCallbacks failed HRESULT 0x80010107)

I'm working with pykd and am able to connect it with my debugger (windbg) but for some reason I'm unable to process any command with pykd.dbgCommand not sure what the issue is as I've tried multiple methods to try and resolve the issue:
Reinstall pydk + python
tried on python2.7, python3.5, python3.8
tried a different system + different debug session
when I tried to run the command on the windbg session it was able to produce the results but somehow it's now able to do that from python console.
pykd.dbgCommand("!analyze -v")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pykd.DbgException: Call IDebugClient::GetOutputCallbacks failed
HRESULT 0x80010107
any help would be appreciated.
thanks to everyone answering in advance.
I tried to run dbgCommand('!analyze -v') with three different dump from python REPL:
usermode native dump: OK
kernel mode dump: OK
managed app dump:
0:000> !py
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> dbgCommand('analyze -v')
Traceback (most recent call last):
File "<console>", line 1, in <module>
pykd.DbgException: Call IDebugControl::ExecuteWide failed
HRESULT 0x80040205
Then I run this script:
import pykd
a = pykd.dbgCommand('!analyze -v')
print(a)
this script works OK with all of these dump.
I believe there is a bug. I've opened an issue:
https://githomelab.ru/pykd/pykd-ext/-/issues/15
Can you provide a dump which reproduce the bug to the pykd issues tracker.

Python 2.7 64 bit search path

I am trying to load sqlite 64 bit while running Python 2.7 64 bit. I can do this interactively, but, not from a script.
Interactive:
$ /c/Python27-64/python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
From this script, one single line, the same as was done from the python shell:
import sqlite3
Run from command line:
$ /c/Python27-64/python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import sqlite3
File "c:\Python27-64\lib\sqlite3\__init__.py", line 24, in <module>
from dbapi2 import *
File "c:\Python27-64\lib\sqlite3\dbapi2.py", line 28, in <module>
from _sqlite3 import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
The script is obviously finding a 32 bit dll. But why? What is the difference between interactive and from the single line script? How is the DLL search being modified?
In case anyone runs into this, the problem was the file _sqlite3.pyd in the directory I was running the script. Can someone explain why Python creates it's own version of the Windows dll? Is this simply wrapped so that Python can make calls into it? Perhaps a ctypes wrapper?

Problems in pexpect (python3.3)

Running 3.3 python on CentOS 7.
Tryin' to write simple script but can't get pexpect module to work as I want
if I use interpreter python 3.3, I can write this commands correctly
[root#localhost expect]# python3.3
Python 3.3.3 (default, Apr 7 2015, 02:31:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> child = pexpect.spawn('telnet 10.1.1.1')
but If I run file pexpect.py with exactly same commands, I get
[root#localhost expect]# python3.3 /usr/etc/pexpect.py
Traceback (most recent call last):
File "/usr/etc/pexpect.py", line 1, in <module>
import pexpect
File "/usr/etc/pexpect.py", line 3, in <module>
child = pexpect.spawn('telnet 10.1.1.1');
AttributeError: 'module' object has no attribute 'spawn'
I found some similar info in the google, advice was to move .py file to another folder.
It didn't work for me.
Another advice was to delete " pycache" folder (I've got same in my pexpect.py location), but it didn't work aswell. Errors are still the same, this folder are still created after running the script (trying, I mean).
Any ideas?
You have called your file pexpect.py. You need to rename it to something else as you are importing from your file not the pexpect module. You also need to delete any .pyc in the same folder. It does not matter where you move your script, the current folder is still going to be in the path before where the actual pexpect module is.

pattern.web URL dowload error -- IOError: [Errno 13] Permission denied

I am having trouble using the URL object from the web package of the pattern library. When I try to download text from a URL object, I get a permission error.
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pattern.web
>>> urlObject=pattern.web.URL("http://google.com")
>>> urlResponse = urlObject.download()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pattern/web/__init__.py", line 426, in download
cache[id] = data
File "/usr/local/lib/python2.7/dist-packages/pattern/web/cache/__init__.py", line 96, in __setitem__
f = open(self._hash(k), "wb")
IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/pattern/web/cache/tmp/c7b920f57e553df2bb68272f61570210'
I had no problem with this when running python with sudo, but I would like to understand what is going on here and how I can avoid granting root permissions to python just to download a URL. Does anyone have any insight on this issue?
Running Ubuntu 12.04, python 2.7.3, pattern version 2.6
You need to run it in a directory where you have write access because it is attempting to create a cache file.
(Probably want to clean up cache files from when you ran it with sudo because it seems to have polluted your system directories with chaff.)
It looks like you could try
urlResponse = urlObject.download(cached=False)
to disable the caching functionality.
Changing the owner/group of /usr/local/lib/python2.7/dist-packages/pattern/web/ to my user seems to have resolved the issue. I installed pattern via pip, nothing fancy, so I am assuming this is a bug and the permissions were too tight when pattern was installed.

Categories