ssh batch script windows : unable to send my commands - windows

I wrote a little script, to automate some actions on a server
#echo off
SET /P host= enter host adress :
#echo off
plink.exe -ssh user#%host% -P 22 -pw password -batch -m "C:\path\to\my_commands.txt"
pause
I can connect with this script, no problem with that. But when I run it, it's not sending what I wrote in my_commands.txt
I've tried to wrote my commands like this :
plink.exe -ssh user#%host% -P 22 -pw password -batch mkdir /my/path
I've tried with or without quotes for the path to my_commands.txt
In both ways, cmd keep telling "bash : mkdir : command not found"
It look like cmd is trying to take the "my_commands.txt" as parameter.

Related

Using Plink, how to auto-input password into Ubuntu ssh?

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?
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.

Plink working directory

MY SERVER: BitVise SSH server for windows
My Client: plink.exe
I cannot for the life of me figure out how to change a directory when using plink.exe and execute a script in that directory.
I am doing something like this to try and send a command to switch a directory and execute a script:
C:\plink.exe -ssh 10.10.10.10 -P 22 -l user -pw password cd C:\sample && install.bat
However, my command fails each time I run this, stating that install.bat does not exist. If I use putty, connect with the GUI, and run the cd C:\sample && install.bat command, everything works as expected.
Is it possible to tell plink what directory to connect to?
Since & is a command separator in cmd, did you put your command in quotes? I would bet that it is trying to run cd C:\sample on the server and install.bat locally.
C:\plink.exe -ssh 10.10.10.10 -P 22 -l user -pw password "cd C:\sample && install.bat"

Send multiple commands with Plink and SSH

I'm using a batch file which contains the following information:
plink.exe -ssh loginuser#192.168.0.1 -pw intec -m t.txt
m.txt contains user and password example:
su
intec
Result:
C:\Plink>plink.exe -ssh loginuser#192.168.0.1 -pw intec -m t.txt
Password:_
The problem is that the cursor is waiting for the password.
How do I send the password to pass this step?
If you want to pass the password, I believe it needs to use the -pw option followed by the actual password, not a filename.

Automatic login using PUTTY.EXE with Sudo command

I am using below command to open putty through windows command prompt:
PUTTY.EXE -ssh -pw "mypass" user#IP -m C:/my.sh -t
Where my.sh mentioned in above command file contains:
sudo su - rootuser
After executing the command, putty console is opened and it prompts for password.
Is there any way where I can provide this password automatically without typing it?
There's a bit of a horrible workaround using Expect and embedding a password.
This is a bad idea.
As an alternative:
Configure sudo to allow NOPASSWD.
Login directly as root using public-private key auth.
Both these introduce a degree of vulnerability, so should be used with caution - but any passwordless auth has this flaw.
Finally, after struggling for almost whole day, I got the way to get this working.
Below command can be executed from windows machine:
PLINK.EXE -t -ssh -pw "password" user#IP /home/mydir/master.sh
master.sh file is located on remote machine. And this file contains below command to execute script with sudo command without prompting password.
echo password | sudo u user -S script.sh
Here, password should be replaced with your password. user should be replaced with your actual user and script.sh is the script on remote machine that you want to fire after sudo login.

Batch script to log in SSH server on Windows

I'm trying to make a batch script to log in into a SSH server and execute a few commands. The start command is:
plink -ssh user#99.99.999.99
Then I need to enter the User Name and Password like the image:
If I have the 'User Name' and 'Password' in two variables, how do I use them when it asks me for?
[EDIT]
last try was this:
(echo username
echo mypassword) | plink -ssh user#99.99.999.99
Output:
User Name:username
mypassword
Password:
The batch didn't "pressed" enter after inputing the username.
Try
plink -ssh -l $USER -pw $PASSWD
after setting environment variables
set USER=name
set PASSWD=secret
see http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter7.html

Resources