Checking SSH credentials in Java on Unix
I have a text file on a unix machine containing an SSH username and password that someone can use to connect to it.
How to check if username and password are correct using Java?
I am trying to connect to SSH on the same computer using Runtime.exec ()?
I can grep for user / etc / passwd. But the password is grayed out.
Any suggestions would be appreciated.
a source to share
Perhaps the question could be expanded to "Checking UNIX credentials from Java" since the mechanism explained is independent of ssh.
Here's my snapshot as a better alternative to Expect (like ProcessBuilder ) without using the network stack overhead:
su -u username -c echo
Just check the exit status to find out the answer. (Bonus points: su is present on all UNIX operating systems and it is even possible on PATH as well as other external authentication system as LDAP).
a source to share
If you have root access, then of course you can read / etc / shadow . I would say that this is the "correct" way to do it.
Also, your frigid way of invoking ssh should be ok I think, but I would use ssh to run a command that, for example, created a random temporary file, then if a file was created (or a command executed), you assume that username / password is correct. Sounds bad, but you can probably make it work in an emergency.
Oh of course frig has a side effect that you are actually logged in as that user. This may or may not be desirable ...!
a source to share
Remember, there is more to UNIX authentication than / etc / passwd and / etc / shadow. For example, they can be authenticated against Kerberos, LDAP, NIS, or any other source. For this reason, I highly recommend learning the PAM wrapper for Java. A quick google came up http://jpam.sourceforge.net/ but I have no experience with it.
Of course sshd can be configured to not use PAM at all, or to allow specific users, etc. If you want to follow the sshd rules exactly then that changes things, but it looks like PAM auth is more than what you want.
a source to share