密码作为 sshpass 命令的输入
password as a input with sshpass command
我写了一个 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}"
你为什么首先使用 sshpass
,因为它是一个唯一目的是规避 ssh
的默认行为的实用程序,这似乎是你想要实现的?
来自man page:
sshpass is a utility designed for running ssh using the mode referred to as "keyboard-interactive" password authentication, but in non-interactive mode.
进一步
Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.
避免在命令行中使用密码:
read -r -s -p "Password:" SSHPASS
export SSHPASS
sshpass -e timeout ... ssh ...
来自man sshpass
:
-e
: The password is taken from the environment variable "SSHPASS".
只是为了其他可能在不久的将来寻找与我相同的解决方案的用户。
#!/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: 1
我写了一个 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}"
你为什么首先使用 sshpass
,因为它是一个唯一目的是规避 ssh
的默认行为的实用程序,这似乎是你想要实现的?
来自man page:
sshpass is a utility designed for running ssh using the mode referred to as "keyboard-interactive" password authentication, but in non-interactive mode.
进一步
Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.
避免在命令行中使用密码:
read -r -s -p "Password:" SSHPASS
export SSHPASS
sshpass -e timeout ... ssh ...
来自man sshpass
:
-e
: The password is taken from the environment variable "SSHPASS".
只是为了其他可能在不久的将来寻找与我相同的解决方案的用户。
#!/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: 1