首页 > 解决方案 > 密码作为 sshpass 命令的输入

问题描述

sshpass我已经为我的办公室使用编写了一个 bash 脚本,以使用命令从设备中获取一些信息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}"

标签: linuxbashshellsshpass

解决方案


避免在命令行中输入密码:

read -r -s -p "Password:" SSHPASS
export SSHPASS
sshpass -e timeout ... ssh ...

来自man sshpass

-e:密码取自环境变量“SSHPASS”。


推荐阅读