Stop Spring applciation running in some port using bat file - batch-file

Hi I am new bat files and I am using a start-service.bat file which runs a spring boot application in a given port say 6600 using a jar.
Now I would like to write a stop-service.bat file which would stop the application running in the port
my stop-service.bat looks something like this:
echo off
set port= 6600
echo stopping service on port: %port%
FOR /F "tokens=5 delims= " %%P IN ('netstat -ano ^| findstr :%port%) Do taskkill /F /PID %%P
when I run the script I am getting an error like :
The system cannot find the file 'netstat -ano | findstr : 6600.
is my approach right?

Related

starting a jar from batch file blocks the batch file

I created a batch file to start a jar, and I am running this from process Builder. when the start command is triggered control never comes back and BufferReader goes into an infinite loop. Appreciate any help to unblock this.
running this code on Windows Server 2012 R2 Standard
Java code
ProcessBuilder pb = new ProcessBuilder("startabx6.bat",abx);
pb.directory(new File(System.getProperty("user.dir")));
Process process = pb.start();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (((line = reader.readLine())!=null )) {
log.info(line);
}
reader.close();
Batch file
#ECHO OFF
echo %1
set build=%1
echo %build%
FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %%P /F
echo "launching abx jar"
start java -jar -DServer.port=8080 libs\ABJars\%build%
Thanks for the help! The solution that worked for me was to use the StreamGobbler class, which enables reading logs in a different thread.

Script to Kill All RDP Sessions

I am trying to create a simple script to kill ALL remote desktop sessions, active or disconnected, without rebooting the server. My server OS is Windows Server 2012R2 with Remote Desktop Services enabled and licensed.
I found a simple batch file script here to do this task: enter link description here
When I run this script locally on my terminal server, I get an error in the batch file runs that says: Session Disc not found
And only the console user is logged off. Can anyone tell me what is wrong with this script?
query session >session.txt
for /f "skip=1 tokens=3," %%i in (session.txt) DO logoff %%i
del session.txt
My Session text file looks like this:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>console Administrator 13 Active
wcunningham 18 Disc
kstarkey 25 Disc
rdp-tcp#11 cyannone 52 Active
rdp-tcp 65536 Listen
What I found is a good workaround is adding an additional line to handle the disconnected sessions. Since disconnected sessions don't list the sessionname, the ID is at position 2 not position 3 like it is with active session. So adding an additional line that specifies token=2 did the trick:
query session >session.txt
for /f "skip=1 tokens=2," %%i in (session.txt) DO logoff.exe %%i
for /f "skip=1 tokens=3," %%i in (session.txt) DO logoff.exe %%i
del session.txt
skip the first 3 lignes and the active user
query session >session.txt
for /f "skip=3 eol=> tokens=2," %%i in (session.txt) DO echo %%i
for /f "skip=3 eol=> tokens=3," %%i in (session.txt) DO echo %%i
del session.txt

Ping with timestamp and log

Guys im using this script in a batch file to perform continuous ping with timestamp and to be recorded in a log file, it is working fine on my computer ( windows 8 64 bit ) but when i try to use it on a windows 7 machine also 64 bit, whenever i run the batch i keep getting that:
Could not find C:\user\administrator\pinglog.txt
knowing that i have created the file pinglog.txt but i really cant figure out the problem.
#echo off
del pinglog.txt
for /f "tokens=*" %%A in ('ping localhost -n 1 ') do (echo %%A>>pinglog.txt && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping localhost -n 1 ') do (echo %date% %time% %%A>>pinglog.txt && GOTO Ping)
i appreciate any help.
Thank you in advance
Over PowerShell you can use this CLI.
ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt
Could not find C:\user\administrator\pinglog.txt means that the file was not found. And the reason for this is really simple. The folder is called users and not user. So the correct path is C:\users\administrator\pinglog.txt.

Terminal Server logon script

I'm writing a simple .bat script to track logons to our terminal servers and append the records to a single CSV file. I can't quite get my script to behave correctly. Almost everything is working, but the clientip variable has an entire line of output instead of just the IP address that I desire. I don't think I'm doing the TOKENS correctly, and I suspect it's because the output of the netstat command isn't delimited by a tab character.
#ECHO OFF
FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A
FOR /F "TOKENS=3 delims=<tab>" %%B IN ('netstat -n ^| find ":3389" ^| find "ESTABLISHED"') DO SET CLIENTIP=%%B
echo Logon,%username%,%date%,%time%,%computername%,%clientip% >> \\dc01\c$\logons.csv
If an example output of netstat -n | find ":3389" | find "ESTABLISHED" is
TCP 10.10.20.100:3389 68.46.xxx.xxx:50373 ESTABLISHED
then how can I set the clientip variable to only (last digits redacted, of course) 68.46.xxx.xxx? A bonus would be leaving off the port, in this case ":50373".

How would I read the contents of a text file and run a command for each line?

I have a batch file that remotely connects to machines over a vpn to download files using robocopy.
Currently it asks for domain credentials and IP address to each machine.
We have 100+ machines and its getting pretty tiresome having to type in an IP each time to run a command.
What I am trying to look for is to have a text file with an IP address on each line, and for the batch file to run its robocopy command for each of those IP addresses. The user will only need to enter their domain credentials once.
This is what I currently have:
#ECHO OFF
SET /P ipaddress= Please enter an IP address
SET /P sitenum= Please enter the store number
CLS
SET /P user= Please enter your domain username
SET /P pass= Please enteer your domain password
CLS
Set filedate=%date:/=%
NET USE \\%ipaddress%\IPC$ /u:DOMAIN\%user% %pass%
ECHO Copying Dataset...
Robocopy "\\%ipaddress%\C$\ProgramData\App\Data" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\Data" /eta
ECHO Done!
ECHO Copying ControlPoint Server Data....
Robocopy "\\%ipaddress%\C$\ProgramData\App\MoreData" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\More Data" /eta
ECHO Done!
NET USE \\%ipaddress%\IPC$ /D
This is probably the most advanced bit of batch that I have put together using googles help!
I have looked into "/F "tokens" but then thats as far as I can figure out. I normally enjoy building these things myself, but on this one occasion I am stuck.
read IPs from text file:
for /f "delims=" %%a in (IPs.TXT) do (
NET USE \\%%a\IPC$ /u:DOMAIN\%user% %pass%
ECHO Copying Dataset...
Robocopy "\\%%a\C$\ProgramData\App\Data" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\Data" /eta
ECHO Done!
ECHO Copying ControlPoint Server Data....
Robocopy "\\%%a\C$\ProgramData\App\MoreData" /Z /S "D:\Transfer Files\%sitenum%\%filedate%\More Data" /eta
ECHO Done!
NET USE \\%%a\IPC$ /D
)
In order to run a script once for each line in a file you can surround your script with :
while read data; do
//your scipt goes here
done
The $data will contain a line from your file (so, an IP address).
Then you can use your script like that : yourScript.sh < ipFile.txt

Resources