0

I don't understand this escaping. Is some generator available for it?

i need replace normal text (185...) with Variable $NEW_DNS

sed -i "185.228.168.168,185.228.169.168|$NEW_DNS /etc/wireguard/wg0.conf

Output

sed: -e expression #1, char 4: unknown command: `.'

Update: i have like you can see answers and its work, just /etc/wireguard/wg0.conf is not rewrited with new Data (but Output from is correct.

# client start menu
echo "What can i do for you today? "
options=("Show Clients" "Show your DNS Server" "Change your DNS Server" "Install/add new Client" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Show Clients")
            ls *.conf
            ;;
            "Show your DNS Server")
            if [ $( head -n1 $CONF | awk '{print $5}') == "1.1.1.1,1.0.0.1" ]; then
                echo "Your Public DNS Server is Cloudflare (1.1.1.1,1.0.0.1), great unfiltered choice for best speed worldwide! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "176.103.130.130,176.103.130.131" ]; then
                echo "Your Public DNS Server is Adguard (176.103.130.130,176.103.130.131), Advertising Filter kill them all! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "84.200.69.80,84.200.70.40" ]; then
                echo "Your Public DNS Server is WATCH.DNS (84.200.69.80,84.200.70.40), great unfiltered choice for German Clients! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "9.9.9.9, 149.112.112.112" ]; then
                echo "Your Public DNS Server is QUAD9 (9.9.9.9, 149.112.112.112), great TLS encrypted unfiltered choice! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "77.88.8.7,77.88.8.3" ]; then
                echo "Your Public DNS Server is Yandex (77.88.8.7,77.88.8.3), safe choice with Family Filter! "
            fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "185.228.168.168,185.228.169.168" ]; then
                echo "Your Public DNS Server is Clean Browsing (185.228.168.168,185.228.169.168), Uuh TLS encrypted safe choice with Family Filter and Youtube-Safe Option! "
                fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "10.8.0.1" ]; then
                echo "Nothing else then own encrypted and logless dedicated DNS Server! "
                 fi
                 ;;
            "Change your DNS Server")
             if [ $( head -n1 $CONF | awk '{print $5}') == "1.1.1.1,1.0.0.1" ]; then
                 echo "Your Public DNS Server is Cloudflare (1.1.1.1,1.0.0.1), great unfiltered choice for best speed worldwide! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "176.103.130.130,176.103.130.131" ]; then
                 echo "Your Public DNS Server is Adguard (176.103.130.130,176.103.130.131), Advertising Filter kill them all! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "84.200.69.80,84.200.70.40" ]; then
                 echo "Your Public DNS Server is WATCH.DNS (84.200.69.80,84.200.70.40), great unfiltered choice for German Clients! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "9.9.9.9, 149.112.112.112" ]; then
                 echo "Your Public DNS Server is QUAD9 (9.9.9.9, 149.112.112.112), great TLS encrypted unfiltered choice! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "77.88.8.7,77.88.8.3" ]; then
                 echo "Your Public DNS Server is Yandex (77.88.8.7,77.88.8.3), safe choice with Family Filter! "
             fi
               if [ $( head -n1 $CONF | awk '{print $5}') == "185.228.168.168,185.228.169.168" ]; then
                      echo "Your want leave your Clean Browsing server and use another one?. You need generate new configs later, if you want to use new Server (Start Menu choice: 3 ) "
                      # client choice NEW DNS
                          read -rp "Do you really want to change your DNS Server? (y/n) " -e -i y NEW_DNS
                            if [ "$NEW_DNS" == "y" ]; then
                              echo "Which DNS do you want to use with the VPN?"
                                echo "You recognise encrypted DNS with "TLS" (Port 853 can be potentially blocked through Government etc.) Some Servers use logs, but no one of them log aWireguard IP. "
                              echo "   1) Cloudflare: log: yes 24h, Filter: no, + best speed worldwide"
                              echo "   2) AdGuard: Log: yes, Filter: advertising"
                              echo "   3) DNS.WATCH: Log: no, Filter: no, + great speed for Germany"
                              echo "   4) Quad9: TLS: yes, Log: yes, Filter: no"
                              echo "   5) Yandex Family: TLS:no, Log: yes, Filter: adult"
                              echo "   6) Clean Browsing Family: TLS: yes, Log: yes, Filter: adult and explicit sites, Youtube- safe mode"
                              read -p "DNS [1-6]: " -e -i 1 NEW_DNS

                              case $NEW_DNS in
                                  1)
                                      NEW_DNS="1.1.1.1,1.0.0.1"
                                  ;;
                                  2)
                                  NEW_DNS="176.103.130.130,176.103.130.131"
                                  ;;
                                  3)
                                  NEW_DNS="84.200.69.80,84.200.70.40"
                                  ;;
                                  4)
                                  NEW_DNS="9.9.9.9, 149.112.112.112"
                                  ;;
                                  5)
                                  NEW_DNS="77.88.8.7,77.88.8.3"
                                  ;;
                                  6)
                                  NEW_DNS="185.228.168.168,185.228.169.168"
                                  ;;
                              esac
                            fi
                      # EOF client choices DNS
                fi
                sed "s/185\.228\.168\.168,185\.228\.169\.168/$NEW_DNS/" /etc/wireguard/wg0.conf
               if [ $( head -n1 $CONF | awk '{print $5}') == "10.8.0.1" ]; then
                 echo "Nothing else then own encrypted and logless dedicated DNS Server! "
                 fi
                 ;;
        "Install/add new Client")
            break
            ;;
        "Quit")
              exit
            ;;
    esac
