2

I'm currently setting up a batch file to ssh from a Windows machine into a Ubuntu machine and issue a series of commands. I'm using plink, and I'm using the -m argument to pass a .txt file with a list of commands.

The batch file code that runs through cmd:

set PATH=c:\path\to\plink.exe
plink.exe -ssh -t user@ipaddress -pw <psw> -m c:\path\to\textFile\commands.txt

The commands.txt code:

sudo -s      #access the root login
<root psw>   #enter the password for the root login
command-1    #issue a command in linux as root
command-2    #issue a command in linux as root
command-3    #issue a command in linux as root

The issue I'm running into is that when I run this batch file, the output within command prompt still prompts the user to manually enter the password. Is there a means to input the password form the next line of the commands.txt file? Or does this process require something else?

1
  • 1
    You could consider modifying the /etc/sudoers file on the Ubuntu machine to include NOPASSWD for this user. Commented Sep 19, 2018 at 21:46

1 Answer 1

2

As even your question says, the file commands.txt specified by -m switch should contain commands. A password is not a command.

Moreover, the commands in the file are executed one-by-one. The sudo (had it worked) would execute an interactive shell session and wait for a user input (commands). Only once the sudo exits, the following commands (command-1, etc) are executed.


Automating password input for sudo is generally a bad idea. If you need to run some commands that require root privileges, a better solution is to associate a dedicated private key with the commands in sudoers file. And then use sudo and the private key in Plink.


Anyway, to automate an input (of a password in this case) to a command, you need to use an input redirection. The same is true for commands to be executed within (not after) sudo.

Like:

(
    echo passwod
    echo command-1
    echo command-2
) | plink.exe -ssh -t user@ipaddress -pw <psw> sudo -s

As now there's only one real top-level command - sudo, I'm specifying it directly on Plink command-line, instead of using -m switch. Functionally, it's identical.

Not the answer you're looking for? Browse other questions tagged or ask your own question.