1

I use plink.exe for automatisation of git commands and this works fine. But i need to use this with servers with login of sudo

plink.exe -ssh [email protected] -m commands.txt

my commands.txt file for example:

echo -e "MYPASSWORD\n" | sudo -S -i
cd /home/www/argentium.ru
git checkout HEAD~3

and output looks like loged in, but git executes as if it works without login of sudo:

[sudo] password for argentium: fatal: Unable to create '/home/www/argentium.ru/.git/index.lock': Permission denied

With help putty.exe works fine

сentos-7-x86_64-minimal @ 23.11.2016
-bash-4.2$ sudo -i
[sudo] password for argentium:
[root@stilnoeserebro ~]# cd /home/www/argentium.ru
[root@stilnoeserebro argentium.ru]# git checkout HEAD~3
Note: checking out 'HEAD~3'.

1 Answer 1

1

The command sudo -S -i reads the password and commands from standard input.

In echo -e "MYPASSWORD\n" | sudo -S -i standard input is from the pipe, which returns a new-line, then end-of-file after the password is read: the remaining commands are run in the normal shell.

There are a couple of ways round: what is closest to what you have coded is to use a here-document:-

sudo -S -i <<EOF
MYPASSWORD
cd /home/www/argentium.ru
git checkout HEAD~3
EOF

Alternatively, you can add all the commands to the input stream:-

echo -e "MYPASSWORD\ncd /home/www/argentium.ru\ngit checkout HEAD~3" | sudo -S -i

Note that I have tested this on a normal bash shell, as I don't have a set-up where I can easily test with plink.

1
  • The alternative variant is look obviously. I could have guessed... Now i understand the sense. But the first variant is convenient. Thank you very much! Commented Oct 9, 2018 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .