If Python, if you are developing a system service that communicates with user applications through sockets, and you want to treat sockets connected by different users differently, how would you go about that?
If I know that all connecting sockets will be from localhost, is there a way to lookup through the OS (either on windows or linux) which user is making the connection request?
On Linux and other unixy system, you can use the ident service.
I'm not sure if Windows offers something similar.
Unfortunately, at this point in time the python libraries don't support the usual SCM_CREDENTIALS method of passing credentials along a Unix socket.
You'll need to use an "ugly" method as described in another answer to find it.
On Linux you can get the source (i.e. client-side) port of the socket and parse the output of the lsof(8) utility searching for who is using that port.
Here's the manual page.
Related
I'm using cassandradb with the datastax python driver on a linux system. Typically when using other databases, I've found that that on unix-compatible systems there's usually a way to establish a connection to the database via a unix socket, instead of using the loopback interface. By connecting via socket, you can bypass the OS networking stack altogether, and the result is usually better performance.
I'm pretty sure that cassandra supports this, not in the least because there is a UnixSocketEndPoint class in the cassandra.connection module of the cassandra pip package. This class is implemented in the github repo, but there's no documentation whatsoever about how to use it. Is anybody using this? How do I go about configuring a unix-socket connection to cassandra with the python API?
I've managed to find the following code snippet, from the cassandra python driver tests on github:
lbp = UnixSocketWhiteListRoundRobinPolicy([UNIX_SOCKET_PATH])
ep = ExecutionProfile(load_balancing_policy=lbp)
endpoint = UnixSocketEndPoint(UNIX_SOCKET_PATH)
cls.cluster = TestCluster(contact_points=[endpoint], execution_profiles={EXEC_PROFILE_DEFAULT: ep})
Which actually is most of what I'm looking for. I'm just confused about how to configure cassandra to listen via socket instead of TCP. Or does cassandra have a default socket path?
Many thanks in advance.
I am sending data via pickle and socket as found here: Socket Programming in Python using Pickle. I am running 2 scripts with different environments and they are communicating with each other. I plan to have this not connected to the network. i.e. no wifi. As I understand it, since they are connected on the same pc, they should still work right or do I need wifi?
P.S. I am not asking for any code, it is done, working, all I am asking is that would something like this work without internet connection.
Yes, you can absolutely use Unix domain sockets or even local servers on the same computer and it should work without a connection to the broader internet.
I'm using PyZMQ for IPC (over TCP) on Windows 10, as part of an automated updater. I have noticed that on some computers, a firewall prompt appears to select if it can use public or private internet, despite the fact that it makes no connections to the internet - only to localhost.
This PyInstaller-packaged script is launched by a user-land script.
So:
Script launches my PyInstaller-packaged script->
Script uses pyzmq strictly to connect to localhost->
Windows prompts how it should be allowed through firewall
This prompt doesn't stop the program, but I don't want users to see that and wonder what virus they might have.
This is the code that supposedly triggers it:
sckt = self.zmq_context.socket(zmq.REQ)
sckt.connect('tcp://localhost:%s' % updater_shared_port)
Is there anything I can do to stop that pop-up from Windows Firewall?
Thanks!
Yes, there is something:
may implement the connection by some other than tcp:// transport class.
ZeroMQ can help you build your ideas via smart transports{ ipc:// | inproc:// | vmci:// } given you need not assemble/disassemble the full-height-L3-ISO-OSI stack to reach a counterparty, hosted on the same localhost.
The firewall could be configured to allow your programmes to run unhindered. That might be a nuissance to do by hand. There's probably a way to have an installer configure the firewall appropriately, but that'll be a ton of work to set up.
On the off-chance that it's the binding end that's the causing the pop up (not the connecting end as you suggest), apparently one can bind a zmq socket to a particular interface. This is done with a connection string such as zmq_bind(socket, "tcp://127.0.0.1:5555"); This will clamp the socket to the loopback, which may well not trouble the firewall at all. zmq_bind(socket, "tcp://*:5555) will open a socket on all interfaces, including any Internet facing interfaces, which should certainly grab the attention of the firewall. If you've not already tried that, might be worth a quick go.
I'm new to coding in Python and what motivates me to start coding is the idea of writing a piece of software that will connect to a proxy server via SSH and then once connected will route all network traffic of the system trough it, seamlessly to the user.
I am actually using the paramiko module to connect to the server and it works fine, but now I would like to know if there is some way to make the system change its socks proxy configuration so I can route the traffic to the proxy, on a way the user doesn't need to do anything. Is there any existing module that will help on this task ?
Thank you.
You can see the existing project sshuttle, it transfers all traffic over ssh.
I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to be given to me but maybe some pointers on what libraries I should use or any issues I need to address when writing the script.
EDIT: All the remote machines are using Windows 2003
People usually recommend paramiko as a library to do ssh (and I'm assuming that you need ssh to get into the remote machine). There is a good tutorial for it.
Edit: On windows, the easiest way is probably to use SysInternals psservice utility, to be invoked with os.system; this can start a remote service, and accepts logon credentials.
If you want to do it directly in Python, you need win32service.StartService. Before that, you need to open the remote service manager, and then the remote service. Before that, you need to impersonate the user as which you want to perform the operation, see the example.
Take a look at Fabric wich is based on paramiko.
This is really a good tool to automate remote tasks with python.
Fabric Documentation will show you how easy it is to use.
What kind of OS is your remote machine running? If it's linux, run ssh(1) using the subprocess module.
If it's windows, then get the win32 extensions. They allow you to call Windows functions. There should be an API to allow to access services. If they don't, there is a tool called sc (docs) which you can run using the subprocess module.
Which OS for the target machines? If 'service' is 'Windows NT service', and your local machine is also Windows, I'd use IronPython as the Python language implementation and call straight into the WMI facilities in the .net System.Management namespace -- they're meant for remote admin like that.
On Windows, the wmi module is now fantastic for this.