Sign up to create your own snipts, or login.

Public snipts » cgv's snipts The latest snipts from cgv.

showing 1-11 of 11 snipts
  • limit rate pings with iptables
    iptables -A INPUT -p icmp -m limit --limit 39.6/m --limit-burst 1 -j DROP
    
    
    # it will cause 2 out of 3 icmp replies to fail when executing a ping like
    # ping -c 3 -i 1 -w 3 10.10.10.10
    #
    # we needed 1 out of 3 successful ping replies in, so that's 2/3 ~= 0.67 replies per second
    # since we cannot use less than 0 values, we up the scale to the minute, so
    # 0.67 * 60 = 39.6 replies in 1 minute :-)
    

    copy | embed

    0 comments - tagged in  posted by cgv on Feb 23, 2010 at 6:26 a.m. EST
  • create and apply a patch between files in two directories
    # produces a patch between the files in the 2 dirs
    
    # -c : special context for output
    # -r : directory recursively
    # -B : ignore blanks
    
    diff -crB before_dir after_dir > foo.patch
    
    # if you want to patch before_dir now
    
    cd before_dir
    
    # -p n : strip the smallest prefix containing n number of slashes 
    # you can do a dry run by adding --dry-run 
    # or use a backup of the file before it gets patched with -b
    
    patch -p1 < ../foo.patch
    

    copy | embed

    0 comments - tagged in  posted by cgv on Feb 03, 2010 at 6:42 a.m. EST
  • elapsed time between two log records
    #!/bin/bash
    
    #
    # parses the time (in seconds) that elapsed between 2 consecutive entries
    # each matching to pattern1 and pattern2.
    # date format : [DDD] [MMM] [dd] [hh]:[mm]:[ss] [YYYY]
    # example     : Tue May 12 06:49:44 2009
    #
    # substitute the patterns at will
    #
    # limitation : it works for entries having the same year
    #
    # example
    # Tue May 12 06:49:41 2009 - pattern1
    # Tue May 12 06:49:44 2009 - pattern2
    #
    # it should output :
    # 3
    #
    
    awk '
    BEGIN{
            m["Jan"]="01";
            m["Feb"]="02";
            m["Mar"]="03";
            m["Apr"]="04";
            m["May"]="05";
            m["Jun"]="06";
            m["Jul"]="07";
            m["Aug"]="08";
            m["Sep"]="09";
            m["Oct"]="10";
            m["Nov"]="11";
            m["Dec"]="12";
    
            d["Sun"]="01";
            d["Mon"]="02";
            d["Tue"]="03";
            d["Wed"]="04";
            d["Thu"]="05";
            d["Fri"]="06";
            d["Sat"]="07";
    
            s=0; # start timestamp
            e=0; # end timestamp
    }
    /pattern1/ {    dt1=$0; gsub(":"," ",$4);
                    spec1=sprintf("%s %s %s %s",$5,d[$1],m[$2],$4);
                    s=mktime(spec1); }
    /pattern2/ {
                    dt2=$0; gsub(":"," ",$4);
                    spec2=sprintf("%s %s %s %s",$5,d[$1],m[$2],$4);
                    e=mktime(spec2); print e-s; e=s=0; }' < $1
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 28, 2010 at 8:52 a.m. EST
  • create pkcs12 cert envelope with openssl
    # mycert.crt - certificate file
    # mykey.key - private key file
    
    openssl pkcs12 -export -in mycert.crt -inkey mykey.key -out env.p12 -password stdin
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 19, 2010 at 3:23 a.m. EST
  • check ipv4 validity
    function is_valid_ipv4() {
      if [ -z "$1" ]; then
        return 0;
      fi
    
      IFS="."
      local isvalid=1
      local bytectr=0
    
      for byte in $1; do
        bytectr=$(( $bytectr + 1 ))
    
        # check if the 1st byte is greater than 0
        if [ ${bytectr} -eq 1 ] && [ ${byte} -le 0 ]; then
          isvalid=0
          break
        fi
    
        if [ ${byte} -lt 0 ] || [ ${byte} -gt 255 ]; then
          isvalid=0
          break
        fi
      done
    
      if [ ${bytectr} -ne 4 ]; then
        isvalid=0
      fi
    
      unset IFS
    
      return ${isvalid}
    }
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 15, 2010 at 7:57 a.m. EST
  • change commit log message of a file in specific revision in cvs
    cvs admin -m 1.7:"new log message" foo.c
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 13, 2010 at 2:51 a.m. EST
  • multicast traffic source/sink with iperf
    # multicast group 239.1.1.1 on port 4321
    
    # multicast listener/sink with read buffer of 512 bytes
    
    iperf -s -u -B 239.1.1.1 -i 1 -l 512 -p 4321
    
    
    # multicast transmitter/source with write buffer of 512 bytes
    # fill up bandwidth of 80Kb/s
    
    iperf -c 239.1.1.1 -u -T 32 -l 512 -i 1 -p 4321 -b 80000
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 11, 2010 at 5:34 a.m. EST
  • sort filezilla server log entries by session id
    #!/bin/bash
    
    #
    # entry example
    #
    # (1579051) 10/16/2009 0:02:13 AM - (not logged in) (192.168.1.11)> USER chris
    #
    # sort -k1n fzs-2009-10-16.log
    # does not work properly because when the session id and timestamp are the same
    # it goes on and tries to sort based on the message
    # if the timestamp granularity was high enough it would work (and so much faster!)
    #
    
    [ $# -ne 1 ] && echo "number of args required is 1" && exit 1
    
    [ ! -e $1 ] && echo "file $1 does not exist" && exit 2
    
    FILEPATH=`dirname $1`
    LOGNAME=`basename $1`
    
    LOGNAME_SORTED=${FILEPATH}/${LOGNAME}".sorted"
    SID_SORTED_FILENAME=${FILEPATH}/"fzlog_sid_sorted.$$"
    
    rm -f $LOGNAME_SORTED
    
    awk '{ split( $1, L, "\\(|\\)" ); print L[ 2 ]; }' $1 | sort | uniq > ${SID_SORTED_FILENAME}
    
    cat ${SID_SORTED_FILENAME} | while read line
    do
            grep "^($line)" $1 >> ${LOGNAME_SORTED}
    done
    
    rm -f ${SID_SORTED_FILENAME}
    
    exit 0
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 05, 2010 at 4:52 a.m. EST
  • breaks up qpushbutton text in to multiple lines
    // header
    
    #include <qobject.h>
    #include <qwidget.h>
    #include <qpushbutton.h>
    
    class LineBreakButton : public QPushButton {
        Q_OBJECT
    
        public:
            LineBreakButton( QWidget *parent, const char *name );
           ~LineBreakButton();
        
            virtual void setText( const QString &text );
    };
    
    
    // part of override implementation
    
    #include <qwidget.h>
    #include <qpushbutton.h>
    #include <qstring.h>
    #include <qstringlist.h>
    #include <qfontmetrics.h>
    
    #include "LineBreakButton.h"
    
    //
    // more trivial stuff here
    //
    
    void
    LineBreakButton::setText( const QString &text ) {
        QString ftext = text.copy();
        QFontMetrics fm = QWidget::fontMetrics();
    
        // split the desired text using any existing newline breaks
        QStringList lines = QStringList::split( "\n", ftext );
    
        QStringList::iterator line = lines.begin();
    
        while ( line != lines.end() ) {
            // use any spaces to further split into words each line that we currently hold
            if ( ( *line ).find( ' ' ) != -1 && fm.width( *line ) >= this->width() ) {
                QStringList words = QStringList::split( " ", *line );
                QStringList::iterator word;
                QString phrase = "";
    
                // try to fit as many words as possible without drawing text out of widget area
                for ( word = words.begin(); word != words.end(); word++ ) {
                    if ( fm.width( phrase + *word + " " ) >= this->width() )
                        break;
    
                    phrase += *word + " ";
                }
    
                // check if there is a single word by its own that exceeds widget width
                // if so, simply draw it and advance the word iterator
                if ( "" == phrase ) {
                    phrase += *word;
                    word++;
                }
    
                // insert the new phrase and advance to the next line
                line = lines.remove( line );
    
                line = lines.insert( line, phrase );
    
                // find the next line after the current one that we have processed
                QStringList::iterator nextline = lines.begin();
    
                for ( ; nextline != lines.end() && nextline != line ; nextline++ )
                    ;
    
                nextline++;
    
                // prepend the rest of the words (if any) to the next line item
                while ( word != words.end() ) {
                    if ( nextline != lines.end() )
                        *nextline += " " + *word;
                    else {
                        lines.append( *word );
                        nextline--;
                    }
    
                    word++;
                }
            } // end of line processing
    
            line++;
        }
    
        QButton::setText( lines.join( "\n" ) );
    
        return;
    }
    

    copy | embed

    0 comments - tagged in  posted by cgv on Jan 04, 2010 at 5:19 a.m. EST
  • tcpdump capture for tcp syn/ack packets
    # capture SYN/ACK flagged packets
    # tcp[13] is the byte location of TCP flags (URG,ACK,PSH,RST,SYN,FIN)
    #
    # mnemonic
    #
    # Unskilled 32
    # Attackers 16
    # Pester     8
    # Real       4
    # Security   2
    # Folks      1
    
    tcpdump -n -i eth0 'tcp[13] & 2 != 0 && tcp[13] & 16 != 0'
    

    copy | embed

    0 comments - tagged in  posted by cgv on Dec 30, 2009 at 9:46 a.m. EST
  • tcpd api usage
    /*
    simple usage of the tcpd/tcp wrapper API
    
    a full-fledged program would probalby call hosts_access() upon accepting a new 
    connection and if denied it would close() 
    the established connection with FIN,ACK or most preferably RST
    
    build with :
    
    gcc -c tcpd_test.c
    
    static libs
    gcc -o tcpd_test tcpd_test.o /usr/lib/libwrap.a /usr/lib/libnsl.a
    
    dynamic libs
    gcc -o tcpd_test tcpd_test.o /usr/lib/libwrap.so /usr/lib/libnsl.so
    
    
    for denying access add this line to /etc/hosts.deny
    
    8888: *
    
    */
    
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <tcpd.h>
    
    
    int main( int argc, char *argv[] ) {
            struct request_info rq;
            struct sockaddr_in myserver_address;
            int rc = 0;
    
            /* setup the criteria */
            myserver_address.sin_family = AF_INET;
            myserver_address.sin_port = htons( 8888 );
            myserver_address.sin_addr.s_addr = INADDR_ANY;
    
            /* initialize the request structure with our criteria */
            request_init( &rq, RQ_SERVER_SIN, &myserver_address, 0 );
    
            /* call fromhosts() before calling hosts_access() as mentioned in the manual */
            fromhost( &rq );
    
            rc = hosts_access( &rq );
    
            fprintf( stderr, "access %s (%d)\n", (rc) ? "granted" : "denied", rc );
    
            return rc;
    }
    

    copy | embed

    0 comments - tagged in  posted by cgv on Dec 28, 2009 at 9:50 a.m. EST
Sign up to create your own snipts, or login.