pythonlinuxbashvpnopenconnect

Redirection unexpected /bin/sh: 1:


I am receiving this error:

/bin/sh: 1: Syntax error: redirection unexpected
Traceback (most recent call last):
  File "vpntest.py", line 24, in <module>
    output = check_output(command, shell=True)
  File "/usr/lib/python2.7/subprocess.py", line 223, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'source /etc/openconnect/dsn-dsmc.conf; openconnect -b -u ${USER} --cafile=${CACERT} --certificate=${CERT} --sslkey=${KEY} ${HOST} <<< $PASS;ping 8.8.8.8 -w 5; ip addr; echo $?' returned non-zero exit status 2

This is my code:

import subprocess
import argparse
import sys
from subprocess import check_output


def parse_args(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", "--user", action="store",
                        help="User for login",
                        dest="user")
    parser.add_argument("-p", "--pwd", action="store",
                        help="Password",
                        dest="pwd")
    args = parser.parse_args()
    return args
args = parse_args(sys.argv[1:])

command = 'source /etc/openconnect/cfgfile.conf; openconnect -b -u ${USER} --cafile=${CACERT} --certificate=${CERT} --sslkey=${KEY} ${HOST} <<< $PASS;ping 8.8.8.8 -w 5; ip addr; echo $?'
     
output = check_output(command, shell=True)

I have tried changing:

command = 'source /etc/openconnect/cfgfile.conf; openconnect -b -u ${USER} --cafile=${CACERT} --certificate=${CERT} --sslkey=${KEY} ${HOST} <<< $PASS;ping 8.8.8.8 -w 5; ip addr; echo $?'

for

command = '#!/bin/bash source /etc/openconnect/cfgfile.conf; openconnect -b -u ${USER} --cafile=${CACERT} --certificate=${CERT} --sslkey=${KEY} ${HOST} <<< $PASS;ping 8.8.8.8 -w 5; ip addr; echo $?'

But when I try to print command or output, It does not show any results, just spaces.


Solution

  • check_output(command, shell=True) uses /bin/sh as the shell which may be not Bash so it may not understand the <<< redirection syntax. If you want to use Bash then you can try like this:

    command = 'source /etc/openconnect/cfgfile.conf; ...'
    # explicitly use bash
    output = check_output(['bash', '-c', command])
    

    And you seem to be sending a password to the openconnect command using <<< syntax. This may not work as you expect. For security reasons, a well designed program would not read password from stdin by default. Instead it would read the passwd directly from /dev/tty. Not sure if openconnect has a special cmdline option so you can pass the password via stdin (or some option like --password <passwd>). If not, you need to use Python libs like .