Let's say we want a list of port numbers that are open on our local system without any extra data around
it we just want you know number 22 to be displayed if we have S-sh listening on port 22 for example
the nets that command can display open ports and instead of me showing you the man page for it I'm just
going to walk you through the options that we're going to use today.
So we'll use the net stack command we'll use the dash in option to display numbers instead of names
so instead of displaying S-sh or S-sh d it will display 22 for 4:22 we can use you to get information
on UDP and use ti to get information on TCAP and dash well for listening port.
So when we run that command we have this output.
So in this particular case we have two lines that comprise the header.
So there are a couple of different ways to manage this one way would be to pipe it to grep and do dash
VI which we know how to do and then use a pattern like this so that we'll get rid of the first line
and then we could continue this by using grap again and we could get rid of the second line like this.
So that would leave us with just the pure data without the header.
Now I didn't really talk about this in grep but what you can also do is use extended regular expressions
with a dash capital E option and then allows you to do something like this.
The pipe symbol in regular expressions is an order.
So if we match active or Prato then we will have made a match and if we hit enter.
You can see that it gets the same results as using those two grip commands above.
So it's a little bit shorter some people like that.
Again this is one way to solve this problem.
Now for looking at the data that we have left there are some things in common with each line.
Some things that are not uncommon.
Some lines have TCAP some have decent B-6 some UDP some have a.
Some have numbers and so on.
But one thing that is constant throughout here are Colon's.
So again let me run this without any declaration or filtering.
So we see the first two lines here do not have any colons so we can do something like this script for
a colon and that would just display all the lines of had a colon in it.
So either way the goal here is to get the data without the header.
Now let's just go ahead and proceed this way.
So let me tell you that the data we're looking to extracts on line 1.
We want to pull out 22 because that's the port.
The second line there we want to pull out 25 which is the port for that.
At first thought you may be thinking we can just split this on a colon and print the second feelable
let's just try that and see what happens.
So do cut Dashti for a colon as a delimiter and we'll print the second feel.
So that doesn't give us exactly what we want because we have some extra data on some lines.
And then on other lines we have blank lines.
So as you'll notice up here this line has a three colon so when we do dash F 2 we get nothing to return
because there's no data in the second field for that line.
So using the cut command in that way is not going to work.
Now if we go back to our original data here we see that all the ports we're interested in are actually
in the fourth column and that column being local address.
You can also see that these columns aren't separated by a consistent number of spaces for example.
So that rolls out cut.
But we know that óg handles these kind of whitespace situations well.
So we're going to use that to pull out the fourth column
so we'll narrow it down to the fields I have the data that we're looking for and then we'll print the
fourth column.
Even further narrowing down the data again we're left with a similar situation where cut wouldn't work
because on every line the port numbers that we're after are on the end of the line so and 22 and 25
call and 22 call and 25 call and 68 call and sevens and 5 5 and call and 2 6 3 1 4.
Now I have the answer there in my description of the data which is every one of those is preceded by
a colon so we could actually use that as a delimiter and just print dollar N.F. which is the last field.
So if we have Colin Colin Colin 22 Deller NF is not going to be the same as you know some numbers and
then Colin 22.
So at one case we have two fields in the other case we have more than two fields.
So let's use Aga's specify a colon as the field separator.
And then what we'll do is just print the last field on that line with dollar aneath.
So now we are left with the data that we want.
Now if we just go back and run our regular nets set command here we can see that we're getting data
from TCAP A.S.C. P-Six OTCBB VOR and TCAP V-6 and the same with UDP.
So one way we can just get TCAP before is to use the dash for option 2 nets that so we can do this.
And that eliminates the TPV 6 data there.
I'm not really using TCAP V-6 so I'm not really worried about that.
So let's break it down with this set of data the same idea just extracting the ports that are listening.
So here again we can use grep to either exclude the headers or include the data as something that's
common and we know that a colon is common in the data.
So that leaves us with the data and then we can do something like fill out the fourth field with AWC
and now in this particular case we are left with two columns of data.
The first column being an IP address and the second column being a port.
And those are separated by a colon.
So this would be an ideal situation where we could use cut so we could use cut the SD colon and get
the second field.
Now we're left with 20 to 25 68 and 77 5:5 on this particular system.
It may not look this way on your system.
We could also use the command in this situation as well.
It doesn't have to be cut.
We could use AHC the field separator of a colon and then print a dollar sign to for example that works.
Or we can even go back to our original command and use dollar sign and F for the last field.
Even though we know there's two fields this will still work.
So in this regard it works with Dash 4 and with out the dash 4 it also works with TCAP V-6 and TZP for
let's put this little command into a script so we can have it when we need it later.
This way we don't have to solve this same problem again in the future.
So I'm really just going to copy this and put it in a script.
We're going to allow a dash for option as we were working on that stat so as to only display the CPV
for Portes
So if I pace our command in here this gets us 99 percent of the way.
The last piece here is if we pass in this dollar sign for the simplest way to do this would be to do
this just pass along whatever was passed to our script to the nets that command itself without doing
any checks.
So this is a personal script and I'm not sharing and I'm not worried about there's a good chance that
I would leave it as is.
Let's try it out and then let's talk about some safeguards that we could put in place here.
So we need to make sure it's executable and then run it and it shows us our report.
And then if we use dash for then it limits those ports to the V for protocols.
But if we do something like this then we get an error from net step because dash block is not a valid
option to Netz that and then we could also send all kinds of different things to net stat that we may
or may not want to do.
So if you want to be more exact you could do something like this.
You have dollar sign one is equal to dash for then and do something like that you can put a check like
that.
I'm not going to do that I'm going to leave that as is.
And that works for our situation here.
One last thing that isn't exactly related to scripting but it's kind of useful to know it's the dash
option to next.
And it displays the PID in the name of the program that has the port open.
But to get that information you need to run that stat with super user privileges.
So we can do this
actually let's limit this data down a little bit what this shows is that we have this HD with a head
of 9 0 9 listening on port 22 on our system.
So that's just a handy command to know.
Hey enough random tips.
Let's get back to what you learned here today.
And this lesson you learned how to extract sections from input using the cut command you learn how to
cut based on byte character and field.
You also learn how to specify a delimiter so you can easily work with CSFB files password files and
any other type of data that is organized in columns.
You also learned about the carrot and dollar sign Ingur regular expressions and use them with a grep
command.
Finally we spent some time dealing with awk and how I can handle multi-character delimiters and whitespace
better than cut.