linux命令之sshpass
1.sshpass介绍
linux命令sshpass是用来实现非交换的ssh操作
2.sshpass用法
sshpass [参数] command
sshpass参数
| 参数 | 说明 | 
| -p | 指定明文密码 | 
3.实例
3.1.sshpass安装
默认系统中没有sshpass命令,需要安装sshpass rpm包
命令:
yum install sshpass
[root@logstash ~]# yum install sshpass
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
CentOS7                                                                                | 3.6 kB  00:00:00     
epel                                                                                   | 4.7 kB  00:00:00     
nginx-stable                                                                           | 2.9 kB  00:00:00     
(1/2): epel/x86_64/updateinfo                                                          | 1.0 MB  00:00:02     
(2/2): epel/x86_64/primary_db                                                          | 7.0 MB  00:00:04     
Resolving Dependencies
--> Running transaction check
---> Package sshpass.x86_64 0:1.06-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================
 Package                   Arch                     Version                      Repository              Size
==============================================================================================================
Installing:
 sshpass                   x86_64                   1.06-1.el7                   epel                    21 k
Transaction Summary
==============================================================================================================
Install  1 Package
Total download size: 21 k
Installed size: 38 k
Is this ok [y/d/N]: y
Downloading packages:
sshpass-1.06-1.el7.x86_64.rpm                                                          |  21 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : sshpass-1.06-1.el7.x86_64                                                                  1/1 
  Verifying  : sshpass-1.06-1.el7.x86_64                                                                  1/1 
Installed:
  sshpass.x86_64 0:1.06-1.el7                                                                                 
Complete!3.2.查看sshpass帮助信息
命令:
sshpass -help
[root@logstash ~]# sshpass -help
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename   Take password to use from file
   -d number     Use number as file descriptor for getting password
   -p password   Provide password as argument (security unwise)
   -e            Password is passed as env-var "SSHPASS"
   With no parameters - password will be taken from stdin
   -P prompt     Which string should sshpass search for to detect a password prompt
   -v            Be verbose about what you're doing
   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used
[root@logstash ~]#3.3.使用sshpass实现非交互ssh登录
命令:
sshpass -v -p root ssh -o StrictHostKeyChecking=no root@192.168.10.250
备注:第一次登录时,必须使用-o StrictHostKeyChecking=no,否则会报错
[root@logstash ~]# sshpass -v -p root ssh root@192.168.10.250
SSHPASS searching for password prompt using match "assword"
SSHPASS read: The authenticity of host '192.168.10.250 (192.168.10.250)' can't be established.
ECDSA key fingerprint is SHA256:jDv7l+tTu6PGYgJyRpb8HkkI7Ywrapqas5w628rTGqs.
ECDSA key fingerprint is MD5:7c:ff:bd:04:46:75:88:65:2c:23:08:97:c8:c1:36:13.
Are you sure you
SSHPASS detected host authentication prompt. Exiting.
[root@logstash ~]# sshpass -v -p root ssh -o StrictHostKeyChecking=no root@192.168.10.250
Warning: Permanently added '192.168.10.250' (ECDSA) to the list of known hosts.
SSHPASS searching for password prompt using match "assword"
SSHPASS read: root@192.168.10.250's password: 
SSHPASS detected prompt. Sending password.
SSHPASS read: 
Last login: Thu Nov 23 08:53:47 2023 from 192.168.10.1
[root@lnmp ~]#后续登录时,可以使用sshpass -v -p root ssh root@192.168.10.250
[root@logstash ~]# sshpass -v -p root ssh root@192.168.10.250     #-v可省略
SSHPASS searching for password prompt using match "assword"
SSHPASS read: root@192.168.10.250's password: 
SSHPASS detected prompt. Sending password.
SSHPASS read: 
Last login: Thu Nov 23 09:31:53 2023 from 192.168.10.244
[root@lnmp ~]# hostname
lnmp
[root@lnmp ~]# exit
logout
Connection to 192.168.10.250 closed.
[root@logstash ~]#









