我已经为我的办公室使用编写了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}"发布于 2020-06-27 11:43:30
避免命令行中的密码:
read -r -s -p "Password:" SSHPASS
export SSHPASS
sshpass -e timeout ... ssh ...来自man sshpass
-e:密码来自环境变量"SSHPASS“。
发布于 2020-06-27 11:05:19
为什么要首先使用sshpass,因为它是一个实用程序,唯一的目的是绕过ssh的默认行为,这似乎是您想要实现的?
来自手册页
sshpass是为使用称为“键盘交互”密码身份验证的模式运行ssh而设计的实用工具,但使用的是非交互模式。
以及进一步
Sshpass在一个专用的tty中运行ssh,使它以为是从交互式用户那里获得密码。
发布于 2020-06-27 12:26:37
只是为了其他用户,他们可能正在寻找相同的解决方案,我是在不久的将来。
#!/bin/bash
# OA_FirmwareCheck.sh
# 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"
# read is a builtin command of the Bash shell. It reads a line of text from standard input.
# -r option used for the "raw input", -s option used for Print the string prompt,
# while option -s tells do not echo keystrokes when read is taking input from the terminal.
# So, altogether it reads password interactively and save it to the environment
read -rsp $'Please Enter password:\n' SSHPASS
export SSHPASS
for host in $(cat enc_list);
do
echo "========= $host =========";
sshpass -e timeout -t 20 ssh -o "StrictHostKeyChecking no" $host -l tscad show firmware summary ;
done | tee -a "${LOGFILE}"
# at last clear the exported variable containing the password
unset SSHPASS演示:
$ ./OA_FirmwareCheck.sh
Please Enter password below:
PTENC
Built: 04/06/2018 @ 06:14
OA Bay Number: 1https://stackoverflow.com/questions/62608976
复制相似问题