如何在 telnet 会话中运行脚本?

wen*_*nzi 5 telnet script bash

我想使用 telnet 连接到远程主机,只是没有用户名/密码验证

telnet remotehost
Run Code Online (Sandbox Code Playgroud)

然后我需要输入一些命令进行初始化

然后我需要重复以下命令:

cmd argument
Run Code Online (Sandbox Code Playgroud)

参数是从本地文件中读取的,在这个文件中有很多行,每一行是一个参数,运行一个“cmd参数”后,远程主机会输出一些结果它可能会输出一行“OK”或输出很多行,其中之一是字符串“ERROR”,我需要根据结果做一些事情。

基本上,脚本是这样的:

initialization_cmd  #some initial comands
while read line
do    
  cmd $line
  #here the remote host will output results, how can I put the results into a variable?
  # here I want to judge the results, like
  if $results contain "OK";then
       echo $line >>good_result_log
  else
       echo $line >> bad_result_log
  fi     
done < local_file

 the good_result_log and bad_result_log are local files
Run Code Online (Sandbox Code Playgroud)

有可能吗?谢谢!

注意:我无法控制 B,我只能在 B 上运行初始 cmds 和 cmd $line

Wil*_*sum 1

如果您手动启动 telnet,这实际上并不是您可以做的事情 - 但是,我会亲自编辑您的脚本文件并从其中启动 telnet,将 telnet 的输出行处理为 bash 中的输入。

我无法真正给出示例,因为我只通过 Windows 脚本而不是 Bash 完成此操作,但是,我知道这将轻松实现您想要的。