简体   繁体   中英

How to cd on remote server via SSH bash script?

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 xxx@yyy.com
cd /some/dir        
read

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

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

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'

您可以使用以下命令

ssh user@watevr <the_cmd_to_be_executed>

你可以试试这个:

ssh abc@hostname :/pathto/specific directory

I think you are missing backtick

#!/bin/bash
echo "SSHing.."
ssh -i ~/.ssh/some-site.pem xxx@yyy.com
ssh `cd /some/dir`        
read

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM