java sftp输入密码_SFTP 自动输入密码短语

如果你的创建的密钥对设置了密码短语,使用ssh或者sftp命令时,提示手动输入密码短语。

这时候如果是自动化的脚本作业,应该如何写呢?

网上的方案有使用sshpass,但是如果服务器是AIX系统,很不幸,大概率是找不到这个命令的。

ssh.com官网的ssh-add有提及用到密码短语的密钥使用情况。

Keys with Passphrases

If the key being added has apassphrase,ssh-addwill run thessh-askpassprogram to obtain the passphrase from the user. If theSSH_ASKPASSenvironment variable is set, the program given by that environment variable is used instead.

Some people use theSSH_ASKPASSenvironment variable in scripts to provide a passphrase for a key. The passphrase might then be hard-coded into the script, or the script might fetch it from a password vault. However, use of passphrases in this manner does not eliminate the need for properkey lifecycle managementand rotation. Instead, we recommend looking at thePrivX On-Demand Access Manageron how to completely eliminate SSH keys in such applications and replace them by short-lived certificates issued on-demand based on centrally managed access policies.

大意就是,可以用SSH_ASKPASS这个环境变量来实现自动输入密码短语。但是,官方不推荐因为这种取巧的方法并不能满足密钥的生命周期管理和轮替。顺便带出另一款产品——PrivX。

我不管,我就用取巧的方法。这里有一个帖子提到如何利用SSH_ASKPASS。

大概如下:

printf "#!/bin/bash\necho password\n" > /usr/bin/sshpass.sh

printf "ls -lrt \n quit \n" > /opt/operation.batch

export SSH_ASKPASS=/usr/bin/sshpass.sh

setsid sftp unixforum@localhost < operation.batch

可以看到,sshpass.sh不过是将密码硬编码输出。这里有一个关键点,setsid。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值