0

I am using ssh on my linux to remote access a second linux. What ssh does is to make my Linux's Terminal the remote Linux's Terminal, and whatever I entered in my Terminal would be executed on the remote Linux. Now I want to use a Shell script to enter a command after the remote access has been established.

I use the following bash to do the ssh part:

#!/bin/sh
ssh user@192.168.178.160

after this it prompts a password which is fine, I enter the password and the I am connected to the remote host. but after that I need to enter some additional commands to be executed automatically ( also from shell script) but simply entering them after the above code lines doesn't work.

Any ideas how to do this?

Lain
  • 1,904
  • 3
  • 17
  • 39
fer y
  • 505
  • 4
  • 18

2 Answers2

4

Tell ssh to send them to the remote shell to be executed.

#!/bin/sh
ssh user@192.168.178.160 << EOF
./foo bar 42
cat baz/quux
EOF
Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • This creates an interactive session. On my machine this means I get a "Pseudo-terminal will not be allocated because stdin is not a terminal." warning and also the multi-line "Welcome to Ubuntu" login message. – John Kugelman Jan 10 '14 at 15:37
  • @Ignacio Vazquez-Abrams great it works, but it executes the second one after the first one is done, adding & to end of lines doesn't work either? can i somehow execute them simultaneously? – fer y Jan 10 '14 at 15:50
0

Put the command at the end:

ssh user@192.168.178.160 echo foobar

To execute multiple commands, use a multi-line string:

ssh user@192.168.178.160 '
    echo foo
    echo bar
'
John Kugelman
  • 307,513
  • 65
  • 473
  • 519