Posts Tagged cli

LCOD – 11.17.09 – Simple page/alert email script

Here’s a quick page script for *nix boxes with mail.
I grabbed the mail / file redirect from

http://theos.in/shell-scripting/send-mail-bash-script/


#!/bin/bash
#Author : dougnaka@gmail.comThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it
#Description : Quick alert/page script, edit EMAIL="" to include a comma separated list of emails to send to
#Usage : page.sh "Short or long message"
if [ "${1}" ]
then
SUBJECT="[ALERT] ${1}"
EMAIL=" someone@example.com"
EMAILMESSAGE="/tmp/`date +%s`-message"
echo "ALERT: at `date`" > ${EMAILMESSAGE}
echo "${1}" >> ${EMAILMESSAGE}
echo "From `hostname`" >> ${EMAILMESSAGE}
mail -s "${SUBJECT}" "${EMAIL}" < ${EMAILMESSAGE}
/bin/rm ${EMAILMESSAGE}
else
echo "Usage: ${0} MESSAGE"
exit 1
fi

, , , ,

No Comments

LCOD 10.9.09 – Script to retry command

I use this almost exclusively with trying to ssh to a machine that’s rebooting, but it’s useful to have, and, I think, should be included as a default command in any Linux distro.

Script : retry
Usage : retry command [args]

Code :

#!/bin/bash
while true
do
echo "trying $@ at `date`"
$@
sleep 1
done

Enjoy.

, , , ,

No Comments

LCOD – 6.18.09 Useful command line grepping (grep!)

There are a few regular expressions I find myself using all the time via grep on the command line. Here are a few, along with a few frequently used commands I can’t live without.

