bash looping

Basic Loop
#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Number $i"
done

Output:
1
2
3
4
5

Using seq
#!/bin/bash
for i in $(seq 1 2 20)
do
   echo "Number $i"
done

Output:
1
3
5
7
9
11
13
15
17
19

Using the C-style loop:
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
 echo "Number $c"
done

Simple isn't it? If you want an infinite loop:
#!/bin/bash
for (( ; ; ))
do
   echo "infinite loops [ hit CTRL+C to stop]"
done

If you are working with files:
#!/bin/bash
for file in /etc/*
do
     echo $file
done

Those are just the basic. There are tons of tutorial online. I will leave the rest of the topics to you.

whowas.pl - a radius current user viewer

A radius log file parser that displays radius users at a particular point in time. It takes a detail file and a datime/time value as parameter and displays usernames, framed-ip-address and nas-port as output.

Usage:

Make sure to set +x attribute to whowas.pl.
$ chmod +x whowas.pl


Software syntax:

$ whowas.pl <detail_file> <date_time>

e.g.

$ whowas.pl detail "2004-1-10 11:00:00"

The above line will display users currently logged in at "2004-1-10 11:00:00"

Code:
#!/usr/bin/perl
# John Homer H Alvero
# Jan. 11, 2004 

use HTTP::Date;

my $Line;
my @Record;
my $SearchTime = $ARGV[1]; #$ARGV[1];
my $SearchTimestamp = str2time($SearchTime); 

sub ProcessRec {
   my $Header;
   my %AuthRec; 
   my $Fieldname;
   my $Value;
   my $Login;
   my $Logout;
 
   $Header = $Record[0];

   for ($i = 1; $i <= $#Record; $i++) { 
      ($Fieldname, $Value) = split("=",$Record[$i]);
      $Fieldname =~ s/\t//g;
      $Fieldname =~ s/ //g;
      $Value =~ s/ //g;
      $Value =~ s/\"//g;
      chomp($Value); 
      $AuthRec{$Fieldname} = $Value;
   } 
 
   $Login = scalar($AuthRec{"Timestamp"}) -  scalar($AuthRec{"Acct-Session-Time"});
   $Logout = scalar($AuthRec{"Timestamp"});


   if ($AuthRec{"Acct-Status-Type"} =~ /Stop/) {
      if (($SearchTimestamp >= $Login) && ($SearchTimestamp <= $Logout)) {
          print $AuthRec{"User-Name"} . "\t\t" . $AuthRec{"Framed-IP-Address"} . "\t\t" . $AuthRec{"NAS-Port"} . "\n";
          }
   }
}

open(FILE1,$ARGV[0]);

while () {
   $Line = $_;
   if ($Line eq "\n") {
        ProcessRec;
        @Record = ();
        end;
   } else {
        push @Record, $Line;
   }
}

close(FILE1);

You can also download the file here.

Sending / Receiving a file using hping

This method will enable you to send / receive files even through restrictive firewalls. You just have to be creative with the port and protocol.

From the manpage:
hping2 - send (almost) any arbitrary TCP/IP packets to network hosts

Here we go.
On host sending the file, do this:

[host_a]# hping2 host_b --udp -p 53 -d 100 --sign signature --safe --file /etc/passwd

On the receiving host, do this:

[host_b]# hping2 host_a --listen signature --safe --icmp

Be creative with the signature parameter. The parameter string is the key where the receiving side of the connection will start receveing the file. Also, port 53 is usually open for DNS queries and 80 for web traffic.
Good luck.

Sending email direct to qmail-queue with DKIM

#!/usr/bin/perl
# John Homer H Alvero
# Oct 24, 2008

use Mail::QmailQueue;
use Mail::DomainKeys::Message;
use Mail::DomainKeys::Key::Private;

$FinalString = <<EOS;
From: user\@domain.com
MIME-Version: 1.0
Subject: Hello World
To: target\@gmail.com

Test Email! This mail should have valid domain keys.
EOS


open my $fh_message, '<', \ $FinalString;

my $mail = load Mail::DomainKeys::Message(File => $fh_message) or die "unable to load message";
my $priv = load Mail::DomainKeys::Key::Private(File => "/path/to/private/key/file") or die "unable to load key";
$mail->sign(Method => "nofws", Selector => "private", Private => $priv);

$signature = $mail->signature->as_string;


my $qmail = Mail::QmailQueue->new("/var/qmail/bin/qmail-queue");
$qmail->sender('[email protected]');
$qmail->recipient('[email protected]');
$qmail->data('DomainKey-Signature: ' . $signature .';' . "\r\n" . $FinalString);
$qmail->send;

close fh_message;

You can also view the script here.

asterisk cheat-sheet

Just a few asterisk commands:

CommandDescription
reload soft-restarts Asterisk and updates internal configs with changes you’ve made to /etc/asterisk/* - does not hang up calls
sip no debug Disable SIP debugging
show dialplan shows the full dialplan of how your calls will be handled
sip show peers shows all registered SIP clients
sip show channels shows current “live” channels that are in use by SIP clients (off-hook)
sip show registry this command will show you the status of any SIP connections with remote hosts. (eg: Your VOIP carrier.) If you have an authenticated connection with them, it will show as registered otherwise it will show it as unregistered.
sip show users this command will show you a list of all the SIP Users setup in the sip.conf - along with their secret password. This is great for when you go to setup the phones.
database show database Dump
sip debug ip Enable SIP debugging on IP
sip debug peer Enable SIP debugging on Peername
sip no debug Disable SIP debugging
stop gracefully shuts down Asterisk after all calls have hung up
stop now shuts down Asterisk, hanging up any current calls

Outlook 2007 + OpenLdap + CentOS 5.2

1. Install OpenLDAP
yum install openldap-servers.i386

2. Install LDAP clients
yum install openldap-clients.i386

3. Edit the file /etc/openldap/slapd.conf. Make necessary changes specially “dc=companyname,dc=com
suffix “dc=companyname,dc=com”
rootdn “cn=manager,dc=companyname,dc=com”
rootpw {SSHA}wCaiPZjCvjCbQX7xp8j/95zBnl9XQQIj
Note: The rootpw parameter is the hash from the command slappasswd -s test

4. Copy the file /etc/openldap/DB_CONFIG.example to /var/lib/ldap as DB_CONFIG
cp /etc/openldap/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

5. Restart the LDAP service
service ldap restart