non-interactive password SSH in JAVA
-
03-03-2021 - |
Question
I have a program that requires cascaded SSH, i.e. it ssh A server and then using same connection ssh B server. Server A is acting as a bridge. I have an instance of shell which is used to ssh first server.
When I am doing ssh user@ipAddress, it asks for password. I tried ssh user@ipAddress\npassword. It doesn't seem to be working.
I cannot use any external tools like ssh-agent or expect. I have no control over the server A.
Is there a way I can provide password as an argument or enter password?
Thanks!!
Solution
You should generate a RSA key on the client
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.
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.
OTHER TIPS
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
The following gem can be used to script a password ssh session
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
echo "yourpassword" | ./sshaskpass.sh ssh user@server.example.com date