2

I have a bash script I'm using to connect to a remote server via ssh. That works, however I want the script to immediately pass the command of cd /some/dir after connecting. That doesn't seem to be working. Here's my code:

#!/bin/bash
echo "SSHing.."
ssh -i ~/.ssh/some-site.pem [email protected]
cd /some/dir        
read

How can I have the cd command be executed right after SSH connection is established?

1

4 Answers 4

10

There are two easy ways to execute commands via SSH from inside the script:

1) ssh user@host 'command'

2)

ssh user@host <<<EOF
command1
command2
<...>
commandn
EOF
Sign up to request clarification or add additional context in comments.

3 Comments

#2 gives the error Pseudo-terminal will not be allocated because stdin is not a terminal.
@Click Upvote see stackoverflow.com/questions/7114990/… for a similar problem, I wouldn't explain better.
I was getting a '-bash: line 1: EOF: command not found' error but changed <<<EOF to <<EOF and everything worked. Thanks!
6

Normally you'd just edit your ~/.profile on the remote machine.

If that is not an option, you could do something like this:

ssh -t theserver.com 'cd /some/dir && bash -i'

Comments

0

You can use the following command

ssh user@watevr <the_cmd_to_be_executed>

Comments

0

You can try this :

ssh abc@hostname :/pathto/specific directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.