Find the non comment parts of a file.
(Useful if you want to see or compare the active parts of a config file.)
Most Unix config files use # as a comment tag, indicating everything after the # is a comment.
grep ‘^[^#]‘ filename.conf

To break this down, first we have grep , which is a command line program to find lines that match a regular expression pattern, and display them. The single quotes indicate that we’re passing the entire section enclosed in single quotes as the first argument, which doesn’t matter as much in this example, but it’s good to practice, so when you have a space or some other character, you won’t be confused when your shell interprits it. The regular expression used is
^[^#]
Which is slightly trick, as far as regular expressions go, for only one reason; the character ^ is used twice, with different meanings.
Normally the ^ is regular expression for the start of a line, and we use it here as this, the first time. The second time, inside the brackets, it means NOT.
Brackets in regular expressions are like a giant OR statement, with every non-escape character (like the forward slash / ) as a possible match. This can hang people up when they expect [bob] to match the full word bob, instead of just the first b.
Then we have the #, which just matches our comment character, #.
Then the closing bracket, since we’re not doing any other options.
The net result of this is to match any line that has a # right after the start of the line.

To do the same thing on a config file that uses a ; as a comment indicator:
grep ‘^[;]‘ filename.conf

OR, to be even trickier, you could just add the # and ; in the same set of brackets, like
grep ‘^[;#] fliename.conf

Note the order of items inside the bracket doesn’t really matter in this case.

Next, often times I have a file with a bunch of empty lines in it, and I want to quickly get rid of them all. To do this I use a combination of a simple grep and a output redirect.

grep . filename
The . character is a regular expression match for any character, but it won’t match empty lines (the regular expression ^$ does though).
To get this into the file, first redirect it to a new file, then move the file back.
grep . filename > newfile
mv newfile filename

or, if you want to sort, just run
sort newfile > filename

perhaps you now want to just get the unique lines
uniq newfile > filename

Of course, I should mention the very obvious ones.
Grepping for words in files.

grep Word filename

Note that grep is case sensitive, but you can turn that off with the -i flag.

grep -i word filename

There is an infinite amount of uses for grep, but the foundation for grep/sed/awk/perl use for patterns is regular expressions. More than any single book I’ve read about techonlogy, the O’Reilly book “Mastering Regular Expressions”, by Jeffrey Friedl has helped me the most in my IT career. I highly suggest you buy and read it.

, , , ,

No Comments

LCOD – 12.6.05 – finding what programs are on what ports

So, you’ve run netstat -an and found something listening on a port you’re not sure what it is?

Not to worry, you can always run this command to find out;
Substitue $PORT with your actual port.

lsof -i | grep $PORT

So, say you’d run
netstat -an | grep LIST |grep tcp
and found this process
tcp 0 0 0.0.0.0:32803 0.0.0.0:* LISTEN
Not sure what 32803 was you checked /etc/services
grep 32803 /etc/services
and got nothing!

So, now you run
lsof -i | grep 32803
and you see

skype 18160 mdxdoug 22u IPv4 101807497 TCP *:32803 (LISTEN)
skype 18160 mdxdoug 23u IPv4 101807498 UDP *:32803

So it’s SKYPE! and not backdoor.pl or something. Good to know.

Enjoy!

NOTE:
If you’re using this to find a service that IS in /etc/services, you should grep for the service by name instead of port.

grep 143 /etc/services

imap 143/tcp imap2 imap4 #Interim Mail Access Protocol v2

lsof -L | grep imap

, , , ,

No Comments

LCOD – 11.3.03 – command line network setup

If you’ve installed a major distribution of Linux then most likely you’ve configured your Networking via the install GUI. Now, what if you want to change your network options, but don’t want to mess with the GUI’s available to you, or say you want to script something network related. Here are some commands you can use to change your network settings. Note, these will be reset when you reboot, if you need to change them permenantly you’ll need to find your config files, or use the provided GUI.

First, the clincher, ifconfig. Type it alone at the command line, you’ll see all your network interfaces listed with the accompanying settings. You should see eth0 and lo at a minimum. Eth0 is your Ethernet interface 0. Ethernet is the most common local area network type (LAN). Lo is your loopback device, it is always 127.0.0.1 and is used for internal network communication. This is a way Unix uses to make programs easily translatable from local use to network use. So, your ethernet interface is the one we’ll be concerned about. To see just it’s configuration type
ifconfig eth0
If you get a message like error fetching device info, then you’re driver for your network card isn’t installed. The quickest way to get it working, if you’re on one of the main distros, is to find which card it is, then which driver it uses ,then load the driver/module manually into the kernel with modprobe modulename. So, if you’ve got an Intel EtherExpress pro100/B, then modprobe pro100. lsmod will tell you what modules you’ve currently got loaded in your kernel, and rmmod modulename will remove an unused module from a running kernel.

So, what can we do with ifconfig. Well, the simplest thing is setup your IP address. For this example, let’s say 192.168.0.50 is what you want your IP address to be, and 192.168.0.0/24 is your network, 192.168.0.254 is your gateway, and 192.168.0.1 is your domain name server (DNS server).
Let’s bring up that interface with those configs, we do that like this, (all on one line):

ifconfig eth0 192.168.0.50 netmask 255.255.255.0 broadcast 192.168.0.255

Now ifconfig eth0 should show us having an inet address of 192.168.0.50. It should also say the keyword UP in there, this tells you the interface is up and working.

Now you should be able to ping it, ping 192.168.0.50.
Remember, in Unix/Linux/*BSD the ping command doesn’t stop after 4 pings, it keeps going forever, until you hit CTRL+C.
This will work even if your network isn’t plugged in, since it’s just talking locally to the computer.
You should also be able to ping your default gateway, ping 192.168.0.254. Remember the CTRL+C to stop the pings.

Now, your interface is up, but you can’t go anywhere. This is becuase you don’t have a default gateway set. Let’s jump on over to the route command. Start with
route -n
This will display your routes, you really only need 3 routes, your local loopback (127.0.0.1), your local area network (192.168.0.0), and your default gateway. If you just added an interface manually, or changed the existing one, then it’s likely all associated routes, such as your old default gateway, were dropped. To add a new default gateway type
route add -net default gw 192.168.0.254
This will setup 192.168.0.254 as your default gateway. What this does is basically tell your computer that if it doesn’t see the computer on the network locally, to forward the packets to the default gateway and let it handle it. This is how 90% of Internet routing works. Any site that has 1 connection to the Internet, only needs 1 default gateway.

Now, back to ipconfig for something very useful. Say you want to have 2 IP addresses, but only have 1 physical network interface. Maybe you want to run a web server and not use Name based virtual hosting. You can add and remove interfaces to your eth0 by just using a colon and a number, and then configuring just like we did eth0.
So, if we want to add 192.168.0.51 to eth0, without losing 192.168.0.50, we type (all on one line)
ifconfig eth0:0 192.168.0.51 netmask 255.255.255.0 broadcast 192.168.0.255

Now ifconfig eth0:0 will tell us information about our new sub-interface, and ping 192.168.0.51 will ping ourselves and we’ll get replies.
This can be added as many times as you want. I’m sure there is some limit, maybe 65535 or 256. I’ve used it in production with 250 addresses on 1 interface without any problems.

, , , , ,

No Comments

Easy AdSense by Unreal