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 [email protected]

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?

0

2 Answers 2

4

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

#!/bin/sh
ssh [email protected] << EOF
./foo bar 42
cat baz/quux
EOF
Sign up to request clarification or add additional context in comments.

2 Comments

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.
@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?
0

Put the command at the end:

ssh [email protected] echo foobar

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

ssh [email protected] '
    echo foo
    echo bar
'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.