Sed escapando "." e usar $variável?

Sed escapando "." e usar $variável?

Eu não entendo essa fuga. Existe algum gerador disponível para isso?

preciso substituir o texto normal (185...) pela variável $NEW_DNS

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

Saída

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

Atualização: eu acho que você pode ver as respostas e seu trabalho, apenas /etc/wireguard/wg0.conf não foi reescrito com novos dados (mas a saída de está correta.

# 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

Responder1

Se você quiser substituir essa string pelo que você tem na variável:

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

Você precisa sed s|string|replacement|. Você pode usá-lo sed s|string|replacement|gpara substituí-lo em todo o arquivo. Você também precisa escapar .com uma barra invertida para que não represente um único caractere. Removi a -iopção para que as alterações sejam gravadas na saída padrão para que você possa ter certeza de que é o que deseja. Você pode colocá-lo de volta no comando depois de confirmar que sim.

Se você quiser editar o próprio arquivo, adicione a -iopção ao seu comando.

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

Responder2

O scomando in sedfaz uma substituição de tudo o que corresponde à expressão regular fornecida pelo texto de substituição fornecido. Sua sintaxe é s/expression/text/flags.

No seu caso, você deseja usar a expressão

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

Eu escapei dos pontos, pois eles são "especiais" em expressões regulares (um ponto sem escape corresponde a qualquer caractere, mas você deseja corresponder aos pontos reais).

O texto que você deseja usar como texto de substituição está em uma variável. Isso significa que toda a sedexpressão deve ser uma string entre aspas duplas (já que o shell ignoraria a variável em uma string entre aspas simples).

Além disso, você não precisa dos sinalizadores opcionais com o scomando aqui, então obtemos

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

Não faça sed -iuma edição no local até que você possa ver por si mesmo que o resultado correto foi obtido.

Observe também que, como estamos injetando o valor da sua variável no sedscript, isso falharia se $NEW_DNScontivesse algum /caractere (o que acredito ser improvável que aconteça neste caso específico).

Se você, pela vírgula, quer dizer "este ou aquele endereço", então terá que usar uma alternância (com |):

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

Estou usando sed -Eaqui porque precisamos habilitar expressões regulares estendidas ( |não faz parte da sintaxe básica da expressão regular, que é o padrão).

informação relacionada