首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码作为sshpass命令的输入

问密码作为sshpass命令的输入
EN

Stack Overflow用户
提问于 2020-06-27 10:49:57
回答 3查看 1K关注 0票数 1

我已经为我的办公室使用编写了bash脚本,以便使用sshpass和ssh命令从设备中获取一些信息。正如我们所知道的,sshpass允许使用-p选项在命令行上传递密码,这使得密码可见,因此我想要一个需要提示的密码,作为屏幕上的用户输入。

下面的脚本工作正常,但我需要一个密码在屏幕上提示用户输入。请建议如何做到这一点,因为我已经搜索了周围,但没有得到任何具体的答案。

代码语言:javascript
复制
#!/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}"
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-27 11:43:30

避免命令行中的密码:

代码语言:javascript
复制
read -r -s -p "Password:" SSHPASS
export SSHPASS
sshpass -e timeout ... ssh ...

来自man sshpass

-e:密码来自环境变量"SSHPASS“。

票数 2
EN

Stack Overflow用户

发布于 2020-06-27 11:05:19

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

来自手册页

sshpass是为使用称为“键盘交互”密码身份验证的模式运行ssh而设计的实用工具,但使用的是非交互模式。

以及进一步

Sshpass在一个专用的tty中运行ssh,使它以为是从交互式用户那里获得密码。

票数 0
EN

Stack Overflow用户

发布于 2020-06-27 12:26:37

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

代码语言:javascript
复制
#!/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

演示:

代码语言:javascript
复制
$ ./OA_FirmwareCheck.sh
Please Enter password below:

PTENC
Built: 04/06/2018 @ 06:14
OA Bay Number:  1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62608976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档