I'm using a radio sender on my RPi to control some light-devices at home. I'm trying to implement a time control and had successfully used the program "at" in the past.
#!/usr/bin/python
import subprocess as sp
##### some code #####
sp.call(['at', varTime, '<<<', '\"sudo', './codesend', '111111\"'])
When I execute the program, i receive the
errmsg: syntax error. Last token seen: <
Garbled time
This codesnipped works fine with every command by itself (as long every parameter is from type string).
It's neccessary to call "at" in this way: at 18:25 <<< "sudo ./codesend 111111" to hold the command in the queue (viewable in "atq"),
because sudo ./codesend 111111 | at 18:25 just executes the command directly and writes down the execution in "/var/mail/user".
My question ist, how can I avoid the syntax error. I'm using a lot of other packages in this program, so I have to stay with Python
I hope someone has a solution for this problem or can help to find my mistake. Many thanks in advance
Preface: Shared Code
Consider the following context to be part of both branches of this answer.
Solution A: Feed Stdin In Python
<<<is shell syntax. It has no meaning toat, and it's completely normal and expected foratto reject it if given as a literal argument.You don't need to invoke a shell, though -- you can do the same thing directly from native Python:
Solution B: Explicitly Invoke A Shell
Moreover,
<<<isn't/bin/shsyntax -- it's an extension honored in bash, ksh, and others; so you can't reliably get it just by adding theshell=Trueflag (which uses/bin/shand so guarantees only POSIX-baseline features). If you want it, you need to explicitly invoke a shell with the feature, like so:In either case, note that we're using
shlex.quote()orpipes.quote()(as appropriate for our Python release) when generating a shell command from an argument list; this is critical to avoid creating shell injection vulnerabilities in our software.