Posts Tagged script

LCOD 5.29.2011 – 2 quick TCP port check scripts in Python

Disclaimer : These scripts could be considered malicious and should only be used to test servers with permission from the server administrator. I accept no responsibility for your use of these scripts, and make no warranty about their usefulness either express or implied.

 

I often find myself troubleshooting someone’s network connectivity, or one of my servers ability to receive connections or mass connections.

For this, nmap is invaluable, but sometimes it’s easier to just send someone a script to run, especially in larger companies where things like nmap, and other port scanners are frowned on.

So, here are 2 scripts that take the same arguments HOST PORT.

They are tcpcheck.py (here is the gzipped version), and tcpcheck-bulk.py (here is the gzipped version).

tcpcheck.py makes 1 connection to a TCP port and reports success or failure.

Example usage and output  :

./tcpcheck.py jonzobrist.com 80
Success. Connected to jonzobrist.com on port: 80

./tcpcheck.py jonzobrist.com 81
Failure. Cannot connect to jonzobrist.com on port: 81

tcpcheck-bulk.py is hard wired to attempt 1500 connections to a TCP port and reports success/failures.

Example usage and output is :

./tcpcheck-bulk.py jonzobrist.com 80
Done with jonzobrist.com on port: 80
Done. Failures : 0 Successes : 1500

This script was very useful when we tried to determine server performance over a link that seemed to be killing our connections.

Please feel free to use it, and share if it helped.

, , , , , , , ,

No Comments

Delete lots of EC2 EBS volumes

Here is a quick script to delete lots of EBS volumes on Amazon EC2.

The output from something like

ec2-describe-instances i-6fc30f01 | grep ‘^BLOCKDEVICE’ | awk ‘{ print $3 }’ > delete-these-volumes.txt

Then run

./delete-volumes.sh delete-these-volumes.txt