done
7
  • Is 185.228.168.168,185.228.169.168 the literal text you want to replace?
    – Kusalananda
    Jun 7, 2019 at 19:43
  • There's I think a couple of issues here, one, the syntax for find replace in sed is more like sed -i "s/toFind/replace/g" (the g is for global replace), second, comma doesn't mean OR like you perhaps want it to, that role is played by the pipe symbol |. Jun 7, 2019 at 19:44
  • Are you trying to replace both of the DNS addresses in that file with the address in that variable? Jun 7, 2019 at 19:48
  • @Kusalananda Yes exactly. Replace just first Part with 185.228.*
    – Kallados
    Jun 8, 2019 at 3:19
  • @NasirRiley exact
    – Kallados
    Jun 8, 2019 at 3:20

2 Answers 2

0

If you want to replace that string itself with what you have in the variable:

sed "s|185\.228\.168\.168,185\.228\.169\.168|$NEW_DNS|" /etc/wireguard/wg0.conf

You need sed s|string|replacement|. You can use sed s|string|replacement|g to replace it throughout the file. You also need to escape . with a backslash so it doesn't represent a single character. I've removed the -i switch so that the changes are written to standard output so you can make sure that it's what you want. You can put it back into the command once you have confirmed that it is.

If you want to edit the file itself, add the -i switch to your command.

sed -i "s|185\.228\.168\.168,185\.228\.169\.168|$NEW_DNS|" /etc/wireguard/wg0.conf
4
  • Your solution working. Output is like i want. Just the Data are not rewrited. Some bug in my Hierarchy i think
    – Kallados
    Jun 8, 2019 at 11:02
  • @Kallados If you want the data in the file to change, use the -i switch with sed. I didn't include it at first so that you could verify that it does what you want before the changes are made to the file itself. I have updated my answer with the command that will edit the file in place. Jun 8, 2019 at 13:54
  • Yeah! Exactly! Im stupid. Thanks you! It work like a sharm!
    – Kallados
    Jun 8, 2019 at 13:58
  • @Kallados You are welcome. Jun 8, 2019 at 14:51
0

The s command in sed does a substitution of whatever matches the given regular expression with the given replacement text. It's syntax is s/expression/text/flags.

In your case, you want to use the expression

185\.228\.168\.168,185\.228\.169\.168

I have escaped the dots since these are "special" in regular expressions (an unescaped dot matches any single character, but you want to match the actual dots).

The text you want to use as the replacement text is in a variable. This means that the whole sed expression has to be a double-quoted string (since the shell would ignore the variable in a single-quoted string).

Also, you don't need the optional flags with the s command here, so we get

sed "s/185\.228\.168\.168,185\.228\.169\.168/$NEW_DNS/" filename

Don't use sed -i to do a in-place edit until you can see for yourself that the correct result was had.

Note also that since we're injecting the value of your variable into the sed script, this would fail if $NEW_DNS contained any / characters (which I believe would be unlikely that it does in this particular case).

If you, by the comma, mean "either this or that address", then you will have to use an alternation (with |):

sed -E "s/185\.228\.168\.168|185\.228\.169\.168/$NEW_DNS/" filename

I'm using sed -E here since we need to enable extended regular expressions (the | is not part of the basic regular expression syntax which is the default).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .