Using user input to customize a .bat shutdown command - batch-file

Okay. I've figured out a way to turn the shutdown command in a .bat file into an instant messenger, because where I work there are no instant messengers allowed:
shutdown -s -m \\[computer name] -t 20 -c "[message]"
PING 127.0.0.1 -n 6
shutdown -a -m \\[computer name]
This works fine, aborting the shutdown command after 5 seconds, but you have to manually edit the computer name and message using a text editor and restart the program in order to send another message. I would like a way to take user input using the SET command that would take the computer name and message as input. I have tried this out, but it didn't work:
:Jump
set /P computer ="Enter the computer name: "
set /P message ="Enter the message: "
shutdown -s -m %computer% -t 20 -c "%message%"
PING 127.0.0.1 -n 6
shutdown -a -m %computer%
GOTO Jump
Any ideas?

You need to remove the space between your variable name and the equals sign:
set /p computer=Enter the computer name:
Otherwise the variable will not be set (it will default to "")

Related

Batch script adjusting Aruba Switch configuration and download it

I've made a script that'll change the configuration and downloads the configuration of an Aruba 2530 switch, but it doesn't work all the time. How can I run the ip ssh filetransfer command from within the batchfile so I can automate things?
** config2530.bat **
#echo off
set /p USERNAME="Username? "
set /p PASSWORD="Password? "
for /f "tokens=*" %%i in (switchlist.txt) do (
echo plink -batch -ssh -l %USERNAME% -pw %PASSWORD% %%i -m config.txt
echo pscp -scp -pw %PASSWORD% %USERNAME%#%%i:/cfg/running-config d:\%%i.cfg
echo plink -batch -ssh -l %USERNAME% -pw %PASSWORD% %%i -m exit.txt
)
** config.txt **
config
timesync sntp
sntp unicast
sntp server priority 1 10.36.8.11
time daylight-time-rule western-europe
time timezone 60
write memory
ip ssh filetransfer
exit
exit
** switchlist.txt **
10.36.1.101
10.36.1.102
10.36.1.103
... etc. etc.
** exit.txt **
config
tftp
exit
exit
The issue is that I can't have plink run the commands from the text files with out "ip ssh filetransfer". I've tried enabling filetransfer using:
plink -ssh -l {username} -pw {password} {ipaddress} (config; ip ssh filetransfer)
but this gives:
Keyboard-interactive authentication prompts from server: ------------------
-Access granted. Press Return to begin session.
End of keyboard-interactive prompts from server ---------------------------
SSH command execution is not supported.
Without SSH filetransfer enabled I can't run config.txt or download the configuration from the switch. When I use Putty and manually enter the command "config" followed by "ip ssh filetranser" and then run the batch-file it'll do whatever I need it to do. I

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.

Windows async batch cannot open database requested by the login

