how to give input to a command line? - batch-file

i need a help on how to give input to a command prompt using a batch file..
for example-
if i run the command mysql -u root -p to open my mysql server and it asks for password.. how i m gonna give that password to it using a batch file...
pls help me i am little new to it

Just specify the password besides -p in batch file. Eg: if username is Ankit and password is Devil, specify the following in the batch file:
mysql -uAnkit -pDevil

Ask for the input using Set /P, e.g.
Set /P "pwd=Please enter your password: "
You can then use the variable %pwd% in your command string, e.g.
mysql -u root -p %pwd% …
You could also use the same method to request the user name too:
Set /P "usr=Please enter your user name: "
Set /P "pwd=Please enter your password: "
mysql -u %usr% -p %pwd% …
Type Set /? at the Command Prompt for the command's usage information.

Related

Executing "su" with PuTTY Plink from a batch file

I'm having trouble trying to get my batch file to also give me super user access in PuTTY/Plink. I can auto-input the ip and password but I want to also automate it to where it will also give me su access when I run this batch file. So far it runs up to the point where I need to manually enter su but I'm trying to automate that so I tried -m commands.txt and now it just crashes.. Looking for any help here thanks.
this is what I have so far
#echo off
color b
cls
echo What is the IP for the unit?:
set /p IP= :
echo What is the SSH Password for this unit?:
set /p Passw0rd= :
echo What is the Root Password for this unit?:
set /p ro0t= :
start plink.exe -ssh user#%IP% -pw %Passw0rd% -m commands.txt
exit/b
what in the commands.txt:
su
The su gives you a hint what's wrong, for sure. But you cannot see it because you start plink using start in a new console window that immediately disappears. Remove the start, at least when debugging the problem.
Anyway, you most likely get "standard in must be a tty" or a similar message. The su by default needs terminal for the password prompt. The Plink is meant an for an automation, not an interactive use. So it by default does not allocate a pseudo terminal for the session. To force use of the terminal, add -t switch:
plink.exe -t -ssh user#%IP% -pw %Passw0rd% -m commands.txt
A similar question: "Sudo" fails with "sudo requires a tty" when executed from PuTTY command line
As a next step, you will probably want to execute further commands within su. For that see Executing (sudo) subcommands using Plink.
Though note that in general automating su, particularly su with a password, is usually a bad idea: Allowing automatic command execution as root on Linux using SSH.

how to get user input in a batch file and use that value in our code in sql server?

I have created a bat file named test.bat as below :
sqlcmd -v item="%item%" -v cost=%cost% -i test.sql sqlcmd -S "172.16.253.72,17001" -U user -P pdw -d Master -I C:\Maintenance\test_blocking_backup\test.sql -o C:\Maintenance\test_blocking_backup\output.txt
so when i will run this file it should prompt me for the value of item and cost. but then how can i fetch the value entered by the user in a variable
Using .Bat file with input Parameters
1) With Prompt:
add before your sqlcmd line
echo off
set /p item=Enter item:
set /p cost=Enter cost:
2) without Prompt :
add before your sqlcmd line
set item=%1
set cost=%2
Then call your bat file like this
test.bat itemvalue,costvalue
where itemvalue and costvalue are the parameters values to pass to the .bat file

How to run these set of sh commands in bat file?

I need to run this sh commands. First, the command will be this one
keytool -importcert -file myfile.cer -keystore keystore.jks -alias "Aliasname"
Enter keystore password:
Re-enter new password:
Trust this certificate? [no]:
I need to run this commands automatically, And when the first command is successful. It asks for a password. I need to enter it automatically. and finally yes.
How to do that using .bat file?
If keytool is compliant with standard input, you can use:
(
echo your_password
echo your_password
echo y
)|keytool -importcert -file myfile.cer -keystore keystore.jks -alias "Aliasname"

vbs execute from network with credentials

I need to run a setup.exe from the local network with credentials.
I was able to do this with a bat file, but the problem is, but i don't want the cmd window showing up when this is executing.
So from what i've read, a good option is to use a vbs script like this, except i can't find a way to use it with credentials.
Set shell = WScript.CreateObject("Wscript.Shell")
shell.Run("\\192.168.0.100\Setup\setup.exe"), 0 , True
Basically what i need is the equivalent of this batch script in vbs:
net use Z: \\192.168.0.100\sh /user:user password /persistent:yes
start /d "Z:\Setup" setup.exe
you may do this:
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\192.168.0.100\sh", True, user, password
Set shell = WScript.CreateObject("Wscript.Shell")
shell.CurrentDirectory = "Z:\Setup"
shell.Run("setup.exe"), 0 , True
Note: MapNetworkDrive will fail if Z: is already mapped. You will need to handle that.
PSExec is what you want:
https://technet.microsoft.com/en-us/sysinternals/pstools.aspx
Like this:
Set shell = WScript.CreateObject("Wscript.Shell")
shell.Run("psexec.exe -u User -p Password -accepteula \\192.168.0.100\Setup\setup.exe"), 0 , True
Documentation:
https://technet.microsoft.com/en-us/sysinternals/pxexec
Using PsExec
Usage: psexec [\computer[,computer2[,...] | #file]][-u user [-p
psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c
[-f|-v]][-w directory][-d][-][-a n,n,...] cmd [arguments]
-u Specifies optional user name for login to remote computer.
-p Specifies optional password for user name. If you omit this
you will be prompted to enter a hidden password.
-accepteula This flag suppresses the display of the license dialog.

Adding a variable in CMD

I wrote a small script which intends to copy a file from our Servers, copy it to a local machine and run it. It works for me, however I would like to add parameters in order to make it easy for others to use it as well.
#echo off
pushd \\NetworkPath & copy batfile.bat \\ComputerName\c$\Users\UserName\Desktop & popd & psexec -i -s -d \\ComputerName -u UserName -p UserNamePassword "C:\Users\UserName\Desktop\batfile.bat"
As you can see it copies the file locally to the Desktop of the user and runs the file itself. Please tell Me how I can use variables for ComputerName,UserName and UserNamePassword in order to have a query each time asking me what are the values.
You need the SET /p command:
#echo off
SET /p pwd=password:
SET /p usr=user name:
SET /p compname=computer name:
pushd \\NetworkPath & copy batfile.bat \\%compname%\c$\Users\%usr%\Desktop & popd & psexec -i -s -d \\%compname% -u %usr% -p %pwd% "C:\Users\%usr%\Desktop\batfile.bat"

Resources