【问题标题】:Using aliases in a heredoc that ssh a remote server在 ssh 远程服务器的 heredoc 中使用别名
【发布时间】:2018-12-17 17:44:16
【问题描述】:

我的 ~/.bashrc 中有一个函数,定义如下

function sshpushg() {
    iro <<EOF      #iro is sshpass -p *** ssh addressOfServer
    ssh computer01 #connect to my remote computer
    Work           
    st
    add
    git commit -m "pushing automatically by ssh"
    pushg
    EOF
}

在我的远程帐户中,我的 ~/.bashrc 文件有以下几行

alias Work=' cd ~/Documents/Work'
alias st='git status'
alias add='git add -A'
alias pushg='git push origin master'    

我希望函数 sshpushg 自动添加、提交并将我的工作文件夹中的所有内容推送到 github,但是当我运行命令时出现错误:

-bash: line 1: Work: command not found
-bash: line 2: st:  command not found
-bash: line 3: add: command not found

然后一堆错误表明这不是 git 存储库,因为未执行 Work 并且我不在正确的目录中。

如何在我的函数 sshpushg 中使用在我的远程帐户上设置的别名?显然,我可以在没有别名的情况下重新键入所有命令,但有没有办法不这样做?

【问题讨论】:

  • 你为什么要连接到addressOfServer,然后再连接到computer01
  • @che 因为 adressOfServer 将用作与其他计算机的连接点,因为它没有很多内存/CPU。但是是的,实际上我可以直接在 addressOfServer 上运行,但在我的实验室中,我们不应该使用 addressOfServer 来做任何其他事情,而不是连接到我们的计算机

标签: linux ssh heredoc


【解决方案1】:

当您使用输入脚本运行 ssh 时,它不会创建终端并且其 shell 不会运行 ~/.bashrc。您可以通过使用ssh computer01 bash -i 强制使用交互式 shell 来解决此问题,但这会尝试将输入文件用作终端并给您带来另一组错误:

$ ssh my-server bash -i <<< 'aliastest'
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
che@my-server ~ $ aliastest
alias works
che@my-server ~ $ exit

您可能可以通过以某种方式伪造终端来解决此问题,但最简单的方法是写出命令,或者通过在服务器上创建脚本然后运行它,类似于

#!/bin/bash
cd ~/Documents/Work
git status
git add -A
git push origin master

然后运行它

sshpass -p *** ssh addressOfServer ssh computer01 ~/path/to/my/script.sh

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2013-10-06
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多