I'm running SQL Server 2005 (Express) on Windows Server 2003 SP2. I'm using a scheduled task to launch a batch program that runs for several hours, during which many child processes are launched.
Almost all these child processes are running synchronously, so the root batch program waits for each child program to complete.
As one of the last actions in the root batch program, several child processes are called asynchronously using start. This allows programs that depend on the work done by this scheduled task to start working on the updated data. The root batch program finishes by sending an email.
scheduled.bat
This is the batch program which is scheduled in Windows Scheduled Tasks.
cd backend
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined timestamp set timestamp=%%x
taskkill /F /FI "USERNAME eq admincm" /FI "WINDOWTITLE eq AtlasSync*"> Sync.Lte.%timestamp:~0,14%.log
title AtlasSync-%timestamp:~0,14%
sqlcmd -t 600 -d ATLAS -i Sync.Lte.build.sql -o Sync.Lte.bat
Sync.Lte.bat>> Sync.Lte.%timestamp:~0,14%.log
Sync.Lte.Bat (truncated)
The root batch program which calls sqlcmd, putty and mail several times. It connects to a remote host, runs a few scripts there, then collects all the log files. It FTPs the data back to the Windows server, where it gets imported into the "ATLAS" database. As soon as the data has been imported, dependencies are called via start.
D:
cd D:\PATH\Sync\backend
echo #!/bin/sh> DirtyNodes.Lte.sh
echo cd /home/cm/PATH/SYNC/>> DirtyNodes.Lte.sh
...
echo open 12.34.56.78> DirtyNodes.Lte.ftp
echo cm>> DirtyNodes.Lte.ftp
...
ftp -v -s:DirtyNodes.Lte.ftp
echo echo `date` -- cd /home/cm/PATH/SYNC/> DirtyNodes.Lte.putty
echo cd /home/cm/PATH/SYNC/>> DirtyNodes.Lte.putty
echo echo `date` -- chmod 755 DirtyNodes.Lte.sh>> DirtyNodes.Lte.putty
...
D:\PATH\TOOLS\putty -ssh -load AtlasSync -l cm -pw XXXXX -P 22 -m DirtyNodes.Lte.putty 12.34.56.78
echo #!/bin/sh> Refresh.Lte.build
echo MOS=/home/cm/PATH/SYNC/Refresh.Lte.mos>> Refresh.Lte.build
echo rm -f $MOS>> Refresh.Lte.build
...
echo #!/bin/sh> Refresh.Lte.sh
echo cd /home/cm/PATH/SYNC/>> Refresh.Lte.sh
echo echo `date` -- Running refresh amosbatch>> Refresh.Lte.sh
...
sqlcmd -t 600 -d CM -h -1 -W -i IpDatabase.Lte.build.sql -o ipdatabase
echo open 12.34.56.78> Refresh.Lte.ftp
echo cm>> Refresh.Lte.ftp
...
ftp -v -s:Refresh.Lte.ftp
echo echo `date` -- cd /home/cm/PATH/SYNC/> Refresh.Lte.putty
echo cd /home/cm/PATH/SYNC/>> Refresh.Lte.putty
echo echo `date` -- chmod 755 Refresh.Lte.build>> Refresh.Lte.putty
...
D:\PATH\TOOLS\putty -ssh -load AtlasSync -l cm -pw XXXXX -P 22 -m Refresh.Lte.putty 12.34.56.78
echo open 12.34.56.78> Import.Lte.ftp
echo cm>> Import.Lte.ftp
...
ftp -v -s:Import.Lte.ftp
cd D:\PATH\Sync\tables
unzip -o Tables.Lte.zip
cd D:\PATH\Sync\backend
sqlcmd -t 600 -d ATLAS -i Import.Lte.build.sql -o Import.Lte.sql
sqlcmd -t 600 -d ATLAS -i Import.Lte.sql -o Import.Lte.log
start D:\PATH\Harmony\backend\ConsistencyCheck.bat
start D:\PATH\Compass\backend\ConsistencyCheck.bat
cd D:\PATH\MAIL\XXXXX
mail.exe -s "Atlas Synchronized" -f "D:\PATH\Sync\backend\Import.Lte.log" -b XXXXX#XXXXX.com XXXXX#XXXXX.com
ConsistencyCheck.bat
This batch is started from Sync.Lte.bat asynchronously. It uses the same credentials as the user running the root batch program.
D:
cd D:\PATH\Harmony\backend
sqlcmd -t 10800 -d ATLAS -i ConsistencyCheck.sql -o ConsistencyCheck.log
ConsistencyCheck.log
Sync.Lte.bat runs just fine, and the email at the end is sent. But in ConsistencyCheck.log, I find the following errors:
Cannot open database "ATLAS" requested by the login. The login failed.
Login failed for user 'XXXXX\admincm'.
I have made both XXXXX\admincm and admincm db_owner of ATLAS.
All programs called from the scheduled task are run under the same user. All other programs (including sqlcmd calls to ATLAS) authenticate just fine, but the started programs do not.
When running ConsistencyCheck.bat manually (interactive RDP session) as another user, it runs fine.
When running ConsistencyCheck.bat manually as XXXXX\admincm, it runs fine.
When scheduling a test from XXXXX\admincm in a couple of minutes (making sure I'm fully logged out of that user in an attempt to emulate normal environment), it runs fine.
When the original scheduled task runs at night, the async'd batches fail with the mentioned errors above.

How to ping a server only once from within a batch file?

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:
ping www.google.de -t
and displays the ping, so it would look like this:
Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms
My problem is, that this will result in this when I execute this command as script:
My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.
I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.
So how to write the batch file to start this command only once and display the ping result?
I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.
my batch file contains below code only
ping 172.31.29.1 -t
with file name as ping.bat
with file name abc.bat
Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:
ping - latest Microsoft documentation for this Windows command
ping - Windows XP documentation for this Windows command
Explanation for option -t given by Microsoft:
Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.
You may want to use:
#%SystemRoot%\system32\ping.exe -n 1 www.google.de
Or to check first if a server is available:
#echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer
echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF
:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
For bash (OSX) ping google.com -c 1 (incase search brought you here)
if you want to use the name "ping.bat", a small trick is to use this code:
#echo off
cd\
ping google.com -t
Just add that "cd\" and you are fine... ;)
Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add #echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.
#echo off
ping www.google.de -t
To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like
%SystemRoot%\Windows\System32\PING.EXE www.google.de -t
Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below
where ping
If the command is recognizable; it will output the path like
C:\Windows\System32\PING.EXE
Else will result in error
I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!
From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1
i used Mofi sample, and change some parameters, no you can do -t
#%SystemRoot%\system32\ping.exe -n -1 4.2.2.4
The only thing you need to think about in this case is, in which directory you are on your computer.
Your command line window shows C:\users\rei0d\desktop\ as your current directory.
So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...
So the complete command would be:
cd ..
ping XXX.XXX.XXX.XXX -t
Having 2 scripts called test.bat and ping.bat in same folder:
Script test.bat contains one line:
ping google.com
Script ping.bat contains below lines:
#echo off
echo Hello!
pause
Executing "test.bat" the result on CMD will be:
Hello!
Press any key to continue . . .
Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script).
Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.
Easy ways to avoid this:
Do not name your script "ping.bat".
You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
Create a text file with text "#%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat".
Just click and run it and you will get the result.
So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).
The answer to your question is this
Ping -n 1 0.0.0.0
But if you want it to be faster than this, this will be your answer
Ping -n 1 -l 1 0.0.0.0
Note: Replace 0.0.0.0 with your desired IP address
Just
write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator

