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
How to pass local text file to ssh command?
提问by MrPickles
I am writing a script in Unix that needs to:
我正在 Unix 中编写一个脚本,它需要:
- Create a local text file
- Log in via ssh (e.g. user@servername)
- Do stuff with local text file
- End script
- 创建本地文本文件
- 通过 ssh 登录(例如 user@servername)
- 用本地文本文件做事
- 结束脚本
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:
尝试:
- create a local text file
- transfer the file to the remote host:
scp local_file user@servername:./remote_file
- login to the remote host and do stuff:
ssh user@servername cat -n remote_file
- 创建本地文本文件
- 将文件传输到远程主机:
scp local_file user@servername:./remote_file
- 登录到远程主机并执行以下操作:
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 ssh
command 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@servername
and user@servername1
are pretty similar. Just to be clear you didn't want to use those in the ssh command itself?:
在你的例子中,ssh user@servername
和user@servername1
非常相似。只是要清楚你不想在 ssh 命令本身中使用那些?:
while read user
do
ssh $user echo hi #ssh to each user/server in turn
done <text.txt