PuTTY command line user input - batch-file

I am trying to write a batch file to automate a PuTTY script. Right now the script successfully logs into the SSH server with this line
putty.exe -ssh user#ipaddress -pw password -m commands.txt > log.txt
My problem is that once it's logged in the server prompts me to hit "Ctrl+y to begin" afterwards it asks me to press "Enter". How would I imitate that in my commands.txt file?

Use Plink (PuTTY command-line tool).
It is a console application, hence it allows input redirection.
Create a text file that contains Ctrl+Y character followed by Enter character. I.e. 0x19 and 0x0A. And then follow with your commands.
Then you can do
plink -ssh user#ipaddress -pw password < input.txt

Related

how to run one command to multiple servers with plink in batch

so far what I have is
plink -batch root#1.1.1.1 -pw password COMMAND & plink -batch root#1.1.1.1 -pw password COMMAND
the problem is after the first command starts the batch waits for a response from the server to stop the session and go to the next command, I cant seem to find a way to get around this, I'm looking to have it send the command and immediately start the next command on the next server
You can have the server itself execute the command in the background. So you can use the same techniques as with OpenSSH ssh, as you will be using server-side functionality, unrelated to the local SSH client you are using:
Getting ssh to execute a command in the background on target machine
So something like this:
plink -batch root#1.1.1.1 -pw password "nohup COMMAND > foo.out 2> foo.err < /dev/null &"
Or use local techniques to execute the command asynchronously:
Running Windows batch file commands asynchronously
So something like this:
start "title" /b plink -batch root#1.1.1.1 -pw password COMMAND

automate the following steps

I have created an .bat file with the following command with a text file which contain "sudo su - oradev2"(C.txt).
cd C:\Users\chakraborty_sayantan\Desktop
plink -ssh serverA -l username -pw password -m "C:\Users\username\Desktop\c.txt" -t
The command above completes the first step of logging into a DB and the c.txt file has the instruction to enter into sudo mode. However, post this there is an authentication. Is there a way to automate the step of entering the password? Any thoughts?
Procedure to automate
login
password
sudo su - oradev
password
echo $ORACLE_SID
sqlplus / as sysdba
create user identified by
default tablspace
The above steps need to get automate using a bat file which consist of plink/putty.
-Sayantan
You can make a password and username txt file and maybe use the IF NOT EXIST Command to check if that user and pass file exists and set some commands up to create that file
Hope this is what you meant :)

Layout of command output executed automatically in PuTTY from a batch file is broken

I have followed this question to build a batch file to run the PuTTYwith my username and password:
How to run a command file in PuTTY using automatic login in a command prompt?
#echo off
START putty.exe -ssh [domain] -l [username] -pw [password] -m code.txt
#echo
And the PuTTY will try to run the code.txt file, which have the following code:
HResults -p -e "???" sil -e "???" sp -L labels/test lib/words3 results/*.rec
read
It will show a matrix. I try to run the batch file, it is able to open PuTTY, login and run the command in text file. But the output in PuTTY terminal is a mess. The layout of output is fine, when I doing those things manually. Is that mean some kind of setting is missing? It's not making any sense a batch file will change the output of another application...... Thanks
The -m switch imply a non-interactive session. While, when logging in manually, an interactive mode is used by default.
It may fundamentally affect output of some applications.
Try forcing the interactive mode using the -t switch:
START putty.exe -ssh [domain] -t -l [username] -pw [password] -m code.txt

Execute sftp commands on remote server using batch file and PuTTY

I have to transfer a file from one server to another. I log in to the first one with PuTTY and then type this:
sftp -v -oIdentityFile=path username#host
cd path
put file
Everything works perfectly! Now I'm trying to do it with a batch file. In the .bat I have:
putty.exe -ssh host1 -l username1 -pw password1 -m script.txt
In the script.txt file:
sftp -v -oIdentityFile=path username2#host2
cd path
put file
exit
It connects to the server number two but then it stops. The prefix sftp> does not appear and it does not read the following lines. Do you have any suggestion?
The remote shell takes the commands and executes them one by one. So it executes the sftp, waits for it to exit (what it never does) and only then it would execute the cd command (but in shell, not in the sftp), the put (failing as that's not a shell command), etc.
If your intention was to simulate typing the commands as on a terminal, use Plink and input redirection.
The Plink (PuTTY command-line connection tool) is a tool from PuTTY package that works like PuTTY, but it is a console, not GUI, application. As such it can use input/output redirection. And anyway, the Plink is the tool to automate tasks, not PuTTY.
plink.exe -ssh host1 -l username1 -pw password1 < script.txt
For more details, see How to type commands in PuTTY by creating batch file? on Super User.

How to use make scp prompt for password when executed with Plink

I want to write a batch script which will run scp command in remote server, using Plink from my Windows.
The code is:
plink.exe -ssh 10.168.5.15 -l username -pw passwd scp abc.txt xuser#10.168.10.108:/home
What I'm expecting is a prompt for password for the remote machine to where I'm coping file in my batch window. When I run above code my batch window shows, "Permission Denied, please try again" three times and "lost connection" message. I also tried scp with PasswordAuthentication=yes and BatchMode=yes but no use.
And I'm sure that Plink is interactive.
Any suggestions please?
The PLink runs the command (scp) in a non-interactive session by default. The scp won't even prompt for password in the non-interactive session for security reasons.
Use the -t switch to force the interactive session.
plink.exe -t -ssh 10.168.5.15 ...
Quoting section 3.8.3.12 of PuTTY/Plink manual:
3.8.3.12 -t and -T: control pseudo-terminal allocation
The -t option ensures PuTTY attempts to allocate a pseudo-terminal at the server, and -T stops it from allocating one. These options are only meaningful if you are using SSH.
These options are equivalent to the ‘Don't allocate a pseudo-terminal’ checkbox in the SSH panel of the PuTTY configuration box (see section 4.24.1).

Resources