Top Nav

Archive | Bash Scripting

Get IP Address In A Script

Recently needed a good way to fetch the IP address of each interface within a script. Tried things like:

and

But this is fairly ugly. So I tried:

This gives a list of IP addresses but they are un-ordered so I can’t guarantee which address goes with which interface.  Here’s one using “ip addr”:

Still very messy.  Finally, found the “ifdata” command which is part of the “moreutils” package. First make sure “moreutils” is installed with:

Now you can query for a wide range of different information:

So here are some examples:

Overall this is much cleaner and more reliable then the earlier approaches.

0

maxclients.sh Script For Apache

This script attempts to look at Apache memory usage and recommend a stable setting for MaxClients.

 

0

getpass.sh Bash Script

Simple bash script for generating random passwords. We didn’t write this and don’t know exactly where it came from.

 

0

Apache Watch Script

Here’s a trivial old script that I sometimes use to monitor for rough processes running on the apache user:

 

0

How-to Cut With Multi-Character Delimiter

The “cut” command is great for splitting a string on a single character and extracting specific fields. But it will not work with multi-character delimiters. Here’s a simple replacement using awk that will get the job done. Let’s assume you have a string like:

We could extract the value of “arg3” using cut with:

But this assumes that there are always the same number of proceeding args:

Now lets do it with awk:

But what if the args are in different order? The we can use sed:

 

1