Batch script to log in SSH server on Windows - shell

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

Related

ssh batch script windows : unable to send my commands

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.

How to pass user input automatically using plink.exe

I need to login to an SSH session using password, and then execute the user input values for a particular account.
For example:
PLINK.EXE -ssh ***** -l ***** -pw *****
I
am able to login, now what I need to do is enter below values:
Please select account to logon: "U"
Press RETURN to continue or OFF to cancel session - "RETURN"
There are similarly this kind of user inputs needed. Is there a way I store the "*" values in a text file and load them using Plink.
I tried:
PLINK.EXE -ssh ***** -l ***** -pw ***** -m C:\input.txt
This does not seems to be working.
Expectation: Passing all the user input i.e. "U", "RETURN" .... using Plink or any other PuTTY tool.
Appreciate your help!
The -m switch is used to execute commands in shell. You cannot use it to provide inputs for the executed commands.
But as Plink is a console application, you can use an input redirection to provide input:
plink.exe -ssh ... -l ... -pw ... < C:\input.txt

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.

How to set Vncserver in bash script (redhat6)

I am using a script to automatically set up a computer. I need to assign a password to the vnc server for the user, which is normally done using the vncserver command. However, it prompts for the user to enter and re-enter their password, neither of which the script is capable of doing.
So, how can I set up the VNC password without an interactive prompt?
Please try following bash script sample:
#!/bin/sh
vncpasswd << EOF
123456
123456
EOF

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.

Resources