Latest 100 public snipts »
cgv's
snipts
showing 1-15 of 15 snipts
-
∞ tiddlywiki syntax cheatsheet
"bold" //italics// __underline__ @@highlight@@ --strikethru-- super^^script^^ sub~~script~~ !heading level1 !!heading level2 ... *list item1 **list item2 ... #ordered list item1 ##ordered list item2 ... ; definition term : definition description {{{ preformatted }}} [[label|http://]] [img[filename][link]] |!heading| ... | |row1,col1| ... | |...|...| |!footer| ... | -
∞ enable/disable SSH X11 forwarding
#!/bin/bash SSHD_CONFIG=/etc/ssh/sshd_config SSH_SVC=/etc/init.d/sshd v=`grep -m 1 -e "X11Forwarding" ${SSHD_CONFIG} | awk '{ if( $0 ~ /^#/ ) print "no"; else print $2; }'` enabled=0; if [ ${v} == 'yes' ]; then enabled=1; fi case "$1" in enable) if [ ${enabled} -eq 0 ]; then sed -i 's/[#]*X11Forwarding[ ]*[a-z]*/X11Forwarding yes/' ${SSHD_CONFIG} ${SSH_SVC} restart else echo "already enabled" fi ;; disable) if [ ${enabled} -ne 0 ]; then sed -i 's/[#]*X11Forwarding[ ]*[a-z]*/X11Forwarding no/' ${SSHD_CONFIG} ${SSH_SVC} restart else echo "already disabled" fi ;; *) echo "unrecognized option" exit 2 ;; esac exit $?
-
∞ create multiple screen session for a specific function
#!/bin/bash # # it starts multiple screen session whose usual task is remote ssh over # a line separated list of IP's # session_name : the prefix of the name of the screen sessions # script_name : implements the remote ssh code e.g. with python's paramiko # input_file_prefix : the prefix of the files for the script to process # iterations : the number of input files which is equivalent to the screen sessions to be spawned (indexing is zero-based) # if [ $# != 4 ] ; then printf "usage: %s: session_name script_name input_file_prefix iterations\n" $0 exit 1 fi SESSION=$1 SCRIPT=$2 INPUT=$3 NUMBER=$4 awk -v name=$SESSION -v script=$SCRIPT -v file=$INPUT -v times=$NUMBER ' BEGIN{ if( times <= 0 ) exit 2 for( i = 0; i < times; i++ ) { strCommand = sprintf( "screen -S %s%02d -md ./%s %s%02d", name, i, script, file, i ); printf strCommand "\n" system( strCommand ); } }' exit 0
-
∞ retrieves the name of the first ethernet interface
ifconfig -a | grep -i "link[ ]*encap[ ]*:[ ]*ethernet" | awk 'BEGIN{ iface="z" } { if( $1 ~ /^eth/ && iface > $1 ) iface=$1 } END{ print iface }'
-
∞ 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 :-)
-
∞ 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
-
∞ 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
-
∞ 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
-
∞ check ipv4 validity
# the argument must in a dot decimal ipv4 format 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} }
-
∞ change commit log message of a file in specific revision in cvs
cvs admin -m 1.7:"new log message" foo.c -
∞ 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
-
∞ 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
-
∞ 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; }
-
∞ 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'
-
∞ 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; }


