Skip to content
Advertisement

Chain SSH script

Let’s say I have 3 linux servers.

I need a script that will login from s1 to s2 via ssh, then from s2 to s3 via ssh, then check, if there is already exists string in ~/.ssh/authorized_keys from s1 then do nothing otherwise do

echo '${id_rsa.pub from s1}' >> ~/.ssh/authorized_keys_on_s3

s1 and s2 are constant, s3 can be variable. ssh from s1 to s2 already settled up passwordless, ssh from s2 to all s3 already settled up passwordless.

I need to have a possibility to add ip of s3 as input for script.

How can I do this?

Advertisement

Answer

You could use ProxyJump in combination with ssh-copy-id

To connect s3 from s1 over s2

user@s1:~$ ssh -o JumpProxy=s2 s3

To copy your public key to s3 when it doesn’t exist

user@s1:~$ ssh-copy-id -o JumpProxy=s2 s3
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement