0

I am trying to set a proxy on a rotating basis; but my export commands are not working. I'm using CentOS 7.

I've gone back to basics and tested a really simple bash script, which sets and unsets environment variables, and then tests them. The environment variables appear set properly, but my external IP is still showing as my local IP.

$1 is an address in the form http://119.27.177.169:80

#!/bin/bash

function set() {
    export http_proxy="$1/"    # not working
    export https_proxy="$1/"   # not working
    testExtIP
}

function unset() {
    unset http_proxy
    unset https_proxy
    testExtIP
}

function testExtIP() {
    externalHTTP=$(curl -s http://api.ipify.org) # an API that echos the external IP quickly
    externalHTTPS=$(curl -s https://api.ipify.org)
    echo "External HTTP: $externalHTTP; external HTTPS: $externalHTTPS" 
}

case "$1" in
    'set')
    set $2 # speed
    ;;
    'unset')
    unset $2
    ;;
    'test')
    testExtIP $2
    ;;
    *)
    echo "Usage: $0 $versum [set|unset|test]"
    ;;
esac

I would like the script to return:

External HTTP: 119.27.177.169; external HTTPS: 119.27.177.169

But my external IP isn't set:

External HTTP: **SERVERIP**; external HTTPS: **SERVERIP**

which I've tested using various tools. Why is the export command not working? Am I missing something important?

marked as duplicate by Patrick, thrig, mdpc, Michael Homer, jimmij Jan 20 '18 at 12:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • this does not seem useful, as the exports are only in the context of the script, and vanish when the script exits (a child process cannot alter the environment of its parent) – thrig Jan 19 '18 at 22:33
  • Just some nitpicking: set and unset are shell built in commands. Just should pick other names for your functions. – Kusalananda Jan 20 '18 at 12:33
0

You can't do it like this. A script has its own set of variables (or environment if you will) which is only available to the script itself and that disappears as soon as the script ends. Therefore the shell which is the parent of the script (because you run the script from within the shell) doesn't know about the environment and set variables of the script.

  • This is helpful. I think here was my problem all along. A good solution would be to use these functions in the script that uses the proxy server? – Michael Riordan Jan 19 '18 at 22:50
  • No, the environment is still only available to the script itself so testing with the external server does not give the expected result. – wie5Ooma Jan 19 '18 at 22:57
  • And by external server I mean the API of course. – wie5Ooma Jan 19 '18 at 22:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.