Batch file to execute redis commands - batch-file

I am trying to write below commands in batch file.
cd C:\Redis
redis-cli
select 6
file name is "xxx.bat"
It should connects to local redis instance and select database 6.
but it stops at second command "redis-cli". and does not even write second command "select 6"
on command prompt.
I mean i connects to server , but does not write next command.
I think i do not know something about executing commands in batch file.
I can run these commands outside of batch file without any problem.
Can somebody please spot the problem.
Regards

CMD does not pass the commands to the redis-cli.
You can test it with quit from the redis-cli, you will see a a CMD error for the select 6 command.
You should put all of your commands (without connecting with redis-cli) in a text file e.g test.txt and pass it all to to redis-cli. i.e:
type test.txt | redis-cli -x
p.s. there's no need to specify .exe or any other extension that is in you %pathext% variable, but it makes your scripts more readable.

Try
cmd /c "redis-cli select 6" >> output.txt

I need to specify exe with the command.
redis-cli.exe select 6

With my redis 2.4.6 on windows you can pass the command as an argument to the redis-cli executable. Here is my windows batch file:
SET REDIS_PATH=E:\Program Files\Redis
"%REDIS_PATH%\redis-cli" select 6
Of course you would change the redis path variable to wherever you have copied / installed Redis to.
Here is the output from running the windows batch file:

Related

A way of typing multiple commands in cmd.txt file using PuTTY batch against Cisco

I am running batch file in Windows environment using PuTTY.exe command below:
for /L %%n in (1,1,5) do (
SET z=Site%%n%
start c:\Users\emrpocadmin\desktop\putty.exe -ssh IPAddress -l User -pw Password -m c:\Users\emrpocadmin\desktop\cmds.txt -load Site%%n%
)
This batch should load variable profiles (n from 1 to 5) predefined in PuTTY and run on them one-by-one the commands defined in cmds.txt file; then save the output to certain folder..
However, in cmds.txt file i could only enter one command!
How can I enter multiple commands on this txt file and it should be passed to the session one by one and then save the whole output results into the output file?
I tried to enter the commands on cmds.txt file as of below formats but does not work:
show run (work as one command only)
show run; show version (does not work)
"show run; show version" (does not work)
echo show run
echo show version (does not work)
The expected result is to enter two or more commands in cmds.txt file and it should be passed to the session one by one and then save the whole output results into the output file.
It's actually a known limitation of Cisco, that it does not support multiple commands in an SSH "exec" channel command.
Quoting section 3.8.3.6 -m: read a remote command or script from a file of PuTTY/Plink manual:
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
Using Plink (PuTTY command-line connection tool) with an input redirection may solve the problem (you should not use PuTTY to automate command execution anyway).
plink.exe -ssh IPAddress -l User -pw Password -load Site%%n% < cmds.txt

Is there a way to check for return code using PSFTP?

I am creating a script file using windows BATCH and then calling it using PSFTP (using Putty from command line to call TransferScript.txt).
The requirement is to get a file from Unix box to windows and then delete it from Unix but after getting the return 0. My code looks like:
TransferScript.txt
lcd E:\Temp
cd /sap/xx/yy
get abc.dat
IF %ERRORLEVEL% EQ 0
rm abc.dat
quit
This doesn't work, however file is getting copied but not getting deleted. Can anyone help?
The command processor for the file is psftp, not cmd.exe. As such, IF is not a recognized command. You can, however, use the ! PSFTP command to pass commands to Windows. This is documented here. Here is a tested working version:
Command: psftp -b script.txt username#ftpserver.com -pw password
#script.txt
!echo Connected. Getting file...
lcd c:\temp
cd sap/xx/yy
get abc.dat
!IF EXIST c:\temp\abc.dat echo Success
!IF NOT EXIST c:\temp\abc.dat echo Failure
quit
Simply replace echo Success with rm abc.dat or whatever you want to happen if the file exists on the local file system.
Another option would be to do all the logic in an external batch file. Write 2 PSFTP scripts like script_get.txt and script_rm.txt and call the second one only if the file exists (like above).
I know neither of these solutions technically answer your question in that they don't capture an error state from PSFTP but hopefully it's an acceptable workaround.

