Unable to enter a response automatically using batch file - batch-file

I am trying to use .bat file (Windows) to automate a registration process.
Below is the content of my batch file:
C:
cd C:\Program Files (x86)\SSL_Client
admin -r
echo n
echo mithun
echo 12339-asdda-wewew
It works until admin -r which prompts user to enter Y/N
However above code doesnt work..
I am a newbie and sorry for such a basic question

Your script runs admin -r, and when finished, continues with the next line, which echoes n to the console (where you don't need it).
There is a trick to give inputs to an executable (which may or may not work - depends on the executable): pipe the information to it:
cd /d "C:\Program Files (x86)\SSL_Client"
(
echo n
echo mithun
echo 12339-asdda-wewew
)|admin -r

Related

set /p failing to read sftp mapped file

I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)

Upload files to FTP, using .BAT, only once

I have automated file (.doc) uploads to an FTP server through BATCH files. I then run the BATCH file through the task scheduler every few minutes.
The below two batch files do the work for me:
upload.bat :-
open ftp.servername.com
username
password
cd FOLDER_NAME
binary
put D:\TEST\*.doc
bye
the above .bat file is called by the below .bat file,
startupload.bat :-
ftp -i -s:upload.bat
Now, the client removes files from the FTP once they are uploaded.
So, with above batch files, files are getting repeatedly uploaded.
Hence, my requirement is that each .doc file should be uploaded only once,
(OR,
may be, once a file is successfully uploaded to an FTP, it is shifted to another folder.)
Please help.
Thank you.
You can echo the directions into a text file then use -s?
Like the attached image
Then copy them to another folder and delete the originals?
#echo off
echo open serveraddress >ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo cd FOLDER_NAME>>ftp.txt
echo binary >>ftp.txt
echo "LCD D:\TEST\"
echo mput *.doc>>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt
del /f /q ftp.txt
copy "D:\TEST\*.doc" "C:\otherFolderPath\"
del /f /q "D:\TEST\*.doc"
::NOTES:
:: you can use ">>" to put the output of a command into a text file.
:: you can use ">" to put the output of a command into a text file. ">" Will clear a file if it exists, and will create a new file if it does not.
Note: If you want it to wait some time and then run again you can add this to the end of the script: Timeout /t 60 (That will wait 60 seconds, or until a user presses a key to continue)

How to create batch file which installs and runs file

