Home > database >  Testing accessibility through SSH
Testing accessibility through SSH

Time:07-30

I'm trying to write a PowerShell script to test user permissions through SSH, the idea is to connect to a distant machine through ssh, then do su to another user and test if we have the permission to do so. I managed to connect to ssh, and send a su command, but the problem is that when I su, I no longer in control with session, so I get stuck into the su user, my question is there a way to solve this issue ?

Regards

CodePudding user response:

Well, you could use the su command with the -c option, which will run a single command and then return (pick a simple command, like hostname). Thereafter you could check the $? variable. If 0, it worked. If non-zero, it failed.

However, if su prompts for a password, then your script is going to get stuck.

sudo, on the other hand, has both a "command" parameter and a "non interactive" parameter. Would sudo work for you?

# -n = Non Interactive
# -u = Username
# -s = Command to run
sudo -n -u root -s hostname
if [ $? ==  0 ]; then echo "It worked"; else echo "No good"; fi
  • Related