【发布时间】:2020-06-27 20:26:37
【问题描述】:
我为我的办公室使用编写了一个 bash 脚本,以使用 sshpass 和 ssh 命令从设备中获取一些信息。正如我们所知,sshpass 允许使用-p 选项在命令行上传递密码,这使得密码可见,因此我想要一个需要在屏幕上提示用户输入的密码。
下面的脚本工作正常,但我需要在屏幕上提示密码以供用户输入。请告知如何做到这一点,因为我已经搜索过但没有得到任何具体答案。
#!/bin/bash
#
# timestamp to be attached to the log file
TIMESTAMP=$(date "+%Y%m%d%H%M%S")
# logfile to collect all the Firmware Version of C7000 components
LOGFILE="/home/myuser/firmware_version-${TIMESTAMP}.log"
for host in $(cat enc_list);
do
echo "========= $host =========";
sshpass -p my_password timeout -t 20 ssh -o "StrictHostKeyChecking no" $host -l tscad show firmware summary ;
done | tee -a "${LOGFILE}"
【问题讨论】:
-
把这个放在
for循环之前:read -r -s -p "Password:" my_password并用-p "$my_password"替换-p my_password。但是,从安全的角度来看,我并不推荐。 -
@Cyrus,是的,我之前尝试过,但我想知道是否有更好的方法。再次感谢您的评论。
-
查看 sshpass 的选项
-e以避免在命令行中输入密码。 -
谢谢@Cyrus,是的,我已经检查了
-e选项,但read -r -s -p看起来更好,而不是硬编码和导出它。