通过 ssh 运行 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2374276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:00:06  来源:igfitidea点击:

Running a Bash script over ssh

bashscriptingssh

提问by Cerin

I'm trying to write a Bash script that will SSH into a machine and create a directory. The long-term goal is a bit more complicated, but for now I'm starting simple. However, as simple as it is, I can't quite seem to get it. Here's my code:

我正在尝试编写一个 Bash 脚本,它将通过 SSH 连接到一台机器并创建一个目录。长期目标有点复杂,但现在我从简单开始。然而,虽然很简单,但我似乎不太明白。这是我的代码:

#!/bin/bash
ssh -T [email protected] <<EOI

# Fix "TERM environment variable undefined" error.
TERM=dumb
export TERM

# Store todays date.
NOW=$(date +"%F")
echo $NOW

# Store backup path.
BACKUP="/backup/$NOW"
[ ! -d $BACKUP ] && mkdir -p ${BACKUP}
echo $BACKUP

exit
EOI

It runs without any explicit errors. However, the echoed $NOW and $BACKUP variables appear empty, and the /backup directory is not created. How do I fix this?

它运行时没有任何显式错误。但是,回显的 $NOW 和 $BACKUP 变量显示为空,并且未创建 /backup 目录。我该如何解决?

回答by Steve Emmerson

The shell on the local host is doing variable substitution on $NOW and $BACKUP because the "EOI" isn't escaped. Replace

本地主机上的 shell 正在对 $NOW 和 $BACKUP 进行变量替换,因为“EOI”没有被转义。代替

 ssh [email protected] <<EOI

with

 ssh [email protected] <<\EOI

回答by hbar

The variables are being evaluated in the script on the local machine. You need to subsitute the dollar signs with escaped dollar signs.

正在本地计算机上的脚本中评估变量。您需要用转义的美元符号代替美元符号。

#!/bin/bash
ssh -T [email protected] <<EOI

# Fix "TERM environment variable undefined" error.
TERM=dumb
export TERM

# Store todays date.
NOW=$(date +"%F")
echo $NOW

# Store backup path.
BACKUP="/backup/$NOW"
[ ! -d $BACKUP ] && mkdir -p ${BACKUP}
echo $BACKUP

exit
EOI

回答by R Samuel Klatchko

Your script is doing substitution on the local host before being sent over.

您的脚本在发送之前正在本地主机上进行替换。

Change your first line to:

将第一行更改为:

ssh -T [email protected] <<'EOI'

This will cause the raw script to get sent over and interpreted on your remote host.

这将导致原始脚本在您的远程主机上被发送和解释。

If you wanted a mix (so for example, if you wanted the datecommand executed on your local host, you should leave ssh line unchanged and quote the individual command):

如果您想要混合(例如,如果您想要在date本地主机上执行命令,您应该保持 ssh 行不变并引用单个命令):

ssh -T [email protected] <<EOI

# Execute the date command on the local machine.  The assignment still
# happens on the remote machine
NOW=$(date +"%F")

# Quote your $ so that the replacement happens on the remote machine
echo $NOW

回答by jmrenouard

How to run a local script over SSH

如何通过 SSH 运行本地脚本

Synopsis:

概要:

Script execution over SSH without copying script file. You need a simple SSH connexion and a local script.

通过 SSH 执行脚本,无需复制脚本文件。您需要一个简单的 SSH 连接和一个本地脚本。

Code:

代码:

#!/bin/sh
print_usage() {
        echo -e "`basename 
NOW=`date +"%F"`
` ssh_connexion local_script" echo -e "Remote executes local_script on ssh server" echo -e "For convinient use, use ssh public key for remote connexion" exit 0 } [ $# -eq "2" ] && [ != "-h" ] && [ != "--help" ] || print_usage INTERPRETER=$(head -n 1 | sed -e 's/#!//') cat | grep -v "#" | ssh -t $INTERPRETER

Examples:

例子:

  • ssh-remote-exec root@server1 myLocalScript.sh #for Bash
  • ssh-remote-exec root@server1 myLocalScript.py #for Python
  • ssh-remote-exec root@server1 myLocalScript.pl #for Perl
  • ssh-remote-exec root@server1 myLocalScript.rb #for Ruby
  • ssh-remote-exec root@server1 myLocalScript.sh #for Bash
  • ssh-remote-exec root@server1 myLocalScript.py #for Python
  • ssh-remote-exec root@server1 myLocalScript.pl #for Perl
  • ssh-remote-exec root@server1 myLocalScript.rb #for Ruby

Step by step explanations

分步说明

This script performs this operations: 1° catches first line #! to get interpreter (i.e: Perl, Python, Ruby, Bash interpreter), 2° starts remote interpeter over SSH, 3° send all the script body over SSH.

此脚本执行以下操作: 1° 捕获第一行 #! 要获得解释器(即:Perl、Python、Ruby、Bash 解释器),2° 通过 SSH 启动远程交互器,3° 通过 SSH 发送所有脚本主体。

Local Script:

本地脚本:

Local script must start with #!/path/to/interpreter - #!/bin/sh for Bash script - #!/usr/bin/perl for Perl script - #!/usr/bin/python for Python script - #!/usr/bin/ruby for Ruby script

本地脚本必须以 #!/path/to/interpreter 开头 - Bash 脚本的 #!/bin/sh - Perl 脚本的 #!/usr/bin/perl - Python 脚本的 #!/usr/bin/python - #! /usr/bin/ruby 用于 Ruby 脚本

This script is not based on local script extension but on #! information.

此脚本不是基于本地脚本扩展名,而是基于 #! 信息。

回答by Pablo Santa Cruz

Try:

尝试:

##代码##