Execute sftp commands on remote server using batch file and PuTTY - batch-file

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.

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

Running a HTA file remotely using PSTools

Its almost certain that HTA files are obsolete, but i've found that they are much better than net send / msg.
I'm trying to run a HTA file on a remote machine using PSTools, but instead of it running, it brings back a broken window:
Running the HTA file using CMD (locally) works perfectly though.
My PsExec line:
PsExec.exe -accepteula -i -d \\itwall cmd 'mstha \\intranet\Downloads\VisitorSystemNewMessage.hta asd'
I even tried to run the HTA from a Batch file, but the exact same thing happens.
Any ideas?
It's because the account running the command cannot interact with the session of the remote user.
Use the -s switch to run the HTA using the system account of the remote computer.
Also, you shouldn't need to run cmd. You should be able to just specify mshta.exe then your arguments.
PsExec.exe -accepteula -s -i -d \\itwall mshta.exe \\intranet\Downloads\VisitorSystemNewMessage.hta asd
Edit: To illustrate that this is not an HTA issue. Run the following command:
PsExec.exe -accepteula -i -d \\itwall notepad.exe
Notice you'll have the same black window showing.

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

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

putty from a batch file and a script?

I have a batch file that opens putty just fine.
c:\putty.exe root#192.168.12.34 -pw boyhowdy. But to make this work for me I need to understand how to include a script of a command so it will run under the putty tool. Like mount –o remount,rw / . Or is this something I can do with a tool called pscp. I am a nube to these tools and really could use some guideance. I have a bunck of these scripts and would really like to automate them. Thankyou
If your goal is to execute shell commands remotely through putty, you should probably look at plink (putty without the gui, in other words an ssh client for windows) and then apply the standard here-doc techniques to plink.
plink is part of the putty collection and is also downloadable from the same page as putty.
If you want to execute a local script you would use
plink user#host -m local_script.sh
For instance. Assuming you're running on some Windows box (fyi the putty suite also runs on Linux) and want to execute a batch of commands on some remote box you would create a shell script on your local machine (say mount.sh) and run it like this:
C:\> type mount.sh
whoami
hostname
/usr/sbin/mount -t iso9660 -o ro /dev/cdrom /mnt
/usr/sbin/mount | grep mnt
C:\> plink remoteuser#remotehost -pw secret -m mount.sh
remoteuser
remotehost
/dev/cdrom on /mnt type iso9660 (ro)
Also, it's probably better to copy your public key over so that the password is not coded in some batch file.
Finally, be aware that not all environment variable defined in an interactive shell process will be available in the remote shell process. You might need to 'source' some profile script at the beginning of your script.

Resources