Link to home
Start Free TrialLog in
Avatar of PepperRed
PepperRed

asked on

plink => su: must be run from a terminal

Hello,

I need to reboot a linux server from Windows.

My script doesn't work.

reboot.bat
echo n | putty -ssh -t -noagent  %LOGIN%@%IP% -pw %PASSWORD% -m localshell.sh

Open in new window

localshell.sh

#!/bin/bash 
echo 123456 | su - reboot
ping 127.0.0.1

Open in new window


I can't change linux parameters.
PermitRootLogin No
My user isn't sudoers

i have root password & user password

Thanks for your help !
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

It's

su -  -c reboot

ping ... in your script is useless, it will not be processed after "reboot"

What should these "echo n"  and "echo 123456" be good for?

Finally. I assume you meant to write "plink" instead of "putty" in the code you posted?
I don't think you can pass in plain text for password with su.

Any reason why you are not using sudoer? You can pass in a password argument with plink if you have sudoer setup. One line in batch. For example -

plink -pw bad user@server sudo /usr/bin/reboot
I assume that the "echo 123456" in localshell.sh is for the root password.  Couple of problems with that - su doesn't read the password from stdin, and it can't be run in a pipeline like that.

You need to use expect:
expect <<EOF
spawn su - -c reboot
expect sword
send "123456\r"
expect eof
EOF

Open in new window

Avatar of PepperRed
PepperRed

ASKER

ping command is just to see the error "su: must be run from a terminal"

With plink or putty in my script, the error is the same.

Don't use sudoer is my company policy.

I try su - -c reboot but : "su: must be run from a terminal"

As Mazdajai say it, i think i can't pass in plain text for password with su.

Do you know an other method to reboot a linux from Windows with my constraints

Thanks
Passing plaintext password with su is the problem. I don't see this is possibile with your contriant.
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Back to the original issue ("su: must be run from a terminal"):

The "-t" flag of plink (not putty!) should take care of allocating a pseudo tty.

Apart from all the other good suggestions above: "su - -c ..." should work and ask you for a password if you really used "plink" and its "-t" flag.
Try without "echo 123456 | "  (If you're still interested, that is).
I try yours differents solutions with no success and finally i have success with expect script.

My expect script which i launch from a batch :

RebootLinux.exp

expect <<EOF
spawn "C:\\Program Files\\PuTTY\\plink.exe" -ssh -t -noagent  test@192.168.12.345 -pw password

expect sword
send "su -\r"
       expect {
           "Password: " {send "123456\r"}
       }
expect sword
send "reboot\r"

expect eof

Open in new window


Thanks all