Reputation: 267049
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?
Upvotes: 2
Views: 17952
Reputation: 1621
You can use the following command
ssh user@watevr <the_cmd_to_be_executed>
Upvotes: 0
Reputation: 497
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'
Upvotes: 6
Reputation: 1598
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
Upvotes: 10