[英]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.