2

I have a local bash script that I would like to run on the second ssh server.

Here are the steps:
user$ - local machine
user$ ssh -A ssh - logging into the first ssh
user@ssh01:~$ ssh server2 - logging into the second ssh
user@server2:~$ - logged into the second ssh server

I'm able to run the bash script on the first ssh server from my local machine with the below command:
user$ ssh -A ssh 'bash -s' < script

However, I don't know how to do it on the second ssh server.
when I try to do it from the first ssh it throws an error
user@ssh01:~$ ssh server2 'bash -s' < script
-bash: script: No such file or directory

Please advice

  • To run the script at some remote machine it has to be present over there somehow. You need to copy it over or access it through the net. You certainly could execute it's lines one by one remotely, but that doesn't sound like fun. – vonbrand Aug 12 '19 at 20:15
0
ssh -At ssh 'ssh -At server2 -- bash -s' < script

This will ssh to the second hop and run the script there.

Note that:

  • I'm enclosing the second ssh command in quotes.
  • I'm using -t to pass the terminal through to both ssh sessions.

You can also use the method described here to ssh DIRECTLY to the second hop:

https://www.cyberciti.biz/faq/linux-unix-ssh-proxycommand-passing-through-one-host-gateway-server/

To summarize, add the following to ~/.ssh/config:

Host server2
    HostName server2
    User username
    ProxyCommand ssh username@jumphost nc %h %p
  • Note also that since you use bash -s, the shell's standard input will be the script itself. Reading things from standard input may do unexpected things. – Kusalananda Aug 12 '19 at 20:32
  • It would be better if you could summarise the text that you link to in your answer. – Kusalananda Aug 12 '19 at 20:35
  • @Rafael it worked! The script has run but there was a message before it executed "Pseudo-terminal will not be allocated because stdin is not a terminal." I tried removing '-t' and it worked without any message. Thanks ;) – Kesav Aug 13 '19 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.