batch file log multiple telnet sessions - file

I have a batch file that logs into a device gathers inf and logs out. the data is recorded to a txt file. it then opens another telnet session gathers data and logs. my proble is i have two logs. any way to combine them into one?
#echo off
cls
:start
#ECHO OFF
:: Get Info
set /p input1="Enter First IP Address and press ENTER "
set /p input2="Enter Second IP Address and press ENTER "
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt "%input1%"
cscript /nologo 1.vbs
timeout /t 3
taskkill /im telnet.exe /f
timeout /t 2
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt "%input2%"
cscript /nologo 1.vbs
echo "DONE!"
pause

Just concatenate both logs in the end with type command
type C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt > C:\Users\%username%\Desktop\TELNET\CHANGE_ME_TOTAL.txt
Before that, you'll need to wait until second telnet is finished and maybe kill it like you did with the first one, or second log file will be locked and unreadable.

Related

Batch file taking a long time to run Stuck on error

I am trying to run this simple script (Below)but it takes it forever to run. I get an error: the rpc server in unavailable. then it sits there for a long time. It will finally finish but takes a long time. How can I add something to the script to make it quit looking on errors and move on to the next device?
#echo off
FOR /F "tokens=*" %%I in (C:\Users\owner\Downloads\listt.txt) do call :pinger %%I goto :eof
:pinger
echo %TIME% >> ping_09_28_17.txt
ping -n 1 %1 >> ping_09_28_17.txt
wmic /user:username /password:XXXXXXXX /node:%1 computersystem get username >> ping_09_28_17.txt
:: DONE

Close a batch window only by typing "exit"?? disable the close "X" button on the top right of batch window

I run a batch file which carries out some task for hours. BY mistake users happen to close the terminal window in which the batch file is executing by pressing the top right "X" close button.
I want to disable it and want to close the batch file window only by typing "exit" inside the window..is this possible?
This can't be done using only one batch file. But there might be a really dirty way for this. You will need three batch files:
EDIT: Here is the solution. Put these three files into the same folder.
controller.bat:
#ECHO OFF
TITLE CONTROLLER
IF NOT EXIST hiddenrunner.vbs (
echo CreateObject^("WScript.Shell"^).Run Wscript.Arguments^(0^), 0, False>hiddenrunner.vbs
)
tasklist /V|findstr "MONITOR"
IF %ERRORLEVEL%==1 hiddenrunner.vbs monitor.bat
:INPUTLOOP
CLS
SET /P input=Type 'exit' to exit:
IF /i NOT %input%==exit GOTO INPUTLOOP
ECHO ok, exiting
taskkill /F /FI "WindowTitle eq Administrator: WORK" /T
taskkill /F /FI "WindowTitle eq Administrator: MONITOR" /T
ping localhost -n 2
del /Q hiddenrunner.vbs
monitor.bat:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE MONITOR
hiddenrunner.vbs work.bat
:LOOP
tasklist /V|findstr "CONTROLLER"
IF !errorlevel!==1 START controller.bat
ping localhost -n 2
GOTO LOOP
work.bat:
#ECHO OFF
title WORK
ping -t localhost
In your case, the work-file will be your original batch you want to execute. You'll have to adjust the window titles (like TITLE CONTROLLER and WindowTitle eq Administrator: WORK) to fit your setup. However, this works for me. If the user closes the input console, it is being restarted after one second. All processes are being terminated after the user inputs exit in the input console.

Batch file cancel timeout when password is typed

