ssh登陆不能在命令行中指定密码。sshpass的出现,解决了这一问题。sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码。
它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环境变量中读取。
下载如下:
https://sourceforge.net/projects/sshpass/
解压,安装
[[email protected] opt]# tar zxvf sshpass-1.06.tar.gz
[[email protected] opt]# cd sshpass-1.06/
[[email protected] sshpass-1.06]# ./configure --prefix=/opt/sshpass
[[email protected] sshpass-1.06]# make
[[email protected] sshpass-1.06]# make install
经过以上步骤,sshpass安装完成,输入命令 sshpass 如出现如下提示即安装成功:
[[email protected] bin]# ./sshpass
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
用法:
-p password
#后跟密码
[[email protected] ~]
# sshpass -p 目标机器的root密码 ssh [email protected]地址
-f filename
#后跟保存密码的文件名,密码是文件内容的第一行。
[[email protected] ~]
# cat 1.txt
123456
[[email protected] ~]
# sshpass -f 1.txt ssh [email protected]地址
-e
#将环境变量SSHPASS作为密码
[[email protected] ~]
# export SSHPASS=123456
[[email protected] ~]
# sshpass -e ssh [email protected]地址
日常用法
sshpass -p user_password ssh -o StrictHostKeyChecking=no [email protected]地址
【-o StrictHostKeyChecking=no 表示远程连接时不提示是否输入yes/no】
使用sshpass远程免密,在远程主机上执行shell命令,如下远程执行命令:touch /opt/file.txt
sshpass -p user_password ssh -o StrictHostKeyChecking=no [email protected]地址 touch /opt/file.txt
[注:shell命令要和sshpass命令写在一行]
原文地址:https://www.cnblogs.com/zy6103/p/12153166.html