![](/img/trans.png)
[英]ssh command to run remote script exist shell on remote server when switching user
[英]When I run a bash script that ssh's into a remote server and run a command like wget it saves on the source not the destination server?
我有以下bash腳本
#!/bin/bash
ssh -o "StrictHostKeyChecking no" ubuntu@$1
cd /home/ubuntu
wget google.com
當我運行它時,。/ ./test.sh
我被ssh'到遠程服務器中,但是除非我按ctrl + d ,否則wget
命令不會運行,然后ssh會話退出並且index.html
文件保存到/home/ubuntu
如何更改此bash腳本,以便可以將index.html
文件保存在遠程服務器上?
只需在ssh
的命令行中指定要執行的命令即可:
ssh -o "StrictHostKeyChecking no" ubuntu@$1 'cd /home/ubuntu; wget google.com'
您的腳本的作用是:
您想要在遠程主機上創建一個腳本並執行它:
run.sh
#!/bin/sh
cd /home/ubuntu
wget google.com
並在本地主機上的腳本中:
ssh -o "StrictHostKeyChecking no" ubuntu@$1 run.sh
您對bash的概念非常錯誤。 Bash不是創建為您進行鍵入的宏的方法。 Bash是一種腳本語言,它將在本地計算機上執行一系列命令。 這意味着它將要運行一條連接到遠程主機的ssh命令。 一旦ssh腳本執行完畢,bash將執行cd,然后執行wget。
您要做的是將命令傳遞到遠程ssh服務器。
#!/bin/bash
ssh -o "StrictHostKeyChecking no" ubuntu@$1 "cd /home/ubuntu; wget google.com"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.