Fork me on GitHub

sshpass笔记

 

sshpass简介

ssh登录的时候使用的是交互式输入,不能预先在命令行使用参数指定密码,sshpass就是为了解决这个问题的。sshpass提供非交互式输入密码的方式,可以用在shell脚本中自动输入密码。

比如在执行ssh、scp、rsync等命令时可以使用sshpass来实现预先指定密码。

 

安装

CentOS使用yum安装即可:

yum install sshpass

 

使用

-p指定明文密码

使用-p选项在ssh登录的时候指定明文密码:

sshpass -p nopasswd ssh root@localhost

使用-p选项在拷贝文件的时候指定密码:

sshpass -p nopasswd scp root@localhost:~/free_buff.sh /usr/bin

指定明文密码的方式简单方便,但是密码容易暴露,比如登录到某台机器上执行一个命令:

sshpass -p nopasswd ssh root@localhost 'sleep 1024'

然后使用ps -ef查看:

image

哈,密码打码了...

 

-e从环境变量读入密码

如果不想明文指定密码的话,可以先将密码设置到一个叫做SSHPASS的环境变量中,然后使用-e选项它就会自己去这个变量中取密码。

比如:

export SSHPASS=nopasswd
sshpass -e ssh root@localhost 'sleep 10'

sshpass会自动去名为SSHPASS的变量中读到密码nopasswd,然后使用这个密码来作为后面ssh的登录密码。

 

-f从文件读取密码

使用-f指定从一个文件中读取密码,比如:

sshpass -f /root/shell/passwd_file ssh root@localhost 'echo ok'

sshpass指定时会去读取/root/shell/passwd_file的内容,然后将读到的内容当做后面ssh的密码输入。

 

从标准输入读取密码

如果没有使用-f/-d/-p/-e中的任何一个,那么将从stdin读入密码。

sshpass  ssh root@localhost 'echo ok'

 

帮助手册

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

 

 

.

posted @ 2017-12-04 23:13  CC11001100  阅读(703)  评论(0编辑  收藏  举报