bash 如何将本地文本文件传递给 ssh 命令?

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

How to pass local text file to ssh command?

bashshellunixsshsolaris

提问by MrPickles

I am writing a script in Unix that needs to:

我正在 Unix 中编写一个脚本,它需要:

  1. Create a local text file
  2. Log in via ssh (e.g. user@servername)
  3. Do stuff with local text file
  4. End script
  1. 创建本地文本文件
  2. 通过 ssh 登录(例如 user@servername)
  3. 用本地文本文件做事
  4. 结束脚本

Unfortunately, I can't do this because once the script logs in via ssh, it can't see the text file. So I am trying to pass a local text to the ssh command, so it will see the text file.

不幸的是,我不能这样做,因为一旦脚本通过 ssh 登录,它就看不到文本文件。所以我试图将本地文本传递给 ssh 命令,这样它就会看到文本文件。

Text file is resembles this

文本文件类似于这个

user@servername1
user@servername2
user@servername3
... etc

I looked on a previous answer from SuperUser, but their solution just returns errors for me.

我查看了 SuperUser 之前的答案,但他们的解决方案只是为我返回了错误。

ssh user@servername < text.txt
#stty: : Invalid argument
#sh: user@servername1: not found
#sh[2]: user@servername2: not found
#sh[3]: user@servername3: not found
#...etc

采纳答案by glenn Hymanman

Try:

尝试:

  1. create a local text file
  2. transfer the file to the remote host: scp local_file user@servername:./remote_file
  3. login to the remote host and do stuff: ssh user@servername cat -n remote_file
  1. 创建本地文本文件
  2. 将文件传输到远程主机: scp local_file user@servername:./remote_file
  3. 登录到远程主机并执行以下操作: ssh user@servername cat -n remote_file

回答by Ruslan Gerasimov

if you need to work with local file, not with remote copy, you can try to do next: call sshcommand in a different terminal, then you can continue to work locally and process you local file

如果您需要使用本地文件,而不是远程复制,您可以尝试下一步:ssh在不同的终端中调用命令,然后您可以继续在本地工作并处理您的本地文件

xterm -e "bash -c \"ssh user@servername; exec bash\"" &

xterm -e "bash -c \"ssh user@servername; exec bash\"" &

回答by jozxyqk

As an alternative to @glennHymanman's two connections to copy and then execute...

作为@glennHymanman 复制然后执行的两个连接的替​​代...

Since you can pipe commands to ssh, which is quite similar to your example,

由于您可以将命令通过管道传输到 ssh,这与您的示例非常相似,

echo "ls" | ssh localhost

All you need is something in the middle to interpret the text file. For example...

您只需要中间的东西来解释文本文件。例如...

cat text.txt | sed 's/^/finger /' | ssh user@servername
#or
cat text.txt | ssh user@servername xargs finger


In your example, ssh user@servernameand user@servername1are pretty similar. Just to be clear you didn't want to use those in the ssh command itself?:

在你的例子中,ssh user@servernameuser@servername1非常相似。只是要清楚你不想在 ssh 命令本身中使用那些?:

while read user
do
ssh $user echo hi #ssh to each user/server in turn
done <text.txt