Not sure but have you tried using functions from the os library? For example you could use execl() to execute your commands and pipe the output somewhere-stdin in your case.