Sobre escape com tubo SSH

4

Eu quero executar o comando awk com parâmetros longos, assim:

ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && $1!="Destination" {printf "%-15s %-20s\n", $1, $2}'|sort -f "

mas existem alguns erros:

syntax error The source line is 1. The error context is NR!=1 && NF>=6 && >>> != <<< awk: Quitting The source line is 1.

Então, como posso corrigir isso?

    
por CatWin 13.04.2014 / 07:00

2 respostas

10

use o here-docs para contornar todas as desagradáveis citações de subshell:

ssh you@host <<-\SSH
    awk -f 3<<\AWK /dev/fd/3
        awk script
        as many lines as you like
        "$vars and quotes" are only evaluated by awk
    #END
    AWK
    "$vars and quotes" are only evaluated by remote shell
    echo 'single quotes and all'
    rest of ssh script
#END
SSH
    
por 13.04.2014 / 07:39
5

Use barras invertidas para proteger $ e " dentro do comando remoto:

ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && \!=\"Destination\" {printf \"%-15s %-20s\n\", \, \}'|sort -f "
    
por 13.04.2014 / 15:14