Bypass the messages - Batch File

I have written the batch file as below :
D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe -s "D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF" -d "D:\FastTools\FTOutput\common" -l "D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe -s "D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF" -d "D:\FastTools\FTOutput\common" -l "D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
xcopy D:\FastTools\FTOutput\common\Displays C:\Users\Public\tls\wap\cfg\operatorInterfaces\DEPLOY\displays /y
when i am running it and due to some reason the 1st line fails then it shows the error message on command windows and does not move the 2nd line of execution without manual intervention.
I want to suppress the error message and move to second line of execution automatically .
Use Start /B to don't stop the execution of your script when launching a process, that means all process starting with "/B" will start at once:
Start /B Process.exe
Start /B "" "D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe" -s "D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF" -d "D:\FastTools\FTOutput\common" -l "D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
Use the output redirection to exclude the error messages: http://ss64.com/nt/syntax-redirection.html
Simplified code:
#Echo OFF
Set "FTGraphicConverter_ExecutablePath=D:\Build\Build_2.0.023\GrConv\FTGraphicConverter.exe"
Set "EDF_File=D:\Customer Data\JSM Prj\JSM_DEMO\HIS0364\WINDOW\L3_TB3B_MNFLD.EDF"
Set "CSV_File=D:\FastTools\VPInput\CentumTagsToFASTTOOLSitems.csv"
Set "Output_Dir=D:\FastTools\FTOutput\common"
Set "Input_Displays_Dir=D:\FastTools\FTOutput\common\Displays"
Set "Outut_Displays_Dir=C:\Users\Public\tls\wap\cfg\operatorInterfaces\DEPLOY\displays"
Start /B "" "%FTGraphicConverter_ExecutablePath%" -s "%EDF_File%" -d "%Output_Dir%" -l "%CSV_File%" 2>NUL
%FTGraphicConverter_ExecutablePath% -s "%EDF_File%" -d "%Output_Dir%" -l "%CSV_File%"
xcopy /y "%Input_Displays_Dir%\*" "%Outut_Displays_Dir%\"
EDIT
If you don't want to launch process in /Background and only want to interact with the error message autmatically then:
Echo Y| Start process.exe
If process.exe sends at the end a error message with a user prompt asking a question for "Y" "N" key then we automatically are sending the "Y" key so, if error at the end the execution will not stop.

Resources