0
I receive error:
awk: bad regex '{|:|}': Invalid preceding regular expression {"arguments":{},"result":"success"} {"port":37482}
Which I believe is related to this line:
PORT=$(echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
#echo $PORT
Does anyone know what it means and how I can fix it? I am new to scripting but as I understand it the expression |:| is incorrect. $json is a file I am pulling from my VPN with port information for forwarding.
My input:
#!/usr/bin/env bash
#
# Enable port forwarding when using Private Internet Access
#
# Usage:
# ./port_forwarding.sh
TRANSUSER=xxx
TRANSPASS=xxxx
TRANSHOST=localhost
error( )
{
echo "$@" 1>&2
exit 1
}
error_and_usage( )
{
echo "$@" 1>&2
usage_and_exit 1
}
usage( )
{
echo "Usage: `dirname $0`/$PROGRAM"
}
usage_and_exit( )
{
usage
exit $1
}
version( )
{
echo "$PROGRAM version $VERSION"
}
port_forward_assignment( )
{
client_id_file="/etc/openvpn/pia_client_id"
if [ ! -f "$client_id_file" ]; then
if hash shasum 2>/dev/null; then
head -n 100 /dev/urandom | shasum -a 256 | tr -d " -" > "$client_id_file"
elif hash sha256sum 2>/dev/null; then
head -n 100 /dev/urandom | sha256sum | tr -d " -" > "$client_id_file"
else
echo "Please install shasum or sha256sum, and make sure it is visible in your \$PATH"
exit 1
fi
fi
client_id=`cat "$client_id_file"`
json=`curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
if [ "$json" == "" ]; then
json='Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding'
fi
echo $json
}
#trim VPN forwarded port from JSON
PORT=$(echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
#echo $PORT
#change transmission port on the fly
CURLOUT=$(curl -u $TRANSUSER:$TRANSPASS ${TRANSHOST}:9091/transmission/rpc 2>/dev/null)
REGEX='X-Transmission-Session-Id\: (\w*)'
if [[ $CURLOUT =~ $REGEX ]]; then
SESSIONID=${BASH_REMATCH[1]}
else
exit 1
fi
DATA='{"method": "session-set", "arguments": { "peer-port" :'$port' } }'
curl -u $TRANSUSER:$TRANSPASS http://${TRANSHOST}:9091/transmission/rpc -d "$DATA" -H "X-Transmission-Session-Id: $SESSIONID"
EXITCODE=0
PROGRAM=`basename $0`
VERSION=2.1
while test $# -gt 0
do
case $1 in
--usage | --help | -h )
usage_and_exit 0
;;
--version | -v )
version
exit 0
;;
*)
error_and_usage "Unrecognized option: $1"
;;
esac
shift
done
port_forward_assignment
exit 0
The script is taken from: https://www.privateinternetaccess.com/forum/discussion/23431/new-pia-port-forwarding-api/p3?
It is designed to make a request to their API for a port number and then to forward that port received into transmission daemon.
What are you trying to accomplish? What is your input, what is your desired output? If you have some input JSON, then post it. If you are trying to retrieve the port number from the JSON, then say so. Try not to ask an XY problem https://meta.stackexchange.com/a/66378/364309
– Attie – 2017-10-18T21:38:45.227I did ask previously here but didn't receive any answers. I thought because I made the question too specific! https://superuser.com/questions/1258891/tomato-transmission-openvpn-and-port-forwarding
– Dodgexander – 2017-10-19T00:11:28.203Sorry for being a noob but I am trying to learn how I can take the port number from the json and then read the given port number so I can forward it to transmission. I understand the json returns the port, but I am not sure how, nor how to take that port and insert it into transmission – Dodgexander – 2017-10-19T23:51:42.893