【shell】CentOS7.x上sshpass的安装使用

一、背景

 

OpenSSH 自带的 ssh 客户端程序(也就是 ssh命令)默认不允许你以非交互的方式传递密码。

很多时候我们在确定安全的情况下,对ssh的交互登录"深恶痛绝",所以就需要进行免密登录。

ssh登陆不能在命令行中指定密码,sshpass的出现,解决了这一问题。

sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码(本机known_hosts文件中有的主机才能生效)。它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环境变量中读取。

ssh命令, 没有指定密码的参数. 以至于在脚本中使用ssh命令的时候, 必须手动输入密码, 才能继续执行. 这样使得脚本的自动化执行变得很差, 尤其当ssh对应的机器数很多的时候, 会令人抓狂.使用非交互式样有两种方式: 一种借助expect脚本, 一种借助sshpass来实现。

 

 

二、sshpass的安装方式

 

方式一:编译安装

# wget https://nchc.dl.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz

# tar  zxf sshpass-1.06.tar.gz

# cd sshpass-1.06

# ./configure  --prefix=/usr/local/

# make && make install

 

方式二:rpm安装

# yum -y install sshpass

# yum info sshpass

【shell】CentOS7.x上sshpass的安装使用

注:如果用rpm的方式安装ansible,sshpass是作为依赖安装的。

【shell】CentOS7.x上sshpass的安装使用

 

 

三、sshpass的使用

 

# sshpass -h

【shell】CentOS7.x上sshpass的安装使用

 

# sshpass -p "123456" ssh  -o 'StrictHostKeyChecking=no'   [email protected]

# sshpass -p "123456" ssh  -o 'StrictHostKeyChecking=no'   [email protected]   -v

 

注:第一次登陆务必加上  -o 'StrictHostKeyChecking=no'  选项,或者保险起见,每次加上

 

【shell】CentOS7.x上sshpass的安装使用

 

【shell】CentOS7.x上sshpass的安装使用

 

 

四、参考

 

non-interactive ssh password auth

https://nchc.dl.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz

 

sshpass 用法举例

https://www.cnblogs.com/nf01/articles/10756100.html

 

Linux ssh命令详解

https://www.cnblogs.com/ftl1012/p/ssh.html

 

sshpass的使用方法

https://www.cnblogs.com/misswangxing/p/10637718.html