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

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

Related

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.

PSEXEC: not recognising remote command

I am able to execute basic operation by triggering batch script on remote location using following command.
psexec -e -h -s -u User -p pass \\10.0.0.240 C:\test.bat
But when the test.bat file is calling other program specific script like somepy.py then I am getting error on master batch file that these are not internal command.
Master batch file in Host computer:
CODE
psexec -e -h -s -u user -p pwd \\10.0.0.240 C:\Users\Desktop\TEST\test.bat
Command inside test.batlocated in remote PC:
cd C:\Users\Desktop\TEST1
impact -batch test_impact_warp_AP.cmd
`pause`
waitfor /t 5 StartNow
REM wait for 5second
echo "Run python script for Warp"
cd C:\Users\Desktop
call warp-python.bat
ipconfig /all
ping google.com
Command inside warp-python.bat present in remote machine:
set PYTHONPATH=C:\Users\Desktop\Python_Reference
cd C:\Users\Desktop\Python_Reference\examples\PYTHON_SCRIPT
python t_capture.py
When I executed the test.bat script directly then warp-python.bat as well as test_impact_warp_AP.cmd executes perfectly without any error.
But when i try to execute test.bat from remote location then *python* and *impact* commands are not recognised. and gives following error:
'Impact' is not recognised as an internal or external Command
But ipconfig/all and ping command is executed perfectly in remote PC
What am I missing in the command line argument such that psexec in not able to executed the command in remote location
Add the psexec.exe to your PATH variable in the environment variable or
give the full path of psexec.exe in your command.
Example:
c:\users\test\Desktop\psexec.exe -e -h -s -u User -p pass \10.0.0.240 C:\test.bat
Make sure C:\test.bat is present in your remote machine.

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

Open SSH tunnel via Plink and run R Scripts via command line batch file

I am trying to open an SSH tunnel via Plink in the command line to run a few R scripts and I cannot get the R scripts to run and when they do run the tunnel was not created, so the connection to my database crashes.
The Plink code looks like this:
plink.exe -ssh [username]#[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
Followed by the code to run the R scripts
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
I cannot seem to get the tunnel to stay open to run the R script. However, I can open the tunnel separately and run the code.
I assume you have the two commands in a batch-file one after another like:
plink.exe -ssh [username]#[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
Then indeed the R.exe is never executed as the plink.exe runs indefinitely.
You have to run the commands in parallel:
You can use start command to run plink.exe in the background.
Use killtask command to kill the background plink process after the R.exe finishes.
You should probably also pause a little before you run R.exe to allow the Plink to establish the tunnel.
rem Open tunnel in the background
start plink.exe -ssh [username]#[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
rem Wait a second to let Plink establish the tunnel
timeout /t 1
rem Run the task using the tunnel
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R
rem Kill the tunnel
taskkill /im plink.exe

windows, Putty, pscp combined in to a single batch file, is it possible?

I'm having trouble getting an answer to my question, in laymens terms. It is probably my lack of knowledge on the subject so, I'm dumbing down the question. I have a windows machine that I run the putty tool from and connect to a linux box. I run " killall /bob/bin/myfile.out " then close putty then type in a cmd prompt pscp.exe myfilet.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out . Can someone show me how to combine these into a single windows batch file? thank you
You could use the free command line tool Plink to run commands on external servers via SSH.
#echo off
Plink root#192.168.1.1 "killall /bob/bin/myfile.out"
pscp.exe myfilet.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out || echo an error occurred when copying the file.
the command after || on the second line will only run if an error level is set by the previous command.
I can't add comments yet, but can you elaborate on how you login with putty, but not do the exact same thing with plink? Plink not only accepts all the same options as putty, but if you have a saved session in putty, you can access it from plink. Without any subcommands, plink should essentially make you CMD shell look like a crude putty window, with subcommands, it will execute them and return:
C:\Users\riglerjo>plink savedputtysession
Using username "rigler".
# hostname
s9-chicago.accountservergroup.com
-bash-3.2$ exit
logout
Run the remote command as an option on plink:
C:\Users\riglerjo>plink savedputtysession hostname
s9-chicago.accountservergroup.com

Resources