0

I'm attempting to shutdown a RHEL7 linux computer remotely from a windows computer in a non-interactive fashion using powershell and plink.

I can send commands and retrieve the result like so:

$plinkPath = "C:\somePath"
$ipAddress = "192.168.111.1"
$username = "userName"
$password = "password"
$resultPath = "C:\somewhere"
$resultFile = "someFile.txt"
& "$plinkPath\plink.exe" @("$ipAddress", "-l", "$username", "-pw", "$password", "(pwd)" | Out-File "$resultPath\$resultFile"

The command above prints the working directory in linux and saves it to file. However, I can't send a shutdown command like this because the command must be run as root on this computer. I believe there should be some syntax like the below to run a sudo command in a similar way:

& "$plinkPath\plink.exe" @("$ipAddress", "-l", "$username", "-pw", "$password", "(sudo ... shutdown -h now)") | Out-File "$resultPath\$resultFile"

Is there some way to send a shutdown command with sudo using this method?

Thanks.

Lee Exothermix
  • 316
  • 2
  • 3
  • 14
  • You are correct - I left out the -pw switch, and also left off a closing parenthesis. I don't see an option to edit my post now. Everything within the inner parenthesis is the command that gets sent over ssh to the linux computer. Maybe those are not needed? – Lee Exothermix Jan 05 '21 at 15:15
  • I receive an error: "no tty present and no askpass program specified". I'm going to read up on this error now. – Lee Exothermix Jan 05 '21 at 15:37
  • Yessir, I had to configure sudoers using visudo to allow a shutdown command for the user without prompting for password. – Lee Exothermix Jan 08 '21 at 17:43

1 Answers1

0

Make sure that the user can indeed run sudo commands. The issue might be that the user that is trying to run the command is requested to insert the sudo password. In the sudoers file insert <YOUR_USERNAME> ALL=(ALL) NOPASSWD

  • Good thinking. I did verify that sudo works directly from the linux computer. However, I don't know a method to provide the password over plink. – Lee Exothermix Jan 05 '21 at 15:20
  • If you add the lines that I suggested there will be no need to supply the password at runtime. The user will be able to run sudo command without providing the password. – Eduard Luca Jan 10 '21 at 10:56