8

If I execute the plink command

plink  user@192.168.1.12   sudo nano /etc/hosts

Upon typing the password when prompted, I am getting

sudo: no tty present and no askpass program specified

Sorry, try again.

sudo: no tty present and no askpass program specified

Sorry,try again.

sudo: no tty present and no askpass program specified

Sorry, try again.

sudo: 3 incorrect password attempts

How to execute the sudo command from plink? Or is there any alternatives?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Paul
  • 1,176
  • 3
  • 12
  • 27
  • See also [Automatically provide the root user password to “su” executed using Plink from a batch file](https://stackoverflow.com/q/66716112/850848). – Martin Prikryl Apr 07 '21 at 06:21

4 Answers4

13

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
McK
  • 653
  • 2
  • 7
  • 24
user2948547
  • 326
  • 3
  • 8
  • 3
    Though this solves the problem at hand, I would seee it as bad security practice. If anyone on the remote host issues a "ps" command at the right time they will have your password in plain text. edit: typos – TobiM Nov 22 '18 at 15:10
  • THX it's working! `@echo on set user=login set host=ip set pswd=passwd plink.exe -v -ssh "%user%@%host%" -pw "%pswd%" "echo -e '%pswd%\n' | sudo -S df -h" pause` – oobe Aug 30 '22 at 18:52
3

Why are you using a plink here? In general, you'd use a 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.

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
Mark J. Bobak
  • 13,720
  • 6
  • 39
  • 67
  • 2
    I need to connect to linux from c sharp code, so i tried using plink. Actually i need to invoke a script. I just given an example. – Paul Nov 27 '13 at 17:59
3

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.
slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123
  • 1
    Thanks. if i use -t option, then for the sudo commands, its prompting for the password. Is there a way to send the password in the command with having to prompt. – Paul Nov 27 '13 at 18:04
  • I am not aware of way to send password in the command. In stead, on your destination server, in sudo, you can configure user with `NOPASSWD` option. So that he won't be promoted for any password for any sudo command that he runs. Read sudo documentation for more information on NOPASSWD. – slayedbylucifer Nov 27 '13 at 18:17
0

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