0

I would like to be able to ssh to a remote host and run some repetitive commands on it before continuing my work.

I have been trying the following command with no luck:

ssh [email protected] 'source /path/to/my/bashrc; ls /logs'  

The commands run fine, however I am not being presented with a shell to continue my work.

What am I missing here?

3 Answers 3

1

You are missing one thing. That if you provide ssh command to run, the session will end as soon as the session ends. This mean as soon as your ls command ends, also ssh will return. To achieve your requested behaviour you need to add the bash/shell to the end and request tty allocation, like this:

ssh -tt [email protected] 'source /path/to/my/bashrc; ls /logs; bash'

or you can substitute the bash command with other shell you prefer.

2

You don't get a shell because when you pass a command to SSH to run remotely, that replaces the shell invocation.

If you want things to happen when you login, you should add those to the account's .bashrc. If the account is shared, well, (1) stop doing that, and (2) there are various ways that the shell can determine who exactly is logging in and do things conditionally.

0

When I have many commands to run on a remote host, I like to use Here Documents[1]

ssh $SERVER << EOF
command 1
command 2
command 3
EOF

[1]http://tldp.org/LDP/abs/html/here-docs.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .