简体   繁体   English

JAVA中的非交互式密码SSH

[英]non-interactive password SSH in JAVA

I have a program that requires cascaded SSH, ie it ssh A server and then using same connection ssh B server. 我有一个程序,需要级联SSH,即它ssh一个服务器,然后使用相同的连接ssh B服务器。 Server A is acting as a bridge. 服务器A充当桥梁。 I have an instance of shell which is used to ssh first server. 我有一个shell实例,用于ssh第一个服务器。

When I am doing ssh user@ipAddress, it asks for password. 当我在做ssh user @ ipAddress时,它要求输入密码。 I tried ssh user@ipAddress\\npassword. 我试过ssh user @ ipAddress \\ npassword。 It doesn't seem to be working. 它似乎没有工作。

I cannot use any external tools like ssh-agent or expect. 我不能使用任何外部工具,如ssh-agent或期望。 I have no control over the server A. 我无法控制服务器A.

Is there a way I can provide password as an argument or enter password? 有没有办法可以提供密码作为参数或输入密码?

Thanks!! 谢谢!!

You should generate a RSA key on the client 您应该在客户端上生成RSA密钥

ssh-keygen

and put the public key in the authorized key in the authorized_key folder on the server to be able to connect to the server without a password. 并将公钥放在服务器上authorized_key文件夹中的授权密钥中,以便能够在没有密码的情况下连接到服务器。

An step by step guide is given here . 这里给出了一步一步的指导。

Edit : If you have no access to the server, use a ssh-library for Java as decrived in this question . 编辑 :如果您无法访问服务器,请使用此问题中描述的 Java的ssh库。

Either you can setup the password-less login or you can just feed the password like described here : 要么你可以设置的密码登录较少或者你可以只给像描述的密码在这里

ssh -t -t <machine> <<EO_MY_INPUT
<password>
date # (or whichever is the command to get date/time)
exit
EO_MY_INPUT

Since you cannot use expect or ssh-keygen 因为你不能使用expect或ssh-keygen

The following gem can be used to script a password ssh session 以下gem可用于编写密码ssh会话的脚本

http://www.debian-administration.org/articles/587 http://www.debian-administration.org/articles/587

#!/bin/bash
# Copyright (C) 2008 John S. Skogtvedt <jss at bzz.no>
# Licence: GNU GPL v3 or later at your option

if [ -n "$SSH_ASKPASS_FD" ]
then
        read password <&$SSH_ASKPASS_FD
        echo "$password"
        exit 0
elif [ $# -lt 1 ]
then
        echo "Usage: echo password | $0 <ssh command line>" >&2
        exit 1
fi

export SSH_ASKPASS=$0
export SSH_ASKPASS_FD=4
[ "$DISPLAY" ] || export DISPLAY=dummy:0
read password

exec 3<&0
# write password 100 times to make repeated ssh connections work
for x in $(seq 100)
do
  echo "$password"
done | exec setsid "$@" 4<&0 0<&3

Save the above as asksshpass.sh can chmod +x 将上面保存为asksshpass.sh可以chmod +x

echo "yourpassword" | ./sshaskpass.sh ssh user@server.example.com date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM