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

 

 

.

转载于:https://www.cnblogs.com/cc11001100/p/7979544.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值