execute sudo command on linux from plink.exe[windows]

假装没事ソ 提交于 2019-12-04 07:07:15
user2948547

i know that the question is old but this can help

you can execute plink (putty tools)

plink -v -ssh -m "path/to/commands.txt" -l usertologin -pw PASSWORDSERVER

commands.txt:

echo -e "PASSWORD\n" | sudo -S cat /etc/passwd

sudo -S is to receive the password from stdin and with the echo -e "password\n" is to pass the password (and \n is like to press intro -new line- )

the same way you can execute the passwd command:

> echo -e "OLDPASSWORD\nNEWPASSWORD\nNEWPASSWORD" | passwd

Why are you using plink here? In general, you'd use plink to call a non-interactive script on a remote host. If you want to do interactive stuff, (like edit a file), just ssh to the server (either from your shell, using 'ssh' client on Linux, or by running putty on Windows), then edit the file.

More info on plink, and some examples, can be found here: http://the.earth.li/~sgtatham/putty/0.53b/htmldoc/Chapter7.html

Why Plink ? use SSH instead. Also SSH would need -t option for sudo commands. From ssh man page:

 -t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a remote
         machine, which can be very useful, e.g. when implementing menu services.  Multiple -t options force tty
         allocation, even if ssh has no local tty.

I needed to make some folders and set perms. These blocks worked for me in PowerShell to automate. Whoever up there that is suggesting to just use SSH hopefully has figured out that is currently not the right solution. Reduction of manual process should be everyone's goal.

$null = iex "cmd /c $plinkpath -ssh -l $linuser -pw $linpass $computername `"echo $linpass | sudo -S mkdir $puppetfolder`"" *>&1

$null = iex "cmd /c $plinkpath -ssh -l $linuser -pw $linpass $computername `"echo $linpass | sudo -S chmod o+wx $puppetfolder`"" *>&1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!