Can be fed into this script (you may want to edit the file first, I accept no responsibility for you deleting your volumes, with or without my script.

Download the gzipped script here.

 

#!/bin/bash
if [ "${1}" ]
then
for V in `cat ${1}`
do
ec2-detach-volume ${V}
echo “Waiting for ${V} to detach”
while ! ec2-describe-volumes ${V} | grep -q available
do
echo -n “.”
sleep 1
done
ec2-delete-volume ${V}
echo “Volume ${V} deleted at `date`”
done
else
echo “Usage : ${0} filename”
echo “Where filename is a file with volumes to be deleted”
exit 1
fi

Example output from me deleting 60 real volumes:

time delete-volumes-list.sh i-6fc30f01.info.delvolumes
Client.IncorrectState: Volume ‘vol-3bab1d50′is in the ‘available’ state.
Waiting for vol-3bab1d50 to detach
VOLUME vol-3bab1d50
Volume vol-3bab1d50 deleted at Mon May 23 10:53:56 MDT 2011
ATTACHMENT vol-33ab1d58 i-6fc30f01 /dev/sdh2 detaching 2011-05-23T16:15:08+0000
Waiting for vol-33ab1d58 to detach
VOLUME vol-33ab1d58
Volume vol-33ab1d58 deleted at Mon May 23 10:54:08 MDT 2011
ATTACHMENT vol-07ab1d6c i-6fc30f01 /dev/sdh3 detaching 2011-05-23T16:15:08+0000
Waiting for vol-07ab1d6c to detach
VOLUME vol-07ab1d6c
Volume vol-07ab1d6c deleted at Mon May 23 10:54:21 MDT 2011
ATTACHMENT vol-17ab1d7c i-6fc30f01 /dev/sdh4 detaching 2011-05-23T16:15:08+0000
Waiting for vol-17ab1d7c to detach
VOLUME vol-17ab1d7c
Volume vol-17ab1d7c deleted at Mon May 23 10:54:33 MDT 2011
ATTACHMENT vol-f9ab1d92 i-6fc30f01 /dev/sdh5 detaching 2011-05-23T16:15:08+0000
Waiting for vol-f9ab1d92 to detach
VOLUME vol-f9ab1d92
Volume vol-f9ab1d92 deleted at Mon May 23 10:54:46 MDT 2011
ATTACHMENT vol-c1ab1daa i-6fc30f01 /dev/sdh6 detaching 2011-05-23T16:15:08+0000
Waiting for vol-c1ab1daa to detach
VOLUME vol-c1ab1daa
Volume vol-c1ab1daa deleted at Mon May 23 10:54:58 MDT 2011
ATTACHMENT vol-ddab1db6 i-6fc30f01 /dev/sdh7 detaching 2011-05-23T16:15:08+0000
Waiting for vol-ddab1db6 to detach
VOLUME vol-ddab1db6
Volume vol-ddab1db6 deleted at Mon May 23 10:55:12 MDT 2011
ATTACHMENT vol-a5ab1dce i-6fc30f01 /dev/sdh8 detaching 2011-05-23T16:15:08+0000
Waiting for vol-a5ab1dce to detach
VOLUME vol-a5ab1dce
Volume vol-a5ab1dce deleted at Mon May 23 10:55:24 MDT 2011
ATTACHMENT vol-8bab1de0 i-6fc30f01 /dev/sdh9 detaching 2011-05-23T16:15:08+0000
Waiting for vol-8bab1de0 to detach
VOLUME vol-8bab1de0
Volume vol-8bab1de0 deleted at Mon May 23 10:55:37 MDT 2011
ATTACHMENT vol-85ab1dee i-6fc30f01 /dev/sdh10 detaching 2011-05-23T16:15:08+0000
Waiting for vol-85ab1dee to detach
VOLUME vol-85ab1dee
Volume vol-85ab1dee deleted at Mon May 23 10:55:49 MDT 2011
ATTACHMENT vol-67a81e0c i-6fc30f01 /dev/sdh11 detaching 2011-05-23T16:15:08+0000
Waiting for vol-67a81e0c to detach
VOLUME vol-67a81e0c
Volume vol-67a81e0c deleted at Mon May 23 10:56:01 MDT 2011
ATTACHMENT vol-4da81e26 i-6fc30f01 /dev/sdh12 detaching 2011-05-23T16:15:08+0000
Waiting for vol-4da81e26 to detach
VOLUME vol-4da81e26
Volume vol-4da81e26 deleted at Mon May 23 10:56:17 MDT 2011
ATTACHMENT vol-57a81e3c i-6fc30f01 /dev/sdh13 detaching 2011-05-23T16:15:08+0000
Waiting for vol-57a81e3c to detach
VOLUME vol-57a81e3c
Volume vol-57a81e3c deleted at Mon May 23 10:56:29 MDT 2011
ATTACHMENT vol-1fa81e74 i-6fc30f01 /dev/sdh14 detaching 2011-05-23T16:15:08+0000
Waiting for vol-1fa81e74 to detach
VOLUME vol-1fa81e74
Volume vol-1fa81e74 deleted at Mon May 23 10:56:42 MDT 2011
ATTACHMENT vol-c9a81ea2 i-6fc30f01 /dev/sdi1 detaching 2011-05-23T16:15:08+0000
Waiting for vol-c9a81ea2 to detach
VOLUME vol-c9a81ea2
Volume vol-c9a81ea2 deleted at Mon May 23 10:56:54 MDT 2011
ATTACHMENT vol-afa81ec4 i-6fc30f01 /dev/sdi2 detaching 2011-05-23T16:15:08+0000
Waiting for vol-afa81ec4 to detach
VOLUME vol-afa81ec4
Volume vol-afa81ec4 deleted at Mon May 23 10:57:07 MDT 2011
ATTACHMENT vol-9fa81ef4 i-6fc30f01 /dev/sdi3 detaching 2011-05-23T16:15:08+0000
Waiting for vol-9fa81ef4 to detach
VOLUME vol-9fa81ef4
Volume vol-9fa81ef4 deleted at Mon May 23 10:57:20 MDT 2011
ATTACHMENT vol-6fa91f04 i-6fc30f01 /dev/sdi4 detaching 2011-05-23T16:15:08+0000
Waiting for vol-6fa91f04 to detach
VOLUME vol-6fa91f04
Volume vol-6fa91f04 deleted at Mon May 23 10:57:33 MDT 2011
ATTACHMENT vol-7da91f16 i-6fc30f01 /dev/sdi5 detaching 2011-05-23T16:15:08+0000
Waiting for vol-7da91f16 to detach
VOLUME vol-7da91f16
Volume vol-7da91f16 deleted at Mon May 23 10:57:45 MDT 2011
ATTACHMENT vol-4fa91f24 i-6fc30f01 /dev/sdi6 detaching 2011-05-23T16:15:08+0000
Waiting for vol-4fa91f24 to detach
VOLUME vol-4fa91f24
Volume vol-4fa91f24 deleted at Mon May 23 10:57:58 MDT 2011
ATTACHMENT vol-57a91f3c i-6fc30f01 /dev/sdi7 detaching 2011-05-23T16:15:08+0000
Waiting for vol-57a91f3c to detach
VOLUME vol-57a91f3c
Volume vol-57a91f3c deleted at Mon May 23 10:58:10 MDT 2011
ATTACHMENT vol-33a91f58 i-6fc30f01 /dev/sdi8 detaching 2011-05-23T16:15:08+0000
Waiting for vol-33a91f58 to detach
VOLUME vol-33a91f58
Volume vol-33a91f58 deleted at Mon May 23 10:58:24 MDT 2011
ATTACHMENT vol-07a91f6c i-6fc30f01 /dev/sdi9 detaching 2011-05-23T16:15:08+0000
Waiting for vol-07a91f6c to detach
VOLUME vol-07a91f6c
Volume vol-07a91f6c deleted at Mon May 23 10:58:36 MDT 2011
ATTACHMENT vol-fba91f90 i-6fc30f01 /dev/sdi10 detaching 2011-05-23T16:15:08+0000
Waiting for vol-fba91f90 to detach
VOLUME vol-fba91f90
Volume vol-fba91f90 deleted at Mon May 23 10:58:49 MDT 2011
ATTACHMENT vol-fda91f96 i-6fc30f01 /dev/sdi11 detaching 2011-05-23T16:15:08+0000
Waiting for vol-fda91f96 to detach
VOLUME vol-fda91f96
Volume vol-fda91f96 deleted at Mon May 23 10:59:01 MDT 2011
ATTACHMENT vol-c9a91fa2 i-6fc30f01 /dev/sdi12 detaching 2011-05-23T16:15:08+0000
Waiting for vol-c9a91fa2 to detach
VOLUME vol-c9a91fa2
Volume vol-c9a91fa2 deleted at Mon May 23 10:59:14 MDT 2011
ATTACHMENT vol-afa91fc4 i-6fc30f01 /dev/sdi13 detaching 2011-05-23T16:15:08+0000
Waiting for vol-afa91fc4 to detach
VOLUME vol-afa91fc4
Volume vol-afa91fc4 deleted at Mon May 23 10:59:27 MDT 2011
ATTACHMENT vol-bda91fd6 i-6fc30f01 /dev/sdi14 detaching 2011-05-23T16:15:08+0000
Waiting for vol-bda91fd6 to detach
VOLUME vol-bda91fd6
Volume vol-bda91fd6 deleted at Mon May 23 10:59:39 MDT 2011
ATTACHMENT vol-85a91fee i-6fc30f01 /dev/sdj1 detaching 2011-05-23T16:15:08+0000
Waiting for vol-85a91fee to detach
VOLUME vol-85a91fee
Volume vol-85a91fee deleted at Mon May 23 10:59:52 MDT 2011
ATTACHMENT vol-6d962006 i-6fc30f01 /dev/sdj2 detaching 2011-05-23T16:15:08+0000
Waiting for vol-6d962006 to detach
VOLUME vol-6d962006
Volume vol-6d962006 deleted at Mon May 23 11:00:05 MDT 2011
ATTACHMENT vol-7196201a i-6fc30f01 /dev/sdj3 detaching 2011-05-23T16:15:08+0000
Waiting for vol-7196201a to detach
VOLUME vol-7196201a
Volume vol-7196201a deleted at Mon May 23 11:00:17 MDT 2011
ATTACHMENT vol-59962032 i-6fc30f01 /dev/sdj4 detaching 2011-05-23T16:15:08+0000
Waiting for vol-59962032 to detach
VOLUME vol-59962032
Volume vol-59962032 deleted at Mon May 23 11:00:29 MDT 2011
ATTACHMENT vol-2196204a i-6fc30f01 /dev/sdj5 detaching 2011-05-23T16:15:08+0000
Waiting for vol-2196204a to detach
VOLUME vol-2196204a
Volume vol-2196204a deleted at Mon May 23 11:00:42 MDT 2011
ATTACHMENT vol-3596205e i-6fc30f01 /dev/sdj6 detaching 2011-05-23T16:15:08+0000
Waiting for vol-3596205e to detach
VOLUME vol-3596205e
Volume vol-3596205e deleted at Mon May 23 11:00:54 MDT 2011
ATTACHMENT vol-0796206c i-6fc30f01 /dev/sdj7 detaching 2011-05-23T16:15:08+0000
Waiting for vol-0796206c to detach
VOLUME vol-0796206c
Volume vol-0796206c deleted at Mon May 23 11:01:07 MDT 2011
ATTACHMENT vol-ed962086 i-6fc30f01 /dev/sdj8 detaching 2011-05-23T16:15:08+0000
Waiting for vol-ed962086 to detach
VOLUME vol-ed962086
Volume vol-ed962086 deleted at Mon May 23 11:01:19 MDT 2011
ATTACHMENT vol-ff962094 i-6fc30f01 /dev/sdj9 detaching 2011-05-23T16:15:08+0000
Waiting for vol-ff962094 to detach
VOLUME vol-ff962094
Volume vol-ff962094 deleted at Mon May 23 11:01:32 MDT 2011
ATTACHMENT vol-fd962096 i-6fc30f01 /dev/sdj10 detaching 2011-05-23T16:15:08+0000
Waiting for vol-fd962096 to detach
VOLUME vol-fd962096
Volume vol-fd962096 deleted at Mon May 23 11:01:45 MDT 2011
ATTACHMENT vol-d39620b8 i-6fc30f01 /dev/sdj11 detaching 2011-05-23T16:15:08+0000
Waiting for vol-d39620b8 to detach
VOLUME vol-d39620b8
Volume vol-d39620b8 deleted at Mon May 23 11:01:57 MDT 2011
ATTACHMENT vol-a59620ce i-6fc30f01 /dev/sdj12 detaching 2011-05-23T16:15:08+0000
Waiting for vol-a59620ce to detach
VOLUME vol-a59620ce
Volume vol-a59620ce deleted at Mon May 23 11:02:10 MDT 2011
ATTACHMENT vol-6f972104 i-6fc30f01 /dev/sdj13 detaching 2011-05-23T16:15:08+0000
Waiting for vol-6f972104 to detach
VOLUME vol-6f972104
Volume vol-6f972104 deleted at Mon May 23 11:02:23 MDT 2011
ATTACHMENT vol-5d972136 i-6fc30f01 /dev/sdj14 detaching 2011-05-23T16:15:08+0000
Waiting for vol-5d972136 to detach
VOLUME vol-5d972136
Volume vol-5d972136 deleted at Mon May 23 11:02:36 MDT 2011
ATTACHMENT vol-3b972150 i-6fc30f01 /dev/sdk1 detaching 2011-05-23T16:15:08+0000
Waiting for vol-3b972150 to detach
VOLUME vol-3b972150
Volume vol-3b972150 deleted at Mon May 23 11:02:48 MDT 2011
ATTACHMENT vol-3797215c i-6fc30f01 /dev/sdk2 detaching 2011-05-23T16:15:08+0000
Waiting for vol-3797215c to detach
VOLUME vol-3797215c
Volume vol-3797215c deleted at Mon May 23 11:03:01 MDT 2011
ATTACHMENT vol-1d972176 i-6fc30f01 /dev/sdk3 detaching 2011-05-23T16:15:08+0000
Waiting for vol-1d972176 to detach
VOLUME vol-1d972176
Volume vol-1d972176 deleted at Mon May 23 11:03:13 MDT 2011
ATTACHMENT vol-ef972184 i-6fc30f01 /dev/sdk4 detaching 2011-05-23T16:15:08+0000
Waiting for vol-ef972184 to detach
VOLUME vol-ef972184
Volume vol-ef972184 deleted at Mon May 23 11:03:26 MDT 2011
ATTACHMENT vol-fd972196 i-6fc30f01 /dev/sdk5 detaching 2011-05-23T16:15:08+0000
Waiting for vol-fd972196 to detach
VOLUME vol-fd972196
Volume vol-fd972196 deleted at Mon May 23 11:03:38 MDT 2011
ATTACHMENT vol-c19721aa i-6fc30f01 /dev/sdk6 detaching 2011-05-23T16:15:08+0000
Waiting for vol-c19721aa to detach
VOLUME vol-c19721aa
Volume vol-c19721aa deleted at Mon May 23 11:03:51 MDT 2011
ATTACHMENT vol-af9721c4 i-6fc30f01 /dev/sdk7 detaching 2011-05-23T16:15:08+0000
Waiting for vol-af9721c4 to detach
VOLUME vol-af9721c4
Volume vol-af9721c4 deleted at Mon May 23 11:04:03 MDT 2011
ATTACHMENT vol-a59721ce i-6fc30f01 /dev/sdk8 detaching 2011-05-23T16:15:08+0000
Waiting for vol-a59721ce to detach
VOLUME vol-a59721ce
Volume vol-a59721ce deleted at Mon May 23 11:04:16 MDT 2011
ATTACHMENT vol-8d9721e6 i-6fc30f01 /dev/sdk9 detaching 2011-05-23T16:15:08+0000
Waiting for vol-8d9721e6 to detach
VOLUME vol-8d9721e6
Volume vol-8d9721e6 deleted at Mon May 23 11:04:28 MDT 2011
ATTACHMENT vol-9d9721f6 i-6fc30f01 /dev/sdk10 detaching 2011-05-23T16:15:08+0000
Waiting for vol-9d9721f6 to detach
VOLUME vol-9d9721f6
Volume vol-9d9721f6 deleted at Mon May 23 11:04:41 MDT 2011
ATTACHMENT vol-33942258 i-6fc30f01 /dev/sdk11 detaching 2011-05-23T16:15:08+0000
Waiting for vol-33942258 to detach
VOLUME vol-33942258
Volume vol-33942258 deleted at Mon May 23 11:04:53 MDT 2011
ATTACHMENT vol-1794227c i-6fc30f01 /dev/sdk12 detaching 2011-05-23T16:15:08+0000
Waiting for vol-1794227c to detach
VOLUME vol-1794227c
Volume vol-1794227c deleted at Mon May 23 11:05:06 MDT 2011
ATTACHMENT vol-fb942290 i-6fc30f01 /dev/sdk13 detaching 2011-05-23T16:15:08+0000
Waiting for vol-fb942290 to detach
VOLUME vol-fb942290
Volume vol-fb942290 deleted at Mon May 23 11:05:19 MDT 2011
ATTACHMENT vol-d99422b2 i-6fc30f01 /dev/sdk14 detaching 2011-05-23T16:15:08+0000
Waiting for vol-d99422b2 to detach
VOLUME vol-d99422b2
Volume vol-d99422b2 deleted at Mon May 23 11:05:31 MDT 2011
ATTACHMENT vol-839422e8 i-6fc30f01 /dev/sdl1 detaching 2011-05-23T16:15:08+0000
Waiting for vol-839422e8 to detach
VOLUME vol-839422e8
Volume vol-839422e8 deleted at Mon May 23 11:05:44 MDT 2011
ATTACHMENT vol-9d9422f6 i-6fc30f01 /dev/sdl2 detaching 2011-05-23T16:15:08+0000
Waiting for vol-9d9422f6 to detach
VOLUME vol-9d9422f6
Volume vol-9d9422f6 deleted at Mon May 23 11:05:56 MDT 2011
ATTACHMENT vol-6595230e i-6fc30f01 /dev/sdl3 detaching 2011-05-23T16:15:08+0000
Waiting for vol-6595230e to detach
VOLUME vol-6595230e
Volume vol-6595230e deleted at Mon May 23 11:06:10 MDT 2011
ATTACHMENT vol-7d952316 i-6fc30f01 /dev/sdl4 detaching 2011-05-23T16:15:08+0000
Waiting for vol-7d952316 to detach
VOLUME vol-7d952316
Volume vol-7d952316 deleted at Mon May 23 11:06:22 MDT 2011
real 12m38.753s
user 18m46.140s
sys 0m38.000s

, , , , , , ,

1 Comment

My EC2 EBS RAID-0 creation script

I want to write up a full howto/tutorial, but have not had the time.

Here is the script that I referred to in my post at http://jonzobrist.com/2011/04/16/ec2-raid0-on-linux-with-ebs-volumes-notes/

This program is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License HERE for more details.

http://jonzobrist.com/files/setup-ec2-raid-0.zip

http://jonzobrist.com/files/setup-ec2-raid-0.sh.gz

This script needs an AWS command line setup, and may need some minor tweaking if you’re not running a Ubuntu server.

Ubuntu’s latest AMI’s are available for 10.04 here.

Please feel free to submit patches, comments, or questions.

Thanks to everyone whose helpful posts online and in the AWS forums helped me with this script. I could not have done it with out your generous sharing of work.

Enjoy!

 

, , , , , , , , , , ,

No Comments

LCOD – 4.12.10 – Quick Mysqlreport to e-mail setup

This will be a quick install to setup your server to e-mail you daily mysql reports using the cool mysqlreport application at hackmysql.com

Click to continue reading “LCOD – 4.12.10 – Quick Mysqlreport to e-mail setup”

, , , , , , , ,

No Comments

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 11.14.07 – Quick RAID check script for cron

This is a quick script that will let you run the new raid checks, or repair, or pause a running raid check. It doesn’t do e-mails or anything, and from what I read on this thread it’s something mdadm needs to be set to watch for. I ran the check on a RAID and received no output, so I guess it was ok?

I will update the script as I find out more. I’d like to add a simple escalation from check to rebuild? (I think it does this anyways), and an e-mail status report, or even a syslog call saying I ran all things look good.


#!/bin/bash
###############
# Quick script to use raid functionality
# Author : Jon Zobrist < jon@bluesun.netThis e-mail address is being protected from spam bots, you need JavaScript enabled to view it >
# http://www.bluesun.net/
# License : LGPL
# Version : 1.0
# Date : 11.14.07
# install/notes:
# put it somewhere, /root/bin/ is where I prefer
# put this in your root crontab to run from cron monthly
# @monthly /root/bin/checkraids check > /root/logs/cron.log 2>&1
###############

case "$1" in
check)
for raid in /sys/block/md*/md/sync_action
do
echo check > $raid
done
;;

repair)
for raid in /sys/block/md*/md/sync_action
do
echo repair > $raid
done
;;

pause)
for raid in /sys/block/md*/md/sync_action
do
echo idle > $raid
done
;;

status)
for raid in /sys/block/md*/md/sync_action
do
echo "******************************"
echo $raid `cat $raid`
echo ""
done
;;

*)
echo "Usage : $0 check|repair|pause|status"
;;
esac

, , , , , ,

No Comments

Easy AdSense by Unreal