Permalink
Browse files

more scripts

  • Loading branch information...
1 parent 3b707a6 commit 65c58e6e415fd2efb672a820a941dc6b0996a691 @poliva committed Sep 14, 2012
Showing with 158 additions and 0 deletions.
  1. +37 −0 awk/cip.awk
  2. +27 −0 awk/parse-csv.awk
  3. +37 −0 bwmeter/bwmeter.sh
  4. +35 −0 sql/create-db.sh
  5. +3 −0 sql/csv2sql.sh
  6. +3 −0 sql/sql2csv.sh
  7. +16 −0 trim/trim.pl
View
37 awk/cip.awk
@@ -0,0 +1,37 @@
+#!/usr/bin/awk -f
+#
+# Convert IP addresses between dotted decimal and binary.
+# 'Binary' means 32 bits interpreted as a signed int.
+# Sorry, can't handle IPv6.
+#
+# run like this: echo "167772172" |./cip.awk
+
+#BEGIN {
+# FS=","
+#}
+
+function ltrim(s) { sub(/^[ \t]+/, "", s); return s }
+function rtrim(s) { sub(/[ \t]+$/, "", s); return s }
+function trim(s) { return rtrim(ltrim(s)); }
+function cip(s) {
+ sign=substr(s,0,1);
+ if (sign == "-") {
+ s = substr(s,2)
+ s = strtonum(0xffffffff)-strtonum(s)+1;
+ }
+ hex = sprintf("%08X",s);
+ oct1=substr(hex,0,2);
+ oct1=strtonum(sprintf("0x%s",oct1));
+ oct2=substr(hex,3,2);
+ oct2=strtonum(sprintf("0x%s",oct2));
+ oct3=substr(hex,5,2);
+ oct3=strtonum(sprintf("0x%s",oct3));
+ oct4=substr(hex,7,2);
+ oct4=strtonum(sprintf("0x%s",oct4));
+ s=sprintf("%d.%d.%d.%d",oct1,oct2,oct3,oct4);
+ return s
+}
+
+$1 {
+ print cip(trim($1))
+}
View
27 awk/parse-csv.awk
@@ -0,0 +1,27 @@
+#!/usr/bin/awk -f
+{
+ n = parse_csv($0, data)
+ for (i = 0; ++i <= n;) {
+ gsub(/,/, "\\,", data[i]) #poso antibarra daban les comes
+ gsub(/;/,"", data[i]) #elimino punts i comes
+ gsub(/\t/," ", data[i]) #elimino tabs
+# printf "%s;%s", data[i], (i < n ? OFS : RS)
+ printf "%s", data[i]
+ if (i<n) printf ";"
+ if (i==n) printf "\n"
+ }
+}
+
+function parse_csv(str, array, field, i) {
+ split( "", array )
+ str = str ","
+ while ( match(str, /[ \t]*("[^"]*(""[^"]*)*"|[^,]*)[ \t]*,/) ) {
+ field = substr(str, 1, RLENGTH)
+ gsub(/^[ \t]*"?|"?[ \t]*,$/, "", field)
+ gsub(/""/, "\"", field)
+ array[++i] = field
+ str = substr(str, RLENGTH + 1)
+ }
+ return i
+}
+
View
37 bwmeter/bwmeter.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+IFACE="eth0"
+SLEEP_T=20
+
+# Sample 1
+t1=`date +%s`
+ifconfig $IFACE > /tmp/sample1
+t2=`date +%s`
+
+sleep $SLEEP_T
+
+# Sample 2
+t3=`date +%s`
+ifconfig $IFACE > /tmp/sample2
+t4=`date +%s`
+
+td=$(( ( ($t4 - $t2) + ($t3 - $t1) ) / 2 ))
+
+# RX
+pre=`cat /tmp/sample1 |grep "RX bytes" |cut -f 2 -d ":" |cut -f 1 -d " "`
+post=`cat /tmp/sample2 |grep "RX bytes" |cut -f 2 -d ":" |cut -f 1 -d " "`
+calc="(${post} - ${pre})/${td}*8/1024"
+res=$((${calc}))
+res2=$(($res /1024))
+echo "DOWN: ${res} kbit/s - ${res2} Mbps"
+
+# TX
+pre=`cat /tmp/sample1 |grep "TX bytes" |cut -f 3 -d ":" |cut -f 1 -d " "`
+post=`cat /tmp/sample2 |grep "TX bytes" |cut -f 3 -d ":" |cut -f 1 -d " "`
+calc="(${post} - ${pre})/${td}*8/1024"
+res=$((${calc}))
+res2=$(($res /1024))
+echo "UP: ${res} kbit/s - ${res2} Mbps"
+
+rm /tmp/sample1 /tmp/sample2
+
View
35 sql/create-db.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+##### CONFIGURE HERE
+USERNAME="username"
+PASSWORD="password"
+BBDD="database"
+DB_PASS="sql-password"
+####
+
+echo "I will create user '${USERNAME}' and database '${BBDD}'"
+echo -n "Press ENTER to continue or CTRL+C to quit."
+read pause
+
+### create BBDD
+echo "Create BBDD: ${BBDD}"
+mysqladmin -u root -p${DB_PASS} create ${BBDD}
+
+### create user
+echo "Create USER: ${USERNAME}"
+QUERY="CREATE USER '${USERNAME}'@'%' IDENTIFIED BY '${PASSWORD}';"
+mysql -u root -p${DB_PASS} -e "$QUERY"
+
+### create privs
+echo "Create PRIVILEGES"
+QUERY="GRANT ALL PRIVILEGES ON ${BBDD}.* TO '${USERNAME}'@'%' IDENTIFIED BY '${PASSWORD}' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;";
+mysql -u root -p${DB_PASS} -e "$QUERY"
+QUERY="GRANT ALL PRIVILEGES ON ${BBDD}.* TO '${USERNAME}'@'localhost' IDENTIFIED BY '${PASSWORD}' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;";
+mysql -u root -p${DB_PASS} -e "$QUERY"
+
+echo "Flush PRIVILEGES"
+QUERY="FLUSH PRIVILEGES;"
+mysql -u root -p${DB_PASS} -e "$QUERY"
+
+echo "DONE!"
+
View
3 sql/csv2sql.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+cat $1 |sed -e "s/'/\\\'/g" |sed -e "s/^/'/" |sed -e "s/;/','/g" |sed -e "s/$/'/" |sed 's/;;/;;/;s/\(^.*$\)/REPLACE INTO TABLE VALUES\(\1\);/' |mysql -u root -p"PASSWORD" DATABASE
View
3 sql/sql2csv.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+QUERY="$1"
+mysql -u root -p"PASSSWORD" -e "$QUERY" TABLE |sed -e "s/\t/;/g"
View
16 trim/trim.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+
+# Declare the subroutine
+sub trimwhitespace($);
+
+# Here is how to output the trimmed text
+print trimwhitespace($ARGV[0]);
+
+# Remove whitespace from the start and end of the string
+sub trimwhitespace($)
+{
+ my $string = shift;
+ $string =~ s/^\s+//;
+ $string =~ s/\s+$//;
+ return $string;
+}

0 comments on commit 65c58e6

Please sign in to comment.