Wednesday, 6 March 2019

SSH and sudo

Following on from my blog Fun with SSH here's a way to run sudo and pass the password on the command line in a script.

First you need to get the password from the user or set the password in a variable.

read -s -p "Enter remote user password: " usrpwd

This prompts the user to enter the password, without echoing to the screen and stores it in $usrpwd.

Now we can ssh to the remote server and run a command via sudo.
Here I'm just running date but you can do anything.

ssh raspberrypi sudo -S <<< $usrpwd date
james@raspberrypi's password:
[sudo] password for james: Wed  6 Mar 15:44:44 GMT 2019
If you incorporate this with my previous blog on SSH you can automate the logging into a remote server and running scripts.

As with all of this stuff the easier you make it for yourself the easier it is for somebody else to destroy your servers.

No comments:

Post a comment

Oracle CPU downloader

Every quarter I have to go through and download numerous patches for the Oracle CPU (Critical Patch Update). You have to view the CPU docume...