【问题标题】:password as a input with sshpass command密码作为 sshpass 命令的输入
【发布时间】:2020-06-27 20:26:37
【问题描述】:

我为我的办公室使用编写了一个 bash 脚本,以使用 sshpassssh 命令从设备中获取一些信息。正如我们所知,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 看起来更好,而不是硬编码和导出它。

标签: linux bash shell sshpass


【解决方案1】:

在命令行中避免密码:

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

来自man sshpass

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

【讨论】:

  • 谢谢,Cyrus 再次在这里给出答案,我认为一旦我们完成了脚本,我们需要unset SSHPASS,所以我们可以在最后添加它,这就是我正在做的. +1。
  • 再次感谢我将其标记为接受,因为这就是我的方法,我最终使用read -rsp $'Please Enter password below:\n' SSHPASS
【解决方案2】:

您为什么首先使用sshpass,因为它是一个实用程序,其唯一目的是规避ssh 的默认行为,这似乎是您想要实现的目标?

来自man page

sshpass 是一个实用程序,旨在使用称为“键盘交互”密码身份验证的模式运行 ssh,但在非交互模式下。

还有更多

Sshpass 在专用的 tty 中运行 ssh,使其误以为是从交互式用户那里获取密码。

【讨论】:

  • pguenther,这不是关于 why i am using it 的问题,我知道使用 sshpass 的本质,我更感兴趣的是它可以让用户输入密码的基础。
  • 那我没明白你的意思,对不起。什么反对使用 ssh 而不使用 sshpass
  • 由于某种原因,我无法在没有其他选项来获取数据的旧设备上安全地连接,因此需要使用 sshpass,目前这只适用。 . 单独使用 ssh 时,我们需要为每次出现我们不想要的服务器登录输入密码,希望你明白我的意思。
  • 现在我想我明白你的意思了。在为您迭代解决方案之前是否使用过一次 bash 的 read 命令? read PASSWORD 会将一行用户输入放入 bash 变量 $PASSWORD。见here
  • 非常感谢您的理解,是的,我现在很难使用 :-)
【解决方案3】:

只是为了其他用户,他们可能会在不久的将来寻找与我相同的解决方案。

#!/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

【讨论】:

    猜你喜欢
    • 2010-12-19
    • 2023-03-25
    • 1970-01-01
    • 2013-11-24
    • 2012-07-15
    • 2022-01-08
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多