Dismiss Notice

Welcome To SNBForums

SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.

If you'd like to post a question, simply register and have at it!

While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!

How do I force a variable to process as variable and not string in shell script

Discussion in 'Asuswrt-Merlin' started by Deplorable, Sep 14, 2017 at 1:36 AM.

  1. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I am really really really hard pressed to keep this post from being laced with expletive after expletive after expletive I have been at it so long with no success and nothing useful on Google.

    pc_insert "bind-address-ipv6" ' "bind-address-ipv4": "$VPNADDR", ' $CONFIG

    All I want to do is use this function to replace a line in a config file with the current VPN address. I can't even get to that part because the $VPNADDR WILL NOT process as a variable. It takes it literally and outputs the var name. If I remove the outside quotes and make it instead

    pc_insert "bind-address-ipv6" " "bind-address-ipv4": "$VPNADDR", " $CONFIG

    It processes the variable but removes crucial items like commas and other double quotes. It won't do both, one or the other. Is shell scripting really this *@&#&@ useless?? Please help, this is so infuriating I'm on the verge of going and stomping on the router I'm working on which would be expensive.
     
    Last edited: Sep 14, 2017 at 1:41 AM
  2. thelonelycoder

    thelonelycoder Part of the Furniture

    Joined:
    Jan 23, 2014
    Messages:
    2,911
    Location:
    In the heart of Switzerland
    From the wiki:
    So, with insert you look for a string in dnsmasq.conf and when found to insert after it.
    If I had "no-poll" in dnsmasq.conf and I want to insert after it I would do this:
    Code:
    pc_insert "no-poll" "bind-address-ipv4: $VPNADDR," $CONFIG
    Which correctly gives this, $VPNADDRESS is defined as holla here for testing purposes:
    Code:
    no-poll
    bind-address-ipv4: holla,
    What you are after is to replace a line, so you'd have to use the replace command.
    Can you post the original line and what it should look like after?
     
  3. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    File is /opt/etc/transmission/settings.json with line

    Code:
    "bind-address-ipv4": "0.0.0.0", 
    Line should be same but with my VPN IP address so let's say it becomes


    Code:
    "bind-address-ipv4": "173.239.210.10", 
    I can't just replace the 0.0.0.0 as once it changes I can no longer search based on 0.0.0.0 and have to pull the IP address from the json then replace it somehow that way. That is why I was trying to just delete the line and replace it.
     
  4. thelonelycoder

    thelonelycoder Part of the Furniture

    Joined:
    Jan 23, 2014
    Messages:
    2,911
    Location:
    In the heart of Switzerland
    How about using sed to replace it instead?
    Code:
    sed -i "/bind-address-ipv4/c\\\"bind-address-ipv4\": \"$VPNADDR\"," /opt/etc/transmission/settings.json
    First "bind-address-ipv4" finds this string in the file and replaces it with the second part. Make sure $VPNADDR is defined before it runs.
     
  5. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I did some testing and that replaces the line perfectly so should be gtg. Need to work out a way to pull current VPN address now.
     
  6. thelonelycoder

    thelonelycoder Part of the Furniture

    Joined:
    Jan 23, 2014
    Messages:
    2,911
    Location:
    In the heart of Switzerland
    BTW: The posted sed one-liner does not need the helper file included, so if this is all you want, only put this in the script.
    ... And no expletives were used to find a solution...
     
  7. thelonelycoder

    thelonelycoder Part of the Furniture

    Joined:
    Jan 23, 2014
    Messages:
    2,911
    Location:
    In the heart of Switzerland
    Is it stored in NVRAM?
    Post the line containing it.
     
  8. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I said a few before you showed up so you're covered. I already commented everything out except the line where I was filling the var and then the sed line you posted. Thanks
     
  9. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I had not really gotten further than grabbing someone's code they posted for something similar they were trying to do. I never had the chance to go through and see if it actually works or rather why it doesn't work. Was side tracked on the actual replacement line you gave me the sed code for.
     
  10. thelonelycoder

    thelonelycoder Part of the Furniture

    Joined:
    Jan 23, 2014
    Messages:
    2,911
    Location:
    In the heart of Switzerland
    See if it is in NVRAM.
    Enter this and then open the file /jffs/nvram.txt to search for your current VPN address and post the line containing it:
    Code:
    nvram show | sort > /jffs/nvram.txt
     
  11. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I updated to 68_2 tonight so murphy's law the pos vpn isn't working so may have to redo it. I'll try but the interface is unresponsive at the moment under that tab.
     
  12. thelonelycoder

    thelonelycoder Part of the Furniture

    Joined:
    Jan 23, 2014
    Messages:
    2,911
    Location:
    In the heart of Switzerland
    If it is in NVRAM, you can use this one-liner to define VPNADDR. If the nvram value "nvram_variable" is empty, 127.0.0.1 is used, else the actual value.
    Replace both instances of "nvram_variable" with the actual name of it.
    Code:
    [[ -z "$(nvram get nvram_variable)" ]] && VPNADDR=127.0.0.1 || VPNADDR=$(nvram get nvram_variable)
    Place this before the sed one-liner in your script.
     
    Last edited: Sep 14, 2017 at 4:02 AM
  13. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I'll try that and see what happens. In the interim I got VPN connected after redoing settings and I see this in the nvram.txt you had me run.
    vpn_client2_rip so likely that is what I want, it's the external IP for the VPN. The other is the internal network IP.
     
  14. Deplorable

    Deplorable Occasional Visitor

    Joined:
    Sep 4, 2017
    Messages:
    27
    I was able to update settings.json with the appropriate listening address so I should be good to go from that standpoint. Just have to get the port_forwarding.sh going and I will be golden. Thanks for all the help!
     
    thelonelycoder likes this.

Share This Page