special-characterssocatash

Passing parameter with special characters to Socat exec script


I have a RNDIS USB modem that allows AT commands through a telnet port (5510). I want to use it to read and send SMS messages from a script. I've used a variant of a similar question posted here but I'm using socat instead of netcat.

  1. I have a helper script called atread.sh:
#!/bin/sh

echo -e $@ 

IFS=$'\n' 
while read line; do
    echo $line >&3
    if [ "${line::2}" = "OK" ] || [ "${line::5}" = "ERROR" ] ; then
        break
    fi
done
  1. I then have a function in my main script:
smsio() {
    response=$(3>&1 socat -T 2 exec:"./atread.sh '$1'" tcp:xxx.xxx.xxx.xxx:5510,crlf)
    ... (some other code to check for a timeout or broken pipe)
}
  1. I invoke the function in the main script like so: smsio "AT+CMGF=1".

This works extremely well as long as the parameter does not include special characters, specifically:

I've tried to escape the special character using backslash (\") and/or adding double quoting ("".."") but it appears that socat is stripping all special characters before it calls the exec script. Any suggestions how to get these special characters through socat?

PS: I've temporarily bypassed the problem via character substitution (i.e using ! for " in the parameter string, then using tr '!' '"' in atread.sh, but I'm looking for a more elegant solution.


Solution

  • Socats address parser interpretes a few characters specially, these are for example:

    : , ! " ' \ ( [ {
    

    You have to escape them with backslash. Please note that you also need to escape a few special characters including \ from the shell parser.

    Use options -d -d -d -d to see the parameters socat receives and uses; try something like

    socat -u /tmp/nonexistingdir/x\\\\x -
    

    to see in the error message what is left of the special characters when socat performs the final system call.