Reputation: 3229
Usually in a shell script you can write commands such as
command1
command2
command3
and they will be executed one after another.
I want to write a Unix shell script to SSH onto a server and THEN execute commands on that server. For example
ssh [email protected]
ls
I try to do this as shown above but ls doesn't display output. What am I doing wrong?
Upvotes: 0
Views: 191
Reputation: 3229
Although the other answers to my question have been fantastic, I ended up using a small modified version. Thought I'd share it with the forum for anyone who's interested.
ssh -t [email protected] "
command1
command2
command3
bash -l"
advantages: it doesn't log you out!
Many thanks to anishsane and John Kugelman
Upvotes: 2
Reputation: 1755
Use Passwordless SSH as mentioned in this guide.
And then your script will contain:
ssh [email protected] command1
ssh [email protected] command2
ssh [email protected] command3
Upvotes: 0
Reputation: 362147
You can feed in commands to ssh on the command line or on stdin. If you have just one command to execute, do:
ssh [email protected] ls
If you have multiple commands, use heredoc syntax to pass them in via stdin:
ssh [email protected] <<END
command1
command2
command3
END
Upvotes: 8