sshpass非交互式SSH密码验证

sshpass非交互式SSH密码验证

六月 12, 2019

sshpass介绍:
ssh登陆不能在命令行中指定密码,sshpass解决了这一问题。sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码。它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行、文件、环境变量中读取。由于sshpass不能使用yum安装,使用源码包安装。本文档是介绍如何在mac环境下配置sshpass,以此方便服务器工程师们登录服务器。

安装步骤:
1、下载sshpass源码包:

zhaodan$ wget http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz
Connecting to jaist.dl.sourceforge.net|150.65.7.130|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 98362 (96K) [application/x-gzip]
Saving to: ‘sshpass-1.06.tar.gz’

sshpass-1.06.tar.gz 100%[==========================================================>] 96.06K 305KB/s in 0.3s

2019-06-12 16:42:12 (305 KB/s) - ‘sshpass-1.06.tar.gz’ saved [98362/98362]

2、解压

zhaodan$ tar zxvf sshpass-1.06.tar.gz
x sshpass-1.06/
x sshpass-1.06/main.c
x sshpass-1.06/install-sh
x sshpass-1.06/AUTHORS
x sshpass-1.06/config.h.in
x sshpass-1.06/depcomp
x sshpass-1.06/aclocal.m4
x sshpass-1.06/configure
x sshpass-1.06/NEWS
x sshpass-1.06/compile
x sshpass-1.06/ChangeLog
x sshpass-1.06/INSTALL
x sshpass-1.06/Makefile.am
x sshpass-1.06/sshpass.1
x sshpass-1.06/COPYING
x sshpass-1.06/README
x sshpass-1.06/Makefile.in
x sshpass-1.06/configure.ac
x sshpass-1.06/missing

3、进入解压后的sshpass目录编译安装

zhaodan$ cd sshpass-1.06
zhaodan$ ls
AUTHORS INSTALL NEWS compile configure.ac main.c
COPYING Makefile.am README config.h.in depcomp missing
ChangeLog Makefile.in aclocal.m4 configure install-sh sshpass.1

zhaodan$ ./configure
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for a thread-safe mkdir -p… ./install-sh -c -d
checking for gawk… no
……此处省去中间编译输出日志……
checking that generated files are newer than configure… done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

zhaodan$ make && make install
/Library/Developer/CommandLineTools/usr/bin/make all-am
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc -g -O2 -o sshpass main.o
./install-sh -c -d ‘/usr/local/bin’
/usr/bin/install -c sshpass ‘/usr/local/bin’
./install-sh -c -d ‘/usr/local/share/man/man1’
/usr/bin/install -c -m 644 sshpass.1 ‘/usr/local/share/man/man1’

4、尝试实行sshpass命令

zhaodan$ 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

5、查看路径(默认为/usr/local/bin/sshpass)

zhaodan$ which sshpass
/usr/local/bin/sshpass

配置iTerm2:
sshpass1.png

command里的命令格式如下:

/usr/local/bin/sshpass -p ‘YourPassword’ ssh -o StrictHostKeychecking=no YourName@PublicIP -p port

配置完成后测试如下:
sshpass2.png