简体   繁体   English

鱼壳和ssh远程命令的问题

[英]Problems with fish shell and ssh remote commands

I use fish shell on my desktop. 我在桌面上使用鱼壳。

We use many servers running nginx within docker. 我们使用许多在docker中运行nginx的服务器。 I've tried to create a function so I can ssh to the servers and then log into the docker. 我尝试创建一个函数,以便可以SSH到服务器,然后登录到Docker。

The problem is fish is complaining about the $ in the command, but the command is the one to be executed on the remote server (running bash), not on my machine running fish. 问题是fish在命令中抱怨$,但是命令是要在远程服务器(运行bash)上执行的命令,而不是在我的运行fish的机器上执行的命令。 I've simplified the script to make it easier to see. 我简化了脚本,使其更易于查看。

config.fish snippet config.fish代码段

function ssh-docker-nginx
ssh -t sysadmin@10.10.10.10 "sudo bash && docker exec -it $(docker ps | grep -i nginx | awk '{print $1}') bash"
end

Fish error: 鱼错误:

$(...) is not supported. In fish, please use '(docker)'.
~/.config/fish/config.fish (line 59): ssh -t sysadmin@10.10.10.10 "sudo bash && docker exec -it $(docker ps | grep -i nginx | awk '{print $1}') bash"
                                                                                            ^
from sourcing file ~/.config/fish/config.fish
called during startup

Is there a way to get fish to ignore this? 有没有办法让鱼忽略这一点?

You'll want to single-quote that argument. 您需要单引号将该参数引起来。

In double-quotes ( " ) fish will try to expand everything that starts with a $ , so it will see that $( and then print the error for it. But it will also see the $1 in your arguments to awk and expand that. 在双引号( " )中,fish将尝试扩展以$开头的所有内容,因此它将看到$( ,然后为其打印错误。但是在awk的参数中它还将看到$1并将其扩展。

And when you want single-quotes to go to the called command (like here, where you want the argument to awk to be single-quoted because this'll go through bash's expansion), you need to escape the quotes with \\ . 而且,当您希望单引号转到被调用的命令时(例如,在这里,您希望awk的参数被单引号引起,因为这将通过bash的扩展进行),您需要使用\\来对引号进行转义。

Try 尝试

ssh -t sysadmin@10.10.10.10 'sudo bash && docker exec -it $(docker ps | grep -i nginx | awk \'{print $1}\') bash'

Thanks for the great advice and tip above about the single/double quotes. 感谢您提供有关单/双引号的出色建议和技巧。 Unfortunately the escaped quotes in awk did not play nicely being passed to ssh. 不幸的是,awk中的转义引号不能很好地传递给ssh。

After various options, I settled with this approach (which needed force tty): 经过各种选择,我采用了这种方法(需要使用tty来解决):

function ssh-docker-nginx
cat docker-bash.sh | ssh -t -t sysadmin@10.10.10.10 
end

# docker-bash.sh
#!/bin/bash
sudo chmod 777 /var/run/docker.sock
sudo docker exec -it $(docker ps | grep -i nginx | awk '{print $1}') bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM