I am trying to replicate this command using python and Popen:
echo "Acct-Session-Id = 'E4FD590583649358F3B712'" | /usr/local/freeradius/bin/radclient -r 1 1.1.1.1:3799 disconnect secret When running this from the command line as it is above, I get the expected:
Sent Disconnect-Request Id 17 from 0.0.0.0:59887 to 1.1.1.1:3799 length 44 I want to achieve the same from a python script, so I coded it like this:
rp1 = subprocess.Popen(["echo", "Acct-Session-Id = 'E4FD590583649358F3B712'"], stdout=subprocess.PIPE) rp2 = subprocess.Popen(["/usr/local/freeradius/bin/radclient", "-r 1", "1.1.1.1:3799", "disconnect", "secret"], stdin = rp1.stdout, stdout = subprocess.PIPE, stderr = subprocess.PIPE) rp1.stdout.close() result = rp2.communicate() print "RESULT: " + str(result) But, I must be doing this incorrectly as the "result" variable contains the radclient usage info, as if it is called incorrectly:
RESULT: ('', "Usage: radclient [options] server[:port] <command> [<secret>]\n <command>.... Anybody any idea where my mistake lies?
Thanks!
echo"-r 1"should be separate arguments:"-r", "1"echoas a subprocess. Just write the string into the input of the other subprocess with Python means. This will make everything way simpler.communicatesupports atimeoutargument so you can bound the time spent waiting for a response. Yes, it only does a single input/output, so live back-and-forth communication isn't possible, but the only way to do that safely is to use threads orselect/selectorsmodule primitives (the same waycommunicatedoes) to manage sending and pulling data in a deadlock-free fashion. Much more complicated, but the only way to do it safely withoutcommunicate.