首页 > 解决方案 > 为什么这个命令不从文件中获取输入,尽管重新编排?

问题描述

执行时,cisco anyconnect VPN 客户端从终端获取 VPN IP、密码和其他一些输入。但是,我不是每次都输入,而是在文件中记下值并尝试将文件重定向到 vpn 客户端命令。

/opt/cisco/anyconnect/bin/vpn < vpndetails.txt

但是,该命令似乎忽略了文件重定向并仍然提示输入。这怎么可能?代码是否从其他文件描述符(不是 0)读取并仍然从终端读取?可能吗?

注意:我知道将密码存储在文件中不是一个好习惯,但我现在不在乎。

标签: bashshellinputterminalio-redirection

解决方案


“有没有可能”这个问题的答案是“是”。

如 Chepner 的评论中所解释的那样,anyconnect vpn 的代码可能是 /dev/tty 作为一个有趣的练习,试试这个脚本:

#! /bin/sh 

read -p "STDIN> " a
read -p "TERMINAL> " b  < /dev/tty
read -p "STDIN> " c

echo "Read $a and $c from stdio and $b from the terminal"

并且,例如,ls / | bash this_script.sh

但是,如果您希望在没有密码的情况下使用 Cisco 自动连接,则应调查 Always On with Trusted Network 检测功能和用户证书。

写信/dev/tty希望它会被脚本接收是行不通的:

ljm@verlaine[tmp]$ ls | bash test.sh &
[3] 10558
ljm@verlaine[tmp]$ echo 'plop' > /dev/tty
plop

[3]+  Stopped                 ls | bash test.sh
ljm@verlaine[tmp]$ fg
ls | bash test.sh
(a loose enter is given)
Read a_file and b_file from stdio and  from the terminal

推荐阅读