Linux常用命令-sshpass

命令

sshpass

描述

noninteractive ssh password provider
非交互式输入ssh密码

用法

1
sshpass [-f|-d|-p|-e] [-hV] command parameters

选项

1
2
3
4
5
6
7
Options:
-p 指定密码
-f 指定密码文件,默认读取文件的第一行
-d number is a file descriptor inherited by sshpass from the runner. The password is read from the open file descriptor.
-e 读取环境变量SSHPASS指定的密码
-P 指定密码提示语(password prompt),默认为"assword:",即可以匹配"Password:" and "password:"
-v 显示详情

注意

需要安装sshpass软件包,该命令仅用于临时通过帐号密码的方式远程登录,不推荐长期使用,可能会暴露用户密码,推荐通过密钥认证的方式进行ssh登录

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ sshpass -V
sshpass 1.06

$ yum install sshpass

# 使用ssh命令提示输入用户密码
$ ssh usera@localhost
usera@localhost\'s password:
# 借助sshpass命令指定用户密码,无需交互输入
$ sshpass -vp centos ssh usera@localhost
SSHPASS searching for password prompt using match "assword"
SSHPASS read: usera@localhost\'s password:
SSHPASS detected prompt. Sending password.
SSHPASS read:

$ sshpass -p centos ssh usera@localhost
# 直接执行远程主机命令后退出
$ sshpass -p centos ssh usera@localhost "hostname"
node11