A really quick one tonight. It will be nice to actually have some time soon to expound more on these things as this semester winds down (and maybe only one more to finish the Masters!)
Sometimes I want to konw what ports are listening on a server. I can use this information to help troubleshoot a non-working inbound connection or I can use this to make sure that specific ports are NOT listening. Run the below as root or using sudo:
netstat -an | grep -i listen or netstat -an | grep "LISTEN"
This command can, like every unix command I can think of tonight, can be used/piped with other commands, such as awk in order to clean-up/format the output.
Thursday, July 21, 2011
Tuesday, July 19, 2011
ps oneliner: search for a specific running process
Have you ever wanted to verify/search for a running process? Or, have you ever wanted to see if you had multiple counts of the same process, possibly indicating orphans or hung processes?
It's rather easy! As an example, assuming you are logged in as root or su'd, and looking for snort:
ps -ef | grep -v grep | grep snort
The "grep -v grep" command will invert the selection for lines matching "grep"....so it will print ONLY lines that do not contain "grep". Why is this important? Well...it's not. However, the grep command is a process itself so if you have one running snort process, you will get two lines returned:
- the line containing the actual results for the real snort process
- the line containing the grep action(s)
So, assuming that you have only snort running, and running only once, this command would return one line, showing the snort process information (including startup arguments...yay!)
But what if you need to count how many processes? Add the -c switch to the final pipe to grep:
ps -ef | grep -v grep | grep -c snort
This will return an integer value of the number of processes containing snort in the return of the ps command.
It's important to note that this DOES count/show EVERY line of the ps output that contains "snort". This could, if you were running other programs that integrated with snort parts, such as Barnyard, count/show more than one line.
There is a lot more fun that could be had with this. For example, you could search for more than one process, use awk to strip their PIDs, and then find the difference of the two....a quick way to see if one of a group of automatically started programs might be have hung after the other one(s) restarted.
Don't forget these helpful notes too:
grep -i ...case-insensitive
|| ...logical OR operator...look for this OR that
&& ...logical AND operator....must match BOTH
--cat filename1 | grep something1 | grep something2 ...is inherently a logical AND operation
It's rather easy! As an example, assuming you are logged in as root or su'd, and looking for snort:
ps -ef | grep -v grep | grep snort
The "grep -v grep" command will invert the selection for lines matching "grep"....so it will print ONLY lines that do not contain "grep". Why is this important? Well...it's not. However, the grep command is a process itself so if you have one running snort process, you will get two lines returned:
- the line containing the actual results for the real snort process
- the line containing the grep action(s)
So, assuming that you have only snort running, and running only once, this command would return one line, showing the snort process information (including startup arguments...yay!)
But what if you need to count how many processes? Add the -c switch to the final pipe to grep:
ps -ef | grep -v grep | grep -c snort
This will return an integer value of the number of processes containing snort in the return of the ps command.
It's important to note that this DOES count/show EVERY line of the ps output that contains "snort". This could, if you were running other programs that integrated with snort parts, such as Barnyard, count/show more than one line.
There is a lot more fun that could be had with this. For example, you could search for more than one process, use awk to strip their PIDs, and then find the difference of the two....a quick way to see if one of a group of automatically started programs might be have hung after the other one(s) restarted.
Don't forget these helpful notes too:
grep -i ...case-insensitive
|| ...logical OR operator...look for this OR that
&& ...logical AND operator....must match BOTH
--cat filename1 | grep something1 | grep something2 ...is inherently a logical AND operation
Labels:
-ef,
grep,
grep -c,
grep -i,
oneliner scripts,
ps -ef,
ps oneliner
Sunday, July 17, 2011
grep oneliner: get the line you want and its neighbors
Grep is great for printing out a line (or multiple lines) that match a given value. However, I have found it sometimes helpful to search large files, especially log files, at get the line I want plus a few before and after.
If I want to find errors in the /var/log/messages file and I know the line will contain the word "ERROR", I can use the below to get all the lines matching (case-sensitive in this example) as well as 3 before and 3 after.
grep ERROR -B 3 -A 3 /var/log/messages
If I want to find errors in the /var/log/messages file and I know the line will contain the word "ERROR", I can use the below to get all the lines matching (case-sensitive in this example) as well as 3 before and 3 after.
grep ERROR -B 3 -A 3 /var/log/messages
Friday, July 15, 2011
Book Review Pending for ACM: Spyware and Adware
In a few weeks, I think, I am going to have my third book review published in an ACM journal. Yay! While I would prefer to have time to actually do research and write something a little more substantial than a review, I do find the reviews to be a fun and enjoyable experience, as well as a learning one.
This most recent review for SIGACT was actually for a relatively smaller book (less than 200 pages). The book itself is called Spyware and Adware by John Aycock. I am going to withhold any in depth comments, but I will say that this is a book that could be useful for one of the largest ranges of people I can think of for a technical book. It's also part of a bigger series by Springer.
This most recent review for SIGACT was actually for a relatively smaller book (less than 200 pages). The book itself is called Spyware and Adware by John Aycock. I am going to withhold any in depth comments, but I will say that this is a book that could be useful for one of the largest ranges of people I can think of for a technical book. It's also part of a bigger series by Springer.
alias oneliner: make yum installs a little faster
I should preface this with: I KNOW that alias is a oneliner command by its very nature. :-) But sometimes it's just fun to pass on even the little commands. dw
Ever get tired of entering:
yum install WhatIWant
then having to enter y or no to confirm. Or worse, being reminded by the system that you need to be root and then having to:
sudo yum install WhatIWant
An easy thing to do in the bash shell is to use an alias. If you want permanent aliases, you can easily create these as well by creating the ~/.bash_aliases file, which will then run at start up. The file should have one row per alias command, exactly the same way you would enter the below on the command line:
alias myyumi='sudo yum -y install'
After running this command, I am now able to enter the below to install something and have the YES option assumed. The two side notes here are:
1) You must be in the sudoers file to execute this alias
2) If you do not have NOPASSWD set in the sudoers file then you WILL have to enter your password prior to the yum process starting.
I have met the two conditions above and run the alias command. Now I can run:
myyumy WhatIWant
and WhatIWant should install without any further interaction on my part (not accounting for any possible dependency hells that is).
A note on the nameing of my alias:
- I like to use 'my' at the start of aliases as a matter of personal preference....because I made it. :-)
- The 'yum' in the middle should be easy to grasp: it's a representation of the root command, in this case yum. If it was a command like system-config-network then I would use 'snc'
- The 'y' at the end is the parameter(s) I am including in the aliases. Metacharacters can be used in aliases too. So if I wanted to run the system-config-network aliased and in the background I would create the alias like:
alias mysnc&='system-config-network &'
Ever get tired of entering:
yum install WhatIWant
then having to enter y or no to confirm. Or worse, being reminded by the system that you need to be root and then having to:
sudo yum install WhatIWant
An easy thing to do in the bash shell is to use an alias. If you want permanent aliases, you can easily create these as well by creating the ~/.bash_aliases file, which will then run at start up. The file should have one row per alias command, exactly the same way you would enter the below on the command line:
alias myyumi='sudo yum -y install'
After running this command, I am now able to enter the below to install something and have the YES option assumed. The two side notes here are:
1) You must be in the sudoers file to execute this alias
2) If you do not have NOPASSWD set in the sudoers file then you WILL have to enter your password prior to the yum process starting.
I have met the two conditions above and run the alias command. Now I can run:
myyumy WhatIWant
and WhatIWant should install without any further interaction on my part (not accounting for any possible dependency hells that is).
A note on the nameing of my alias:
- I like to use 'my' at the start of aliases as a matter of personal preference....because I made it. :-)
- The 'yum' in the middle should be easy to grasp: it's a representation of the root command, in this case yum. If it was a command like system-config-network then I would use 'snc'
- The 'y' at the end is the parameter(s) I am including in the aliases. Metacharacters can be used in aliases too. So if I wanted to run the system-config-network aliased and in the background I would create the alias like:
alias mysnc&='system-config-network &'
Subscribe to:
Posts (Atom)