I don't know how to cancel the timeout when the password is typed it. Also it doesnt show till after i hit enter at that point the computer logs out because of the event
#echo off
:TIMER
pause
timeout 60
shutdown -l
:LOGIN
echo Enter password to stop the timer!
set /p pass ="Password: "
if NOT %pass%== Password goto :FAIL
if %pass%== Password goto :Success
:FAIL
echo Invalid password please try again!
goto :LOGIN
:SUCCESS
exit
This was a bit more difficult that it initially appeared. I've commented everything, but let me know if you have any questions.
#echo off
pause
REM We need to start the timer and shutdown in a separate window because batch
REM can't do multiple things at once. Also, we're starting the second window
REM minimized mostly for aesthetics, but also to reduce the chance of the user
REM pressing a key by accident and killing the timer without diffusing the stop.
REM
REM The /t 10 waits 10 seconds before shutting the computer down. Unfortunately,
REM the /l option to log out does not allow the /t switch to be used. This adds
REM a bigger element of danger, anyway. The way the code works, you'll still get
REM a pop-up that says that Windows is shutting down in less than a minute, but
REM the shutdown will be aborted.
start "" /min cmd /c "timeout 60&shutdown /s /t 10"
:LOGIN
echo Enter the password to stop the timer!
REM I moved the left quote to the left of pass so that quotes would still be in
REM effect but they wouldn't be visible. I also removed the space between pass
REM and the = because the way it was before, you had a variable that would have
REM to be accessed as %pass % because you can put spaces in variable names for
REM some reason.
set /p "pass=Password: "
REM I've put quotes around both %pass% and Password to prevent a syntax error
REM that would occur if the user didn't enter anything. Also, I got rid of the
REM space to the left of Password because the way it was originally written,
REM the password would have been <space>Password and I wasn't sure if that was
REM deliberate. If it was, just add it back in.
if not "%pass%"=="Password" (
echo Incorrect password! Please try again!
goto :LOGIN
) else (
echo CORRECT!
REM Thankfully, the timeout command is a separate executable from the rest
REM of CMD, so we can just simply kill it. Note that if you have any other
REM timeouts running, this will kill them as well. The >nul is to prevent
REM a "The process timeout.exe with PID 12345 has been terminated" message.
taskkill /F /IM timeout.exe >nul
REM I've added a one-second(-ish) pause between aborting the timeout and
REM aborting the shutdown so that the shutdown command actually has a
REM chance to start. Otherwise, there's a race condition where the abort
REM might run before the shutdown is triggered and that would be really bad.
ping 127.0.0.1 -n 2 >nul
REM This just aborts the shutdown command.
shutdown /a
)

scheduling of batch script through task scheduler

I have below script which have output as well as log redirection along with date and tiem written into the log and erro files. i am saving this script with .cmd extension. Manually when i try to run this script through the command prompt it runs perfectly and first write current date and time in output and error log and then start recording the logs. but when schedueled through task schedule it only writes current date and time int othe logs but not hte actual logs. Can someone please let me know how i can schedule this script in such a way it will first record the current date and time and then start recording the logs.
Echo Date:%date% Time:%time% >> error.txt
#echo off
(
Echo Date:%date% Time:%time%
start "" /wait /b "D:\ITSMaaS\BTscripts\capgemini\BESExtract\bin\BES_EXTRACT.exe" "-f D:\ITSMaaS\BTscripts\capgemini\BESExtract\conf\BES_EXTRACT.CONF"
:loop
for /f "tokens=2 delims=: " %%a in ('tasklist ^| find "BES_EXTRACT.exe"' ) do (
if "%ERRORLEVEL%"=="0" (
ping -n 10 localhost > nul 2>nul
goto loop
)
)
start "" /wait /b "D:\ITSMaaS\BTscripts\capgemini\BESExtract\bin\BES_DATA_MAP.exe" "-f D:\ITSMaaS\BTscripts\capgemini\BESExtract\conf\BES_DATA_MAP.conf"
) >> Output.txt 2>> error.txt
Use redirection on the batch file. In order to output date and time to both files, you could use this technique:
ECHO Date:%date% Time:%time%
1>&2 ECHO Date:%date% Time:%time%
The first ECHO will write to standard output which would be caught with >> Output.txt applied to the batch file. The second ECHO will write to standard error, that output would be redirected by 2 >> error.txt.
If you are worried that the values might differ slightly (by hundredth of a second, perhaps), you could first store the output string to a variable:
SET "datetime=Date:%date% Time:%time%"
ECHO %datetime%
1>&2 ECHO %datetime%
Set 'program/script' : 'yourfile.bat' WITHOUT PATH
Set 'Start in' the rest of path

command line parameter while

I want to run command line with patameters to make it wait for connection then do "gpupdate /force" and then nofity user the job is done. The thing I am stuck at is making it wait for connection.
cmd /c echo Connect a network cable. & *something* & echo n | gpupdate /force & msg * Done.
If something was following code then it would work:
:top
ping -n 1 site.com >nul
if errorlevel 1 (
goto top
)
but I don't know how would I write it on 1 line as a parameter.
How would I make it work the way I wish? Alternative solutions are welcome as well!
I don't understand your aversion to a batch script. But the following one liner should work.
#echo Connect a network cable.&cmd /q /c "for /l %N in () do ping -n 1 site.com >nul&&exit"&echo echo n^|gpupdate /force&msg * Done.
The key bit is cmd /q /c "for /l %N in () do ping -n 1 site.com >nul&&exit". A new CMD.EXE process is created that enters an infinite FOR loop. It continuously pings the site until it is successful. The EXIT command only executes when PING was successful because of the && operator. Once EXIT executes, control is returned to the parent CMD.EXE process.
Why not make it all a batch file?
#echo off
echo Connect a network cable.
:top
ping -n 1 site.com >nul || goto :top
echo Connected.
gpupdate /force
msg * Done.

Resources