如何通过 SSH bash 脚本在远程服务器上进行 cd?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14703262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cd on remote server via SSH bash script?
提问by Click Upvote
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:
我有一个 bash 脚本,用于通过 ssh 连接到远程服务器。这是有效的,但是我希望脚本在连接后立即传递 cd /some/dir 的命令。这似乎不起作用。这是我的代码:
#!/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?
如何在建立 SSH 连接后立即执行 cd 命令?
回答by KBart
There are two easy ways to execute commands via SSH from inside the script:
有两种简单的方法可以从脚本内部通过 SSH 执行命令:
1) ssh user@host 'command'
1) ssh user@host 'command'
2)
2)
ssh user@host <<<EOF
command1
command2
<...>
commandn
EOF
回答by rtytgat
Normally you'd just edit your ~/.profile on the remote machine.
通常你只需在远程机器上编辑你的 ~/.profile 。
If that is not an option, you could do something like this:
如果这不是一个选项,您可以执行以下操作:
ssh -t theserver.com 'cd /some/dir && bash -i'
回答by Antarus
You can use the following command
您可以使用以下命令
ssh user@watevr <the_cmd_to_be_executed>
回答by Rudra
You can try this :
你可以试试这个:
ssh abc@hostname :/pathto/specific directory

