Creating batch file from a cmd command - batch-file

Made a simple batch file that pings google.com and saves/updates the results to a txt file as it is performing the ping command.
cmd command that works:
ping google.com -t > C:\Scripts\PingTestResults.txt
I then copied this to a batch file and it is showing as this when running it,
ping google.com -t 1>C:\Scipts\PingTestResults.txt
This results in the ping timing out
I tried removing spaces in between -t and the > as well as changing location of where the batch file was running from.

Related

Which IP is being used in #file psexec

I am running a command of PSEXEC, which is reading IPs from a text file using
Psexec #IPlist.txt -u adc -p P#$rCL cmd /c WHERE /r D:\ %n%>res.txt
how can I know that which IP is currently being used from #IPlist.txt, can I store it in variable or can I pass it to another batch file?
You can iterate the list in the text file "manually" with a batch file, calling psexec multiple times, or instead of that "WHERE" command you could write a batch file that is called by psexec that then determines the IP address of the PC it is currently running on (whilst then running on the client PC, using eg. netsh).

Access denied while redirecting output from plink to local file

I am trying to redirect my plink command file output (That executes a script on UNIX server) to another file. But I am getting Access Denied issue while redirecting output.
Batch file:
cd "C:\Program Files\PuTTY"
plink -t w44gq8asd#USA7061UX02.wv.abcd.net -pw T#12Ts "PATH=/bin:/usr/bin:/usr/local/bin /opt/siebel/w44gq8asd/a.sh" > output.txt
cmd
Your working directory is in Program Files, where you do not have a write permissions, so it's only natural, that you are getting "Access denied".
You have to write the output file to a different folder:
plink ... > C:\path\where\you\can\write\to\output.txt
If you wanted to write to the path, where your batch file is, what about not changing the working directory in the first place:
"C:\Program Files\PuTTY\plink" ... > output.txt
(and remove the cd command)

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.

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

Resources