Findstr in windows bat - batch-file

I use the following command
netstat -ano -p tcp | findstr "18812"
and got result like this:
TCP 0.0.0.0:18812 0.0.0.0:0 LISTENING 3116
TCP 127.0.0.1:3099 127.0.0.1:18812 ESTABLISHED 5112
TCP 127.0.0.1:18812 127.0.0.1:3099 ESTABLISHED 3116
But the line 2 is not what I want,i.e: I want the lines which the source port is 18812, rather than the dst port .
Any body knows how to deal with this? Thanks very much!
Edit: I tried regular expression, but "\d","+","\s" seems not work

One small improvement to the solution in your comment to fedmich. Probably not needed for port 18812 since the max number of digits is 5, and you've used all of them. But if you are looking for a 4 digit port (or smaller), you will want to make sure there is a space after the port number.
For example, if you were looking for source port 3099
netstat -ano -p tcp | findstr /R /C:"TCP[ ]*[0-9.]*:3099 "

Are you sure you are using regular expression on the first and last part?
".*STRING.*"
Try this out.
netstat -ano -p tcp | findstr ".*:18812.*"

Related

How to force Windows Comand Prompt to flush text output to a file line-by-line

I'm trying to log every ping in my Windows CE5.0 machine Command Prompt using
> ping 192.168.1.1 -t -l 60000 >> ping.txt
The file starts with a single line of output and then only flushes after pressing 'ctrl+c'.
I was wondering if there was a way to force it to print in every new line.
I do not know any direct way to do this.
But you could work around that by doing single ping requests in a (infinite) loop and write to the log file in the loop, like this:
for /L %I in () do #(timeout 1 > nul & ping 192.168.1.1 -n 1 -l 60000 | find "TTL=" >> "ping.txt")
The timeout 1 command establishes a one-second delay in every loop iteration in order to avoid heavy CPU load, > nul suppresses its console output.
The find command is used to filter for lines containing TTL from positive replies (like Reply from 192.168.1.1: bytes=60000 time<1ms TTL=128). If you want, you can change that to findstr /B /C:"Reply from " /C:"Request " /C:"Ping request ", for example, to capture positive replies as well as negative ones like Request timed out. or Ping request could not find host ..., or you can remove it completely (also the |) to write the whole ping response to the file, including header and footer.

Is there a way to programatically identify the status of a tcp connection/port?

I get a port after seeing the $DISPLAY environment variable, and need to check if the vnc on which the current program is run is connected or not.
❯ netstat -an --tcp | grep 5902
tcp 0 0 0.0.0.0:5902 0.0.0.0:* LISTEN
The above is a netstat output.
On tcp connection established for the port, the following is the output:
$ netstat -an --tcp | grep 5902
tcp 0 0 0.0.0.0:5902 0.0.0.0:* LISTEN
tcp 0 0 172.16.100.219:5902 172.16.100.129:35542 ESTABLISHED
One can call netstat from within C/c++ code something like
port = process_display(std::getenv("DISPLAY"))
is_connected = call_this("netstat -anp | grep <porttocheck> | grep ESTABLISHED | wc -l");
I need the is_connected and do some logic.
However, this relies on variety of factors, if the program is going to run on different machines, I would rather not rely on calling netstat from code.
Is there a better way to check if a port has a established TCP connection, from C code? Parsing /proc/ or something similar also looks very unweildy.
I am ok for a linux only solution.
I think you can create a socket with the port which you want the status of it. If socket successfully created it means that the port was closed otherwise it is open. like this

CMD - Ping and traceroute from list of ips

We have sporadic connection failures when webserver tries to connect to service on the net.
There is a problem to trace failure from PHP for many reasons.
I'm a web-programmer and not familiar with command-line scripts. Can anyone help with following cmd-script:
-there is a list of ips separated by newline in text file (ip_list.txt)
-take ip from list and ping it, if it fails on first attempt - traceroute it
-go to next ip in file
I don't really sure what you want to test, but here's a pretty useful command to test ping. Enter ping IP_ADDRESS -l 10 -n 10 directly to cmd, change the IP_ADDRESS to ip address you want. -l 10 - Ping you ip address with 10 bytes of data
-n 10 - Ping for 10 times
ping /? - For more informations
To get each ip address (line by line) in a text file, use for /f %%a in (YOUR_FILE.txt) do ( //to do ). Since I'm not sure about what you want, so that's all I can help with :)

Nmap port scanning array

I am doing a nmap bash script, and I am just wondering if there is any possibility to use array list for my port commands. For example:
port=[23,45,75,65]
for i in 21 do
nmap -p x,y 192.168.1.$i
done
e.g. At the x,y place I want to use the number 23,45
I'm not sure if that's what you want, but you can try this:
ports="23,45,75,65"
for i in 21 do
nmap -p "$ports" 192.168.1.$i
done
You can also do:
ports="23,45,75,65"
targets="1-25"
nmap -p "$ports" "192.168.1.$targets"
Scanning an array of ports is already built in to nmap. See http://nmap.org/book/man-port-specification.html for more details on the syntax, but here's an excerpt that may give you what you need:
For example, the argument -p U:53,111,137,T:21-25,80,139,8080 would scan UDP ports 53, 111,and 137, as well as the listed TCP ports.

Stream a continuously growing file over tcp/ip

I have a project I'm working on, where a piece of Hardware is producing output that is continuously being written into a textfile.
What I need to do is to stream that file as it's being written over a simple tcp/ip connection.
I'm currently trying to that through simple netcat, but netcat only sends the part of the file that is written at the time of execution. It doesn't continue to send the rest.
Right now I have a server listening to netcat on port 9000 (simply for test-purposes):
netcat -l 9000
And the send command is:
netcat localhost 9000 < c:\OUTPUTFILE
So in my understanding netcat should actually be streaming the file, but it simply stops once everything that existed at the beginning of the execution has been sent. It doesn't kill the connection, but simply stops sending new data.
How do I get it to stream the data continuously?
Try:
tail -F /path/to/file | netcat localhost 9000
try:
tail /var/log/mail.log -f | nc -C xxx.xxx.xxx.xxx 9000
try nc:
# tail for get last text from file, then find the lines that has TEXT and then stream
# see documentation for nc, -l means create server, -k means not close when client disconnect, waits for anothers clients
tail -f /output.log | grep "TEXT" | nc -l -k 2000

Resources