【问题标题】:Variables in TelnetTelnet 中的变量
【发布时间】:2016-06-21 03:25:18
【问题描述】:

我创建了一个通过 telnet 登录到服务器的 bash 脚本。现在我想cat $var >> /path/on/server/a.log. $var 在进入 telnet 会话之前获取之前在 bash 脚本中指定的内容。不知何故,输出 "sh: ---------------------------: not found" 是在 telnet 会话中创建的。

我猜这是因为我的 file.log 有多行。第二行是“---------------”。

这是我的脚本:

#!/bin/bash
var=$(cat /path/to/local/file.log)
sleep 1
sh << EOF | telnet 192.168.178.72
sleep 2
echo user
sleep 1
echo pass
sleep 5
# echo "$var" >> /path/on/server/file.log (Can't Test because saving the var is not working)
sleep 5
EOF

我的最终目标是将 file.log 放到服务器上。连接的唯一方法是通过 telnet。客户端不支持 ssh 或 rsync 或任何东西。

编辑: 更多信息:

脚本不必在客户端上运行。我也可以在服务器上运行它。我更喜欢在客户端上运行脚本。

【问题讨论】:

标签: bash telnet


【解决方案1】:

我会推荐这样的方法:

#!/bin/bash
(
echo "telnet 192.168.178.72"
sleep 2
echo user
sleep 1
echo pass
sleep 5
echo "cat >> /path/on/server/file.log"
sleep 5
cat /path/to/local/file.log
) | sh

请注意,这可能会导致在远程服务器上执行/path/to/local/file.log 的内容。我强烈建议您考虑这样做的安全隐患,或许可以找到更好的方法。

【讨论】:

  • : not found sh: Raspbian: not found 不适合我。我用 raspbian 从我的 KathreinUfs910 连接到我的 Raspberry。我猜这就是为什么它用 raspbian 显示一些东西的原因。我知道这不安全,但它在我自己家里的网络中。我创建了 file.log,我知道里面会有什么。
  • 这 telnet 进入远程主机,然后等待该会话终止,然后再运行 sleep 2 等。我认为这不是 OP 所要求的。
  • @chepner:谢谢,已修复。
猜你喜欢
  • 2019-12-07
  • 2013-05-15
  • 2020-08-03
  • 2014-09-01
  • 2021-12-11
  • 1970-01-01
  • 2018-11-05
  • 2021-01-25
  • 2020-05-12
相关资源
最近更新 更多