Telnet 中的变量

Variables in Telnet

我创建了一个 bash 脚本,它通过 telnet 登录到服务器。现在我想 cat $var >> /path/on/server/a.log. $var 在进入 telnet 会话之前在 bash 脚本中指定。以某种方式在 telnet 会话中创建了输出 "sh: ---------------------------: not found"。

我想这是因为我的 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 或任何东西。

编辑: 更多信息:

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

我会推荐这样的方法:

#!/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 的内容。我强烈建议您考虑这对安全的影响,也许会找到更好的方法。