Skeets
Skeets

Reputation: 4828

Find if current user's public SSH key is authorized for SSH on remote host using PHP or Bash

I need a PHP script to execute a script on a remote system.

It's working great where the current user is authorized on the remote system.

However, before running the script, I want to check if the public key on the local system is authorized in the authorized_keys file on the remote system.

Is there a way to do this? I tried executing something like this:

ssh -o ConnectTimeout=5 -o PubkeyAuthentication=yes -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o BatchMode=yes -o ChallengeResponseAuthentication=no host.address.com 2>&1 | grep "Permission denied" 

but it just hangs the console when authentication is successful.

Upvotes: 0

Views: 710

Answers (1)

Romeo Ninov
Romeo Ninov

Reputation: 7235

You can use command like this and grab the exit code:

ssh -q host exit

This will contact host host and execute command exit. Return code 0 mean everything is fine

If you want to grab exit code execute after ssh

EXIT_CODE=$?

and then check the variable

Upvotes: 1

Related Questions