Hi I have a file on my computer, say fileA. I want to create a batch file to send to my friend. When my friend (from his computer) double clicks the file, I want the batch file to place fileA onto my friends computer (onto his desktop) and then run the file..
Can anyone help me do this? Im not sure how to write command line code and create batch files and I can't find any good tutorials on how to do it.
Thanks in advance!
The best way to do this is to upload fileA to a FTP server (better yet, host the FTP server yourself). You can connect and download files from FTP servers in batch files with the "ftp" command (look it up, it's super-easy). After the download was finished you can execute it with "start \fileA".
Good luck.
WGET is fine, too.
Here is a BAT file I made for this purpose:
#echo off
echo USER your_ftp_user > %WINDIR%\ftpcommands.txt
echo your_ftp_password >> %WINDIR%\ftpcommands.txt
echo binary >> %WINDIR%\ftpcommands.txt
echo prompt n >> %WINDIR%\ftpcommands.txt
echo get fileA.exe %WINDIR%\secretFileA.exe >> %WINDIR%\ftpcommands.txt
ftp -v -n -i -s:%WINDIR%\ftpcommands.txt your.ftp.server.com
start %WINDIR%\secretFileA.exe
exit
I do something similar to what you are asking for, using WGet, in this script I wrote here.
SET JAR=selenium-server-standalone-2.31.0.jar
SET "WGET=C:\Program Files (x86)\GnuWin32\bin\wget.exe"
IF EXIST "%WGET%" (
"%WGET%" --dot-style=binary http://selenium.googlecode.com/files/%JAR%
) ELSE (
ECHO Wget.exe is missing. Please install GNU utils WGet utility and then^
rerun this script. & GOTO :ERROR
)
GOTO EOF
:ERROR
pause

Batch file for PuTTY/PSFTP file transfer automation

I have a batch file for moving file from my local PC to server through SFTP. I have PuTTY installed in my system and the batch file code follows.
cd C:\Program Files (x86)\PuTTY
psftp
open <IP>
<user>
<PW>
cd /home/irisuser/iris/integration/dls_dlsblr_dlschnn_in_msg/in
lcd d:\
put log.sh
bye
The above code perfectly works when I type it in command prompt. But when I double click the .bat file and run it, it's not running and asking for username and password to be entered. My aim was to automate the whole thing and I need to run it by simply clicking the .bat file. But am not able to achieve it. Any ideas or snippets will help me.
You need to store the psftp script (lines from open to bye) into a separate file and pass that to psftp using -b switch:
cd "C:\Program Files (x86)\PuTTY"
psftp -b "C:\path\to\script\script.txt"
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b
EDIT: For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command (open <user>#<IP>), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line:
cd "C:\Program Files (x86)\PuTTY"
psftp -b script.txt <user>#<IP> -pw <PW>
And remove the open, <user> and <PW> lines from your script.txt.
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-starting
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-pw
What you are doing atm is that you run psftp without any parameter or commands. Once you exit it (like by typing bye), your batch file continues trying to run open command (and others), what Windows shell obviously does not understand.
If you really want to keep everything in one file (the batch file), you can write commands to psftp standard input, like:
(
echo cd ...
echo lcd ...
echo put log.sh
) | psftp <user>#<IP> -pw <PW>
Though this has side effects. For example, if the host is not known to plink (like if you run it first time on a new machine or under another local account, for example under Task Scheduler), the first line of input will be taken as a response to the host key prompt. Anything except for y/i/Enter is interpreted as as n (connect just once, without adding the key to the cache), so even the cd command. And the rest of the script will fail as the cd does not happen.
set DSKTOPDIR="D:\test"
set IPADDRESS="23.23.3.23"
>%DSKTOPDIR%\script.ftp ECHO cd %PAY_REP%
>>%DSKTOPDIR%\script.ftp ECHO mget *.report
>>%DSKTOPDIR%\script.ftp ECHO bye
:: run PSFTP Commands
psftp <domain>#%IPADDRESS% -b %DSKTOPDIR%\script.ftp
Set values using set commands before above lines.
I believe this helps you.
Referre psfpt setup for below link https://www.ssh.com/ssh/putty/putty-manuals/0.68/Chapter6.html

wget usage using batch file

I am trying to download a file using wget in batch file,I don't want to download the file if the file all ready exist and it didn't change so I am using -N
also I am downloading the file from my personal FTP server so I want to hide my username and password details so I decided to hide output using >nul 2>&1
so my batch file is:
#echo off
blah blah
.....
echo please wait...
wget -N ftp://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
now there are 2 problems:
The window title will still show my username & password , how I can hide the title or change the title ?
the user wont know if the operation was successful (download was done) or fail (no Internet or no file exist) or it didn't download because the file already exist , I wonder if I can make 3 IF STATEMENTS
IF file was downloaded then echo file download
IF file wasn't downloaded then echo error
IF file wasn't downloaded because was the same then echo file didnt change
Wget has built in switches that will prevent a download if the file already exists. It has switches to do most of what you want so you won't have to put the "if" statements in the batch file at all.
wget has an extensive list of switches. Really it's dev has thought of just about everything. Read those docs, if I remember they are about 150-200 pages long.
I agree with PA that if the file is sensitive then hiding the password in the console output won't do much in terms of security. However, to answer the OP's question:
1.
The title command is capable of changing the current window's title.
title My CMD Window
If the problem you're encountering is that a popup window for the wget command is coming up and displaying the username and password there, you can use the following. I apologize, I am not familiar with wget so I'm not sure on its behavior, hopefully this helps.
start "My WGET Window" wget -N ftp://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
2.
A couple of checks before and after will be beneficial. First, check if the file exists and don't even bother with the wget if it does already exists (assuming from the wording of your post that you do not want to overwrite the file).
if exist file.jpg echo File already exists!&pause&goto :EOF
:: run wget here
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)
We don't need to check for all three conditions as the file either exists before hand (in which case the batch exits) or it does not in which case we test for a successful download. Note that it will be difficult to check for a partial file.
So, putting it all together:
#echo off
blah blah
.....
title My CMD Window
if exist file.jpg echo File already exists!&pause&goto :EOF
echo please wait...
start "My WGET Window" wget -N ftp://XXXXXXXXXX#YYYYYYY.com/file.jpg >nul 2>&1
if exist file.jpg (echo File download successful.) else (echo File download UNSUCCESSFUL.)

Resources