Batch script issue (Tomcat install the service but start using batch file )

I saved the below code as batch file but script able to install the tomcat service but not able to execute the line 4. Please let me know why it is not able to execute the line 4.
If i copied all the code and paste cmd window it is working fine and able to execute each command. but if double click on the batch file then not able execute the line 4 & 5. Please do the needful help. Thanks in advance
cd %CATALINA_HOME%
net stop SERVICE_NAME
sc delete SERVICE_NAME
service.bat install SERVICE_NAME
tomcat7.exe" //US//SERVICE_NAME --JvmMs=1024 --JvmMx=1024 --Jvm="C:\Program Files\Java\jre7\bin\server\jvm.dll" ++JvmOptions="-XX:MaxPermSize="512m" --Startup auto'
net start SERVICE_NAME
Use call command to start another batch script and return to calling batch as follows:
call service.bat install SERVICE_NAME
Recheck the tomcat7.exe line for proper use of " double quotes.
Use full paths to service.bat or tomcat7.exe if an error occurs similar to
'ddd' is not recognized as an internal or external command, operable
program or batch file.

Force bat file to use non default cmd.exe

I'm writing a .bat file to handle some script generation automatically so I don't have to type in half a dozen command arguments each time I want to run it.
I have to run a vb script from the batch file
#call ..\Database\scripts\runscriptupdates.vbs
However the script will only run if using the the command prompt from
C:\Windows\SysWOW64\cmd.exe
By default the bat file uses the cmd.exe in system32
C:\Windows\System32\cmd.exe
Is there a way to force the batch file to use this cmd.exe to run the vbs file? I've been trawling the web for about an hour now and haven't found anything which helps (so far).
I've tried running the syswow64 with "start ..." however it doesn't seem to take the arguments after it.
Many thanks, Neil
You can try:
%windir%\SysWoW64\cmd.exe /c mybatch.bat
This will run the batch itself from a 32-bit command prompt. Thus, the call to your vbs will also be coming from a 32-bit command prompt.
I also had this problem, and I found the way to solve it.
You just need to change System Variables.
Go to Control Panel » System » Advanced System Settings » Environment Variables.
Find the variable ComSpec, then just click Edit... and change the path to "C:\Windows\SysWow64\cmd.exe"
Try typing this one line in your batch file.
%windir%\SysWoW64\cmd.exe /c ["]cscript [script name] [host options] [script arguments]["]
Where:
script name is the name of the script file, including the file name extension and any necessary path information.
host options are the command-line switches that enable or disable various Windows Script Host features. Host options are always preceded by two slashes (//).
script arguments are the command-line switches that are passed to the script. Script arguments are always preceded by one slash (/).
Example:
%windir%\SysWoW64\cmd.exe /c "cscript VoltageDrop.vbs /"Campbell.sin" "L08""
Note: In this line I do not pass any host options. This command will execute the string,
cscript VoltageDrop.vbs /"Campbell.sin" "L08"
as a command in the 32-bit command prompt.

How to write sequential commands in batch file

I want ot execute the following commands using batch file:
1.ftp 127.0.0.1
2.USERNAME
3.PASSWORD
4.put
5.D:\\zz.xml
6.mmm.xml
Each command is a sequence of the prev command
I want to move file from local to ftp server
I googled for that and I found that insert ; between commands is the solution but itsn't correct for me
Please support me
1 is a command on the command prompt, but 2..6 are FTP commands.
I would put lines 2..6 in a file called commands.txt then redirect that file to stdin.
ftp 127.0.0.1 < commands.txt
Edit:
You could use -s:commands.txt instead of the < according to the ftp help.
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
Use a CRLF between each command.
I would rather use the expect command that allows you to script inputs according to outputs.
You can find some information here: http://en.wikipedia.org/wiki/Expect
You have an example on that page that describes exactly what you want to do.
#Albin Sunnanbo
Thanks for your reply
I solved the problem now from the following tutorial
http://www.dostips.com/DtTipsFtpBatchScript.php
I write the foillowing commnads in batch file:
FTP -v -i -s:commands.txt
then commands.txt contaisn the following commands
open 127.0.0.1
UserName
Password
put
D:\\zz.xml
mmm.xml

Resources