TECHIES WORLD

For Techs.... Techniques.... Technologies....

AWSBashCpanelLinux

How to run a command by switching to root programatically

There might be some requirements to run commands by switching to root programmatically. This article explains one script to achieve this requirement.

#!/bin/bash
PASS=""
COMMAND=""
expect -c "
spawn su -
expect {
\"*assword:\" {
send \"$PASS\r\"
send \"$COMMAND\r\"
interact
}
}
"

Here we need to put the values of root password and the command to be executed.

This script will be helpful in server where remote root login is not allowed.

That's all…