Правильные настройки конфигурационного файла ssh для туннелирования к третьей машине

Решение perl , предполагается, что входной файл отсортирован по c1, c2 и т. Д. Таким образом, сохранение в хэше / массиве не требуется

$ perl -lane '
$F[0] =~ s/\..*//;
if($F[0] ne $p && $. > 1)
{
    print "$p $sum";
    $sum = 0;
}
$sum += $F[2];
$p = $F[0];
END { print "$p $sum" }' ip.txt
c1 -0.618902
c2 -0.000118000000001395
c3 -0.7893993
c4 1.634748
  • -la удалить символы новой строки из ввода и добавить во время печати, разделить строку ввода на пробелы и сохранить в @F массив
  • $ F [0] = ~ s /\..*// удалить все символы из . для первого поля
  • if ($ F [0] ne $ p && $.> 1) если номер строки ввода не является первой строкой, а первое поле не совпадает с предыдущим {{1} }
    • напечатать имя поля и накопленную сумму, очистить переменную суммы
  • В конце, напечатать еще раз, чтобы учесть последнюю запись


Другой способ - не разбивать строку ввода и использовать регулярное выражение для извлечения требуемый ключ и значение:

$ perl -lne '
($k, $v) = /^([^.]+)(?:\S+\s+){2}(\S+)/;
if($k ne $p && $. > 1)
{
    print "$p $sum";
    $sum = 0;
}
$sum += $v;
$p = $k;
END { print "$p $sum" }' ip.txt
c1 -0.618902
c2 -0.000118000000001395
c3 -0.7893993
c4 1.634748
18
15.04.2018, 19:31
1 ответ

Ответ Джакуйе правильный, но поскольку OpenSSH 7.3теперь можно использовать -JProxyJump, что проще. Смотрите мои заметки:

OpenSSH 7.3или выше

Используйте ProxyJump. Как поясняется в руководстве:

-J [user@]host[:port]
Connect to the target host by first making an ssh connection to the jump host and then establishing a TCP forwarding to the ultimate destination from there. Multiple jump hops may be specified separated by comma characters. This is a shortcut to specify a ProxyJump configuration directive.

ProxyJump ~/.ssh/configпример

~/.ssh/config

Host server1
  Hostname server1.example.com
  IdentityFile ~/.ssh/id_rsa

Host server2_behind_server1
  Hostname server2.example.com
  IdentityFile ~/.ssh/id_rsa
  ProxyJump server1

Подключиться к

ssh server2_behind_server1 -v

Добавить -vдля подробного вывода

ProxyJump -JПример командной строки

~/.ssh/config

Host server1
  Hostname server1.example.com
  IdentityFile ~/.ssh/id_rsa

Host server2
  Hostname server2.example.com
  IdentityFile ~/.ssh/id_rsa

Подключиться к

ssh server2 -J server1 -v

Или используйте-o

ssh server2 -o 'ProxyJump server1' -v

OpenSSH 5.4или выше

Используйте ProxyCommandс-W

~/.ssh/config

Host server1
  Hostname server1.example.com
  IdentityFile ~/.ssh/id_rsa

Host server2
  Hostname server2.example.com
  IdentityFile ~/.ssh/id_rsa
  ProxyCommand ssh server1 -W %h:%p

Подключиться к

ssh server2 -v

Или используйте-o

ssh server2 -o 'ProxyCommand ssh server1 -W %h:%p' -v

Оповещение OpenSSH5.4

~/.ssh/config

Host server1
  Hostname server1.example.com
  IdentityFile ~/.ssh/id_rsa

Host server2
  Hostname server2.example.com
  IdentityFile ~/.ssh/id_rsa
  ProxyCommand ssh server1 nc %h %p 2> /dev/null

Подключиться к:

ssh server2 -v

Или используйте-o

ssh server2 -o 'ProxyCommand ssh server1 nc %h %p 2> /dev/null' -v

Источники

-Jдобавлено в OpenSSH 7.3

  • ssh(1): Add a ProxyJump option and corresponding -J command-line flag to allow simplified indirection through a one or more SSH bastions or "jump hosts".

-Wдобавлено в OpenSSH 5.4

  • Added a 'netcat mode' to ssh(1): "ssh -W host:port..." This connects stdio on the client to a single port forward on the server. This allows, for example, using ssh as a ProxyCommand to route connections via intermediate servers. bz#1618
46
27.01.2020, 19:46

Теги

Похожие вопросы