I get an "operation not permitted" traceback with trying to use os.setegid(). I've found a few answers to similar questions to this one, but the questions are more complicated than my case, and the answers presume the users did a setuid first, or suggest setting the setgid bit on the directory.
This issue is much simpler. I'm just doing a os.setegid with group id that I am a member of. In the shell I can use sg(), newgrp(), or chgrp() with the same group successfully. Why does this python command fail?
>>> import os
>>> os.setegid(34007)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 1] Operation not permitted
>>>
I am using RHEL7 and an older version of python (2.7.5) but the same error occurs with python 3.6.10, though in that case it's a PermissionError rather than an OSError.
Ensure you're running as root.
https://www.geeksforgeeks.org/python-os-getegid-and-os-setegid-method/
...method is typically available only to the superuser. Superuser
means a root user or an administrative user who has all the
permissions to run or execute any program in the operating system
Related
I am using Python3 on Ubuntu with the HID module python3-hid.
If I use it to open a device, I simply get back:
OSError: open failed
Which prompted me to handle the exception, and see if I can get more information out of it:
try:
h.open(vend, prod)
except OSError as error :
print(error)
print(error.strerror)
print(error.filename)
sys.exit(1)
To my surprise, no further information was available, as both strerror and filename were None.
open failed
None
None
NOTE: My open() succeeds when I run as root, so I know this is a permission thing. But why can't this OSError tell me that it is permission related?
How do I get all the details (specific reason of failure, and filename involved) out of an OSError?
UPDATE: The traceback leads to hid.pyx file:
Traceback (most recent call last):
File "/tmp/./tst.py", line 30, in <module>
h.open(vend, prod)
File "hid.pyx", line 66, in hid.device.open
I am not sure why my system can't find the hid.pyx file.
$ dpkg -S hid.pyx
dpkg-query: no path found matching pattern *hid.pyx*
Checking the upstream package from Ubuntu... it turns out that python3-hid is part of the software for a crypto coin wallet? Strange. I think I will skip using this module, and maybe write directly in C, using libhidapi-hidraw which this python module also uses.
I have the following code:
import os
print (os.listdir("C:/Windows/System32/config"))
print (os.listdir("C:/Windows/System32/winevt"))
I am running this Python script as an administrator and confirmed those directories in fact exist.
The first line outputs with some files and folders, but not all files. For example, there is a registry hive contained within called "SOFTWARE" which does not appear in the output.
The second line says the path wasn't identified. Here is the full output:
['Journal', 'RegBack', 'systemprofile', 'TxR']
Traceback (most recent call last):
File "test.py", line 3, in <module>
print (os.listdir("C:/Windows/System32/winevt"))
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Windows/System32/winevt'
This module (os.listdir), works pretty much everywhere else on the system, but not within System32. I suspect this may be permission related, but am not too sure on how to fix it as I am already running the script as an administrator.
Help would be appreciated. Thanks!
I'm trying to run this in the Python REPL:
file = open('/dev/sda1','rb')
However, I am getting this error:
Traceback (most recent call last):
File <stdin>, line 1, in <module>
IOError: [Errno 13] Permission denied: /dev/sda1
How do I gain access to /dev/sda1?
You likely have to be root to read /dev/sda1. You usually can run sudo to run things as root. For example, to run myscript.py:
sudo python myscript.py
Or to run the REPL:
sudo python
You must be a member of the appropriate group to use sudo. Additionally, your system may not have sudo. If your system does not have sudo, you may have to use su. If your system does not have su, you may have to log in as root.
For more information on sudo, type man sudo.
For more information on su, type man su.
I'm attempting to read in a file and produce a hash of said file. For the most part, this has gone smoothly. However, there are a few files that I am unable to get a handle to. For instance:
file("c:\\windows\\system32\\nvvsvc.exe")
Produces the error: IOError: [Errno 2] No such file or directory: 'c:\\windows\\system32\\nvvsvc.exe
Similarly, the command:
subprocess.check_output(r"dir c:\windows\system32\nvvsvc.exe", shell = True)
Produces the error:
File Not Found
Traceback (most recent call last):
File "< stdin >", line 1, in < module >
File "C:\Python27\lib\subprocess.py", line 544, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'dir c:\windows\system32\nvvsvc.exe' returned non-zero exit status 1
I tried many variations thereof, including trying to even get cacls.exe to admit there's a file there. However, when I back out of the python instance (exit()) and perform a dir c:\windows\system32\nvvsvc.exe, it returns that there is indeed a file there.
A similar test using calc.exe instead allows me to view it and hash it both just fine from inside python.
I'm running Python 2.7. I've attempted kicking off python with administrative privs (run as administrator for the command prompt), but that doesn't change anything. There appears to be some change in privileges when running from inside python to the extent that the file system is essentially pretending that file doesn't exist to python.
For reference, the owner of the file in question (there are others that are similar), is system, and the cacls on the file allow reading of it from user, admin and system.
Simple answer! (but not necessarily easy to find) In regards to the IOError, it's because you don't need to escape backslashes.
The second part looks like a permissions error, as mentioned in the comments above.
If this is the 64-bit system try this:
subprocess.check_output(r"c:\windows\SysNative\nvvsvc.exe", shell = True)
The key here is "SysNative"
I'm trying to use the ipy.vim script to set up a small python dev environment, but I'm running into a connection problem. When I type ipy_vimserver.setup("demo") I get this error:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/usr/lib/pymodules/python2.6/IPython/Extensions/ipy_vimserver.py", line 109, in serve_me
self.listen()
File "/usr/lib/pymodules/python2.6/IPython/Extensions/ipy_vimserver.py", line 93, in listen
self.socket.bind(self.__sname)
File "<string>", line 1, in bind
error: [Errno 98] Address already in use
When I type it a second time, everything is fine but when I launch gvim the F4/F5 command do nothing and state that they can't connect to the Ipython server.
any suggestion?
Problem:
Look at the last line of the stack trace: error: [Errno 98] Address already in use
Explanation:
A nice explanation of "Address already in use" error can be found here: "Bind: Address Already in Use"
Possible Solution:
As I have not tried ipy.vim setup myself, from networking point of view, a quick suggestion would be to:
close/kill both the server (ipython server) and the client (vim running ipy.vim).
Restart ipython server
Run vim with ipy.vim and try to debug.
Additional Info:
On Linux/Unix machines, the timeout values are defined in /proc/sys/net/ipv4/tcp_keepalive_time and /proc/sys/net/ipv4/tcp_fin_time
On Windows machines, this is set in HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpTimedWaitDelay. More details is here: TcpTimedWaitDelay
If I run the exact same code I get the same error. If I change the name from "demo" to, for example, "cookies" it works. Hmm....
This happens when there's already a socket with the name you're trying to create, in this case 'demo'. You need to delete it before doing the ipy_vimserver.setup - in your case, the socket file to delete is ~/.ipython/demo
(I'm one of the authors of ipy.vim)