Useful scripts

graudeejs

Son of Beastie

Reaction score: 692
Messages: 4,615

Ye, but simplicity of original task simply offend my level of knowledge
 

graudeejs

Son of Beastie

Reaction score: 692
Messages: 4,615

ah, one thing about ashbook script. For now you can't add address with spaces :(
I'll see if I can fix this later

you can search partial strings with for example
Code:
$ ashbook find name=killa.*
then again, this won't work
Code:
$ ashbook find name=.*smurf86
EDIT:
http://aldis.git.bsdroot.lv/ashbook/
 

gcooper@

Member
Developer

Reaction score: 14
Messages: 71

LeFroid said:
Heres a few simple scripts I wrote.

Heres a basic script to play any video in your ~/Videos dir

play.py
Code:
 #!/usr/bin/env python

 import sys
 import os

 # what to play the video with
 player = "vlc"
 # arguments, ex 'play "myvid.avi"'
 video = sys.argv[1]
 # home dir
 home = os.path.expanduser('~')

 # combine strings
 execStr = player+" "+home+"/Videos/"+video
 # run execStr
 os.system(execStr)
  1. Use exec family functions so the python script isn't looming in the background after your player has been executed and thus you don't get funky interactions with other applications in terms of signal handling, you don't have unnecessary overhead, etc.
  2. os.path.join is preferred over os.path.sep concatenation.
  3. You aren't properly error checking sys.argv to make sure that at least one argument has been specified.
 

gcooper@

Member
Developer

Reaction score: 14
Messages: 71

chroot_me:
Code:
#!/bin/sh

SRCBASE=${SRCBASE:-/usr/src}
[ -d "${DESTDIR}" -a -d "${SRCBASE}" ] || exit $?

export UNAME_s=$(uname -s)
export UNAME_m=${UNAME_m:-$(uname -m)}
export UNAME_p=${UNAME_p:-$(uname -p)}
REVISION=$(cd "${SRCBASE}/sys/conf" && grep REVISION= newvers.sh | cut -f2 -d'"')
BRANCH=$(cd "${SRCBASE}/sys/conf" && grep BRANCH= newvers.sh | head -n1 |  cut -f2 -d'"')
export UNAME_r="$REVISION-$BRANCH"
export OSVERSION=$(awk '/\#define.*__FreeBSD_version/ { print $3 }' "$SRCBASE/sys/sys/param.h")
export UNAME_v="$UNAME_s $UNAME_r #0: $(date -v1m -v+1y)	root@$HOSTNAME:$SRCBASE/sys/$UNAME_p/compile/GENERIC"

cp -L /etc/resolv.conf "$DESTDIR/etc/resolv.conf"
[ -c "$DESTDIR/dev/zero" ] || mount -t devfs dev "$DESTDIR/dev"

d=$DESTDIR
s=$SRCDIR

unset DESTDIR SRCBASE 

chroot "$d" /bin/sh

# XXX: exiting immediately and trying to unmount $DESTDIR/dev doesn't always work... hmmm...
sleep 10

umount -f "$DESTDIR/dev"
make_usb:
Code:
#!/bin/sh
#
# A simple script for creating memory disks.
#
# Some of the logic was adapted from release/scripts/make-memstick.sh -- e.g.
# make_usb (thanks kensmith@!)
#

TEN_MB_IN_1K=$((10 * 1024))

# Reporting functions. Nothing much to see here...
die() {
	echo >&2	"${0##*/}: ERROR: $*"
	exit 1
}
info() {
	echo	    "${0##*/}: INFO: $*"
}

# Usage
usage() {
	echo "usage: ${0##*/} [-f mtree-file] input-directory image"
	exit 1
}

# Clean up all consumed memory disks.
cleanup() {
	set +e
	trap : 0 1 2 15
	if [ "x${md_unit}" != x ]; then
		info "cleaning up memory disk"
		mdconfig -d -u ${md_unit}
	fi
	rm -f "${tempfile}"
}

# Create a complete USB image.
#
# 1 - Input directory.
# 2 - Output image (for USB stick).
make_usb() {

	# In order to use mdconfig(1) you should be root -- I realize this is
	# overly conservative, but I'd rather not make this too complicated...
	if [ "x$(id -ru)" != x0 ] ; then
		die "you must be root when executing make_usb"
	fi

	[ $# -eq 2 ] || usage

	[ -d "$1" ] || die "input directory - $1 - doesn't exist"

	set -e

	tempfile=$(mktemp /tmp/usb.XXXXXXXX)

	trap "[ \$? -ne 0 ] && rm -f '$output_image'; cleanup" 0 1 2 15

	in_dir=$1
	output_image=$2

	#sed -e '/^vfs.root.mountfrom.*/d' -i "" "$in_dir/boot/loader.conf"
	#echo 'vfs.root.mountfrom="ufs:/dev/da0a"' >> "$in_dir/boot/loader.conf"

	#makefs -o -O,1,-b,4096,-f,512,-o,space${inode:+,-i,${inode}} -f 1% \

	# Don't want to append to the existing image; this is what happens by
	# default with mdconfig(1), because we used -t vnode.
	rm -f "$output_image"

	makefs_args="${tempfile} ${in_dir}"
	if [ "x$mtreefile" != x ]; then
		makefs_args="-x -F $mtreefile $makefs_args"
	fi
	makefs -f 1% $makefs_args

	#
	# Calculate the needed file size for the mounted image.
	#
	# The calculation is done as shown for the following reason (as verbatim
	# from: .../release/scripts/make_memstick.sh):
	#
	#    Use $BLOCKSIZE for transfers to improve efficiency.  When
	#    calculating how many blocks to transfer "+ 2" is to account for
	#    truncation in the division and to provide space for the label.
	#
	block_count=$(( $(stat -f '%z' "$tempfile") / $TEN_MB_IN_1K + 2))

	# Create the backing file.
	dd if=/dev/zero of=$output_image bs=$TEN_MB_IN_1K count=$block_count

	# Create a new memory disk.
	md_unit=$(mdconfig -a -t vnode -f "$output_image")
	# Partition and label the sucker. Add appropriate bootloader bits to
	# the partition table and slices.
	fdisk -BIq /dev/${md_unit}
	bsdlabel -Bw /dev/${md_unit}
	# Sync the contents on the temporary file to the memory disk.
	dd "if=$tempfile" of=/dev/${md_unit}a bs=$TEN_MB_IN_1K conv=sync

}

# Parse available options and commands.

mtreefile=

while getopts "f:" option; do
	case "$option" in
	f)
		if [ ! -f "$OPTARG" ] ; then
			die "mtree file specified - $OPTARG - doesn't exist or isn't a regular file!"
		fi
		mtreefile=$OPTARG
		;;
	*)
		usage
		;;
	esac
done

shift $(( OPTIND - 1 ))

make_usb $@
/etc/rc for an automated install with a predefined payload (was using it for make_usb generated images before my project `changed direction'):
Code:
#!/bin/sh
#
# A first-time boot rc script which goes in, labels the disks appropriate to
# the config files, newfs's the sucker, then mounts, unwraps the payload, and
# does a basic sanity check to ensure that the system is bootable and
# functional.
#

DESTDIR=${DESTDIR:-/mnt}
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PKG_PATH=/packages

debug() {
	echo "${0##*/}: DEBUG: $@"
}

info() {
	echo "${0##*/}: INFO: $@"
}

# functions adlibbed from install/stage/etc/rc.

clear_swap() {
	# This always errors out when wiping the entire device; I don't care if
	# it does or not...
	info "Creating swap partition"
	dd if=/dev/zero of=/dev/$1s1b bs=1m 2>/dev/null || :
	swapon /dev/$1s1b
}

cleanup_mountpoints() {
	trap : 0 1 2 15
	set +e
	mounted_destdir=$(mount | awk '$3 != "/" && $3 !~ /^\/dev/ { print $1 }')
	until [ "x${mounted_destdir:-}" = x ]; do
		umount -f ${mounted_destdir}
		mounted_destdir=$(mount | awk '$3 != "/" && $3 !~ /^\/dev/ { print $1 }')
		sleep 1
	done
	[ "x${md_unit}" != x ] && mdconfig -d -u ${md_unit}

}

# Do all `post-install' configuration actions, like generate /etc/fstab, etc.
#
# 1 - Disk
configure_system() {
	generate_fstab $1
	clear_swap $1
}

# Clear all of the data off of a disk.
#
# 1 - Disk
format_disk() {
	fdisk -BIq $1 2>/dev/null >/dev/null
}

# Generate /etc/fstab based on the filesystems mounted under ${DESTDIR}.
#
# 1 - Disk
generate_fstab() {

	old_IFS=$IFS

	IFS="
"

	echo "/dev/$1s1b none swap sw 0 0" > "${DESTDIR}/etc/fstab"

	for i in $(mount); do

		i=$(echo "$i" | sed -e 's# on##g' -e 's#(.*)$##g' -e 's#mnt/*##g')

		case "$i" in
		/dev/$1*)
			echo "$i ufs rw 1 1" >> "${DESTDIR}/etc/fstab"
			;;
		esac

	done

	IFS=$old_IFS

}

# Is the disk real?
#
# 1 - Disk
isa_real_disk() {
	[ -c /dev/$1 ]
}

# Label a disk according to a seemingly logical scheme
label_disk() {

	disklabel -w -r -B /dev/${1}s1 auto

	tmp_label=$(mktemp /tmp/disklabel.XXXXXX)

	cat <<EOF > $tmp_label
a: 4G 16 4.2BSD 2048 16384
b: $(( $(sysctl -n hw.physmem) / 1024 / 1024 ))M *  swap
d: 4G *  4.2BSD 2048 16384
e: 8G *  4.2BSD 2048 16384
f: *  *  4.2BSD 8192 65536
EOF

	disklabel -R /dev/${1}s1 $tmp_label

	rm -f $tmp_label

}

mount_disk() {

	for i in 1a: 1d:usr 1e:var 1f:usr/home; do

		partition="/dev/${1}s$(echo "$i" | cut -d: -f1)"
		mountpoint="${DESTDIR}/$(echo "$i" | cut -d: -f2)"

		debug "Mounting ${partition} on $mountpoint"

		test -d "$mountpoint" || mkdir -p "$mountpoint"
		mount $partition "$mountpoint"

	done

}

newfs_disk() {
	for i in /dev/${1}s1[adef]; do
		newfs -O2 -m 8 -o time $i >/dev/null
	done
}

stage_install() {

	info "Installing base"

	# Copy over the contents of the media to $DESTDIR , skipping over a
	# select set of directories (but not /packages because pkg_add would
	# fold in on itself if it's not present based on the value of
	# getcwd(3), as pkg_add(1) -C chroots into $DESTDIR, and chroot(2)
	# does not actually modify the value of the current directory returned
	# by getcwd(3)).
	#
	# XXX (garrcoop): 1. --exclude for tar(1) should work in this regard,
	# but it doesn't 100% in this case (for some reason). 2. -C for cpf
	# should work as well, but doesn't.
	(cd / ; tar -cpf - $(ls -d * | grep -E -v 'dist|mnt') | tar xpf - -C "$DESTDIR")

	# Override the USB copies of files with their dist/ bretheren.
	(cd /dist ; tar -cpf - . | tar xpf - -C "$DESTDIR")

	sed -e '/^vfs.root.mountfrom/d' -i "" "$DESTDIR/boot/loader.conf"

	if [ -d "$PKG_PATH" ] ; then

		info "Installing packages"

		mount -t devfs dev $DESTDIR/dev

		mountpoints="$DESTDIR/dev $mountpoints"

		# XXX (garrcoop): pkg_add(1) barfs with exit code 1 even when
		# -F and -f are specified; this needs to be fixed.
		(find "$PKG_PATH" -type f | xargs -n 4 pkg_add -C "$DESTDIR" -Ff || :) 2>/dev/null >/dev/null

		rm -Rf "$DESTDIR/$PKG_PATH"

		info "Packages installed"

	fi

	info "Install complete"

}

wipe_disk() {
	info "Wiping disk $1"
	dd if=/dev/zero of=/dev/$1 bs=1m count=1 2>/dev/null || :
}

set -e

md_unit=$(mdconfig -a -t malloc -s $(( 16 * 1024 * 1024 )) )
newfs /dev/$md_unit >/dev/null
mount -t ufs /dev/$md_unit /tmp
trap cleanup_mountpoints 0 1 2 15

# Wait for the devices to be detected.
i=0
while [ $i -lt 15 ] && ! isa_real_disk "$1" ; do
	# Avoid RAIDs, memory disks, and SCSI disks.
	set -- $(sysctl -n kern.disks | awk '{ for (i=1; i <= NF; i++) { if ($i !~ /ar|da|md|mf[0-9]+/) { disks = disks " " $i } } } END { print disks }')
	sleep 2
	: $(( i += 1 ))
done

if [ "x$1" = x ] ; then
	echo "${0##*/}: ERROR: didn't find a configurable harddisk."
	exit 1
else

	for step in wipe format label newfs mount; do
		debug "Calling ${step}_disk on $1"
		eval "${step}_disk $1"
	done

	stage_install
	configure_system $1

	cleanup_mountpoints

	read -p "Remove the CD / USB media and press Enter to continue " j
	init 6

fi
Certain hacks are in place for our specific environment, but you're free to use this in whatever way you deem appropriate.

All stuff here BSD licensed of course :p.
 

Ruler2112

Well-Known Member

Reaction score: 25
Messages: 422

ckester said:
One of the first lessons I learned when I began my software career many years ago is to never add features that aren't in the spec -- at least, not unless you get the client's agreement and understanding that the project might be delayed or have additional bugs as a result.

Even if it's an in-house job.
My brother was fired from his job last year because of just this. He wrote what the person wanted, then expanded it to include cool features. By the time he was done adding stuff that he thought would be useful, it was so complex that the person it was for couldn't figure out how to use it. They went around 2-3 times, trying to get it to be what the person wanted, but he really didn't know what he wanted it to do and my brother was guessing based on varied and vague descriptions. (This didn't come out until much later.) It was an in-house job and because it didn't do what was wanted, the person had him fired. :( My brother has always ripped on me for spending time doing system requirements engineering and getting the document explaining what the system will do initialed on all but the simplest of projects; if he were to have done so, he would not have been fired IMO.

Sorry to go off-topic - this is an important concept for new coders to grasp. More and better isn't always better.
 

gcooper@

Member
Developer

Reaction score: 14
Messages: 71

Yes. Making life easier is nice, but simplicity is always key; otherwise the added support and complexity is just unruly and a pain.
 

gcooper@

Member
Developer

Reaction score: 14
Messages: 71

Delete empty @pkgdep lines created by unknown source (portmaster?, ports?):

Code:
find /var/db/pkg/ -name +CONTENTS -mindepth 2 -maxdepth 3 | xargs -J % -n 1 sed -i "" -E -e '/^@pkgdep *$/d' %
 

graudeejs

Son of Beastie

Reaction score: 692
Messages: 4,615

I just developed playlist randomization for my playd (in pure sh)

this function takes 1 argument (maximal value to return) and return random number 0 to $1
Code:
playd_rnd() {
# arg1 Max value
  a=$(dd if=/dev/random of=/dev/stdout bs=1 count=4 2>/dev/null | od -D -An)
  b=$(echo "$1 / 4294967296 * $a" | bc -l | sed 's/\..*$//')
  if [ "$b" ]; then
    echo $b
  else
    echo 0
  fi
}
 

vermaden

Son of Beastie

Reaction score: 1,125
Messages: 2,729

@killasmurf86

You should accomplish the same with that one-liner:
[CMD=""]% expr ${RANDOM} % 10 + 1[/CMD]

... where 10 is maximum, You may generalize that into:
[CMD=""]% expr ${RANDOM} % ${MAX} + 1[/CMD]
 

ProFTP

Active Member

Reaction score: 8
Messages: 137

simple script to correct MySQL database tables

http://www.x0.org.ua/blog/user/1/view/39
Code:
#!/usr/bin/perl

use DBI;

my $db_name = 'mysql';

# user from db
my $db_user = 'root';
my $db_pass = '';
my $db_type = 'mysql';
my $db_host = 'localhost';

my $dbh = DBI->connect(
    "DBI:$db_type:database=$db_name;host=$db_host",
    $db_user, $db_pass,
    {
        RaiseError => 1,
        PrintError => 1
    }
) || die $DBI::errstr;

my $sth = $dbh->prepare( 'select Db from db' );
$sth->execute();
my $loop_data;
push @{$loop_data}, $_ while $_ = $sth->fetchrow_hashref();

$sth->finish();
$dbh->disconnect();

foreach my $ii ( @{$loop_data} ) {

    my $db_name = $ii->{Db};

    my $dbh = DBI->connect(
        "DBI:$db_type:database=$db_name;host=$db_host",
        $db_user,
        $db_pass

    );

    my $loop_data2;

    eval {

        my $sth = $dbh->prepare('show tables');
        $sth->execute();

        while ( my @row = $sth->fetchrow_array ) {

            die "bad table name: $row[0]" unless $row[0] =~ /^[w_]+$/;
            push @{$loop_data2}, $row[0];

        }

    };

    next if ($@);

    foreach $_ ( @{$loop_data2} ) {
        $dbh->do( qq{REPAIR TABLE $_ } );
    }

    $dbh->disconnect();

}
 

Alt

Aspiring Daemon

Reaction score: 82
Messages: 726

Another script for fixing all MYSQL tables in all databases (without shutdown):
Code:
#!/bin/sh
# Written by Alt 2010. Beer-ware license xD

M_LOGIN="root"
M_PASS="YoUrPaSsWoRd"

DBS=`echo 'show databases;' | /usr/local/bin/mysql -p$M_PASS -u$M_LOGIN | /usr/bin/grep -v Database | /usr/bin/grep -v information_schema | /usr/bin/xargs echo `
for DB in $DBS; do
    echo "DB $DB"
    TBLS=`echo 'show tables;' | /usr/local/bin/mysql -p$M_PASS -u$M_LOGIN $DB | /usr/bin/grep -v Tables | /usr/bin/xargs echo `
    for TBL in $TBLS; do
        echo -n "  Tbl $TBL .. "
        echo "repair table $TBL quick;" | /usr/local/bin/mysql -p$M_PASS -u$M_LOGIN $DB | /usr/bin/grep -v Msg_type
    done
done
/usr/local/bin/mysqladmin -p$M_PASS -u$M_LOGIN flush-tables
 

ProFTP

Active Member

Reaction score: 8
Messages: 137

i think from the beginning we should see whether the table is corrupted ...! (because for a long time ...)
... and then correct ...
but it is not essential ...
 

graudeejs

Son of Beastie

Reaction score: 692
Messages: 4,615

vermaden said:
@killasmurf86

You should accomplish the same with that one-liner:
[CMD=""]% expr ${RANDOM} % 10 + 1[/CMD]

... where 10 is maximum, You may generalize that into:
[CMD=""]% expr ${RANDOM} % ${MAX} + 1[/CMD]
uhhh, man.... and I had to invent new method just because I couldn't find anything how to get random number

EDIT:
I noticed, that your solution only generates random numbers up till 2^15 [at lest so it seams].
My solution [current implementation] can generate number up 2^32, and I don't see problems making even bigger numbers (just need to modify a bit)
 
J

john_doe

Guest


vermaden said:
You should accomplish the same with that one-liner:
[CMD=""]% expr ${RANDOM} % 10 + 1[/CMD]
It will not work in sh(1). Use jot(1), e.g.
$ jot -r 1 0 $(((1 << (8 * [highlight]4[/highlight])) - 1))
where 4294967295 is max of 4 bytes of /dev/random

And the function can be reduced to
$ playd_rnd() { jot -r 1 0 $1; }
 

graudeejs

Son of Beastie

Reaction score: 692
Messages: 4,615

john_doe said:
It will not work in sh(1). Use jot(1), e.g.
$ jot -r 1 0 $(((1 << (8 * [highlight]4[/highlight])) - 1))
where 4294967295 is max of 4 bytes of /dev/random

And the function can be reduced to
$ playd_rnd() { jot -r 1 0 $1; }
I can't believe that this was in front of my nose all the time :D
 

t4z3v4r3d

Member


Messages: 65

Hello . I'm trying to write a graph er ! script ... and its not good to present .:d
How can i do that ?
Code:
#!/usr/bin/env bash
clear
# this can be temperature or something else 
# Total line number is 33 !

temp_array=( "" 32 31 31 30 29 28 28 35 37 38 38 39 40 40 41 48 45 44 43 42 40 39 36 )
# THE h line len=32 ! so i should get diffz between start and stop line ! (negatively)
size="${#temp_array[@]}"
	### "Y"
for ((l=1 ; l < $size ; l++ ));do
let "l2=l+$l"
	for ((t=1 ; t <= ${temp_array[$l]} ;t++));do
		let "temp=50-${temp_array[$l]}"
		echo -en "\033[${temp};"$l2"f\033[1;31m *\r \033[0;0m"
  		echo "${temp_array[$l]}"
	done
done
echo	-e "\033[0;0m"
	### "X"
for ((k=1 ; k < 32 ; k++ ));do
 	echo -en "\033[32;$kf\033[32;"$k"d\033[1;31m |\033[0m"
done
echo
 

Dereckson

Member

Reaction score: 16
Messages: 64

Grab your photos from memory card to hard disk

I wrote this TCL script to copy photos from my memory card to my hard disk.

It copies only new pictures and, if the $GrabPhotos(enableresize) preference is set to 1, will call ImageMagick to create resized version (I found it's quicker to browse 1600x1200 and convenient to have 800x600 versions to send to your contacts).

Code:
#!/usr/local/bin/tclsh8.5
#
# GrabPhotos.tcl -- Photo memory card helper
# Sébastien Santoro aka Dereckson <dereckson@gmail.com>
#

# # # # # # # # # # # # #
# General configuration #
# # # # # # # # # # # # #

# Where to copy the pics?
set GrabPhotos(targetdir) "~/docs/pics/albums"
# Where are the pics (e.g. your memory card location)?
set GrabPhotos(sourcedir) "/mount/flashmedia"

# On the memory card, where are the pics ?
# set GrabPhotos(picsdir) "" to disable DCIM-like directories lookup
set GrabPhotos(picsdir) DCIM

# If 1, work with any subdirectory from the source
set GrabPhotos(recursive) 1

# Prints an header with credits
set GrabPhotos(printcredits) 0

# verbose prints script action, debug extra details
set GrabPhotos(verbose) 1
set GrabPhotos(debug) 0

# When debug is 1, that also implies verbose to 1, discarding your verbose value
# To disable this behavior, comment the following line (putting a # in front of it) :
if $GrabPhotos(debug) {set GrabPhotos(verbose) 1}

# # # # # # # # # # # # # # # # # # # # #
# Specific configuration :: resize code #
# # # # # # # # # # # # # # # # # # # # #

#This part is if you want to create resised versions with ImageMagick

set GrabPhotos(enableresize) 1
set GrabPhotos(resizeddir) "~/docs/pics/albums/_resized"
set GrabPhotos(sizes) "75x56 800x600 1600x1200"
set GrabPhotos(jpegquality) 80
set GrabPhotos(convert) "/usr/local/bin/convert"
set GrabPhotos(mogrify) "/usr/local/bin/mogrify"

# # # # # # # # # # # # #
# General configuration #
# # # # # # # # # # # # #

# Now, if you just want a copy or a resizing, it's operate as is. Just run it.
# The following code is fully customisable and commented to help you to
# adapt it for your needs.

# # # # # # # # # # # #
# File types handlers #
# # # # # # # # # # # #

# By default, this script handles .jpg files.
# You can add handlers, just creating a proc named handler_ext,
# where ext is the file lowercase extension.
# The file argument is the complete path to the file

# This handlers copy .jpg files to $GrabPhotos(targetdir)/YYYY/YYYY-MM folders
# and then create thumbnails version 

proc handler_jpg {file} {
    global GrabPhotos

    #Gets created time of the file, and then YYYY and MM values
    set ctime [get_created_time $file]
    set YYYY [clock format $ctime -format "%Y"]
    set MM [clock format $ctime -format "%m"]

    #Gets the target directory. Creates it if needed
    set targetdir "$GrabPhotos(targetdir)/$YYYY/$YYYY-$MM"
    if $GrabPhotos(debug) {puts "\nTarget: $targetdir"}
    create_directory $targetdir

    #Copies file if it doesn't exist
    copy_file $file $targetdir

    #Calls resize code if enabled
    if $GrabPhotos(enableresize) {resize $file "$YYYY/$YYYY-$MM"}
}

# # # # # # # # #
# Plugs in code #
# # # # # # # # #

proc resize {file subdir} {
    global GrabPhotos
    if $GrabPhotos(verbose) {puts "Generating resized version from $file"}
    foreach size $GrabPhotos(sizes) {
        set targetdir "$GrabPhotos(resizeddir)/$subdir/$size"
        create_directory $targetdir
        set targetfile "$targetdir/[get_filename $file]"
        if ![file exists $targetfile] {
            #copy_file $file $targetdir
            exec -- $GrabPhotos(convert) [file nativename $file] -resize $size -quality $GrabPhotos(jpegquality) [file nativename $targetfile]
        }
    }
}

# # # # # # # # # # #
# Helper procedures #
# # # # # # # # # # #

proc copy_file {file targetdir} {
    global GrabPhotos
    set fileExists [file exists "$targetdir/[get_filename $file]"]

    if $GrabPhotos(debug) {
        #DEBUG print
        puts -nonewline "Copying $file to $targetdir"
        if $fileExists {
            puts " \[skipped, already exists\]"
        } {
            file copy $file $targetdir
            puts " \[ok\]"
        }
    } elseif !$fileExists {
        file copy $file $targetdir
    }
}

proc create_directory {dir} {
    global GrabPhotos
    if ![file exists $dir] {
        if $GrabPhotos(verbose) {puts "Creating $dir"}
        file mkdir $dir
    }
}


proc get_created_time {file} {
    file stat $file test
    return $test(ctime)
}

proc get_filename {file} {
    lindex [file split $file] end
}

proc parse {directory} {
    global GrabPhotos

    if $GrabPhotos(verbose) {puts "Parsing $directory"}

    #Get files in this directory
    foreach file [glob -nocomplain -directory $directory *] {

        #Another directory, parses it if recursive mode enabled
        if {[file isdirectory $file] && $GrabPhotos(recursive)} {
            parse $file
        } {
            #A regular file, gets the extension and check if we've an handler
            set ext [string tolower [string range [file extension $file] 1 end]]
            set handlerproc [info procs handler_$ext]
            if {$handlerproc != ""} {
                #An handler exists        
                if $GrabPhotos(debug) {puts "Calling $handlerproc $file"}
                $handlerproc $file
            } {
                #File unhandled by a proc
                if $GrabPhotos(verbose) {puts "Ignored file: $file"}
            }
        }
    }

}

proc print_credits {} {
    puts "___________________________________________________________________"
    puts "GrabPhotos 0.1 :: the photo memory card helper"
    puts "___________________________________________________________________"

}

proc print_usage {} {
    puts "Usage: GrabPhotos <source>"
}

# # # # # # # # # #
# Procedural code #
# # # # # # # # # #
if {$argc == 0} {
    if {![info exists GrabPhotos(sourcedir)]} {
        print_credits
        print_usage
        exit
    }
} {
    # Gets sourcedir from argument
    set GrabPhotos(sourcedir) [lindex $argv 0]
}

if {![file isdirectory $GrabPhotos(sourcedir)]} {
    puts stderr "Fatal error: $GrabPhotos(sourcedir) isn't a directory."
    exit
}


if $GrabPhotos(printcredits) {
    print_credits
}

# Go to $GrabPhotos(picsdir) (by default DCIM) directory, if it exists
# If not, we stay in current $GrabPhotos(sourcedir) (by default the script argument)
if [file isdirectory "$GrabPhotos(sourcedir)/$GrabPhotos(picsdir)"] {
    set GrabPhotos(sourcedir) "$GrabPhotos(sourcedir)/$GrabPhotos(picsdir)"
}

parse $GrabPhotos(sourcedir)
 

Andres

New Member

Reaction score: 1
Messages: 6

The version in shells/tcshrc is missing a few things, like tmux/screen terminfo:
Code:
set echo_style = 'both'
set title = '$USER@$HOST:ar $cwd'

switch ($TERM)
case {dtterm,rxvt,screen,xterm}*:
	alias cwdcmd 'echo -n "\e]2;'$title'\a"'
	breaksw
case sun*:
	alias cwdcmd 'echo -n "\e]l'$title'\e\\"'
	breaksw
case vt[24]20*:
	alias cwdcmd 'echo -n "\e]0;'$title'\a\e\\"'
	breaksw
default:
	alias cwdcmd 'unalias cwdcmd'
	unset title
endsw

cwdcmd
 

ProFTP

Active Member

Reaction score: 8
Messages: 137

[Perl] example upload photos to a server with thumbnail previews

files are not uploaded to the server, php programmers could not solve this problem simply wanted to change hosting :)

Code:
#!/usr/bin/perl

#  @author     Dmitriy Shilenko <q7u5@ukr.net>
# ProFTP 

use warnings;
use strict;
use CGI;
use DBI;
use Image::Magick;

use CGI::Carp qw(fatalsToBrowser);    # used only for tests
# use Data::Dumper;
# $CGI::POST_MAX = 100000000 # maximum file upload size for safety

my $form = new CGI;


# print $form->header; #Print HTML header. this is mandatory



my $p;
%{$p} = $form->Vars;

if (!$p || !%{$p}) {

    print $form->header( -charset => "utf8" );
    #print `pwd`;
    print 'This is perl, my name is Satan666 :)';
    exit;

}



my @a = qw (cid date obj name fam phone age addr project edu more icq status b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13 b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 b24);

  @a = map { $p->{$_} || '' } @a;

# print Dumper \@a;
# exit;

my $db_name = '';
# user from db
my $db_user = '';
my $db_pass = '';
#
my $db_type = 'mysql';
my $db_host = 'localhost';

my $prefix = ''; #  prefix table...

my $web_home = "/home/files/";


my $dbh = DBI->connect(
    "DBI:$db_type:database=$db_name;host=$db_host",
    $db_user, $db_pass,
    {
        RaiseError => 1,
        PrintError => 1
    }
) || die $DBI::errstr;


	$dbh->do('INSERT INTO '.$prefix.'_base (cid,date,obj,name,fam,phone,age,addr,project,edu,more,icq,status,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24) 
	
	VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
	', undef, @a );

my $id_db = $dbh->{mysql_insertid};

 my $hash;

my @hash;

# 1..8 foto's
for (0..8) {

 my $key = "foto".($_ == 0 ? "" : $_);
 my $id_file = $_ == 0 ? 1 : ($_+1);
  

 if ( my $UPLOAD_FH = $form->param($key) ) {
 
  uploadfile($UPLOAD_FH,$id_db, $id_file );  
  $hash->{$key} = '/file/'.$id_db.'_'.$id_file.'.jpg';
 
 
 }


}




# print Dumper $hash;


$dbh->do('UPDATE '.$prefix.'_base SET '.( join ", ", map { $_ .' = ? ' } keys %$hash ).' WHERE id='.$id_db.' LIMIT 1', undef, values %$hash ) if ($hash && %$hash);





##this is the only way to send msg back to the client

## print "<script>parent.callback('upload file success')</script>";





sub uploadfile {

 my ($UPLOAD_FH,$id_last, $id_name) = @_;

 my $newfilename = $web_home."or_".$id_last."_".$id_name.".jpg";

 # umask 0000; #This is needed to ensure permission in new file

 open (IMG, ">$newfilename"); binmode IMG;  print IMG while (<$UPLOAD_FH>); close (IMG); chmod 0644, $newfilename; 

 image_thumbnail('100', '100', $newfilename, $web_home."".$id_last."_".$id_name.".jpg");

};




sub image_thumbnail {

 my ($ix, $iy, $file_name, $file_name_out) = @_;

my $photo = Image::Magick->new;
  $photo->Read($file_name); 


my ($ox, $oy, $oc, $ic, $nx, $ny, $geo);
  ($ox,$oy)=$photo->Get('columns','height');
  if (($ox > $ix)||($oy > $iy)) {
   $oc = $ox/$oy; $ic = $ix/$iy;
   if ($oc < $ic) {$ny = $iy; $nx=int(($ox/$oy)*$iy);}
   elsif ($oc > $ic) {$nx = $ix; $ny=int(($oy/$ox)*$ix);}
   else {$nx = $ix; $ny = $iy;}}
  else {$nx=$ox;$ny=$oy;}



  $geo = 'geometry';
  $photo->Resize(geometry=>$geo, width=>$nx, height=>$ny);
  
  $photo->Write($file_name_out);  
    
}

$dbh->disconnect();

	# redirect:

  print $form->redirect( -uri => 'site.com', -status => 301 );


exit;
interesting:


Code:
$dbh->do('UPDATE '.$prefix.'_base SET '.( join ", ", map { $_ .' = ? ' } keys %$hash ).' WHERE id='.$id_db.' LIMIT 1', undef, values %$hash ) if ($hash && %$hash);
my blog
 

da1

Aspiring Daemon

Reaction score: 95
Messages: 876

I was searching for a ftp version of sshguard and I couldn't find one. Therefore, I wrote the following. Tested and working with pure-ftpd and pf on 8.0,8.1:
It is not hard to adapt it for ipf or ipfw with other ftp softwares out there.


Code:
grep="/usr/bin/grep"
awk="/usr/bin/awk"
uniq="/usr/bin/uniq"
ftpguard="/etc/ftpguard.pf"
table="ftpguard"
pfctl="/sbin/pfctl"


if [[ -z $1 ]];then
        echo "Usage: grep_ip_from_file <file_name>"
else
        $grep "\[ERROR\]\ Too\ many\ authentication\ failures" $1 | $awk '{print $6}' | $grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'| $uniq | while read IP
                do
                   for i in $IP;
                                do
                                   if [[ $i != `$grep $i $ftpguard` ]];
                                then
                                   echo $i >> $ftpguard
                                   $pfctl -t $table -T add $i
                                fi
                                done
                done
fi
The script expects that you have a entry in pf.conf like:
Code:
table <ftpguard> persist file "/etc/ftpguard.pf"
[...]
block in quick on $ext_if proto tcp from <ftpguard> to any port { 21, other ftp related ports } label "ftp bruteforce"
and
Code:
-rw-------  1 root  wheel  264 Sep  6 12:19 /etc/ftpguard.pf
I use crontab to scan /var/log/messages every 10 minutes:
Code:
*/10    *       *       *       *       root    /path to a folder/ftpguard.sh /var/log/messages &> /dev/null
Pls note that I have appended &> /dev/null because I do not want to receive mails about this.


Hope it comes handy for some one.
 

rbelk

Active Member

Reaction score: 54
Messages: 199

After getting asked to many times on how to update the ports system, I wrote the following script. I hope some one finds it useful. If anyone finds an issue, or makes an addition, please forward them back to me. I might submit this to to "portmaster" author and see if he would include it in his port.

I added this script to root's bin directory and chmod'ed it to 0700.

Code:
#!/bin/sh
#===============================================================================
#
#          FILE:  pmm
# 
#         USAGE:  pmm
# 
#   DESCRIPTION:  A menu driven Shell script for ports management
# 
#       OPTIONS:  None
#  REQUIREMENTS:  /bin/sh, /usr/sbin/pkg_version, /usr/local/bin/portmaster
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  $Author: rbelk $
#       COMPANY:  OnlyBSD
#       VERSION:  $Header: /root/bin/RCS/pmm,v 1.4 2010/11/14 03:35:36 rbelk Exp $
#       CREATED:  $Date: 2010/11/14 03:35:36 $
#      REVISION:  $Revision: 1.4 $
#           LOG:  $Log: pmm,v $
#           LOG:  Revision 1.4  2010/11/14 03:35:36  rbelk
#           LOG:  added the pkg_version requirement
#           LOG:
#           LOG:  Revision 1.3  2010/11/14 03:33:26  rbelk
#           LOG:  cleaned up the comments
#           LOG:
#           LOG:  Revision 1.2  2010/11/14 03:30:19  rbelk
#           LOG:  Update the RCS comments section
#           LOG:
#===============================================================================

while :
do
 clear
 echo "P O R T S - M A I N T I A N C E - M E N U"
 echo ""
 echo "NOTE: You must first do the following steps for PMM to run correctly"
 echo "- Install the ports tree with the command /usr/sbin/portsnap fetch extract"
 echo "- Install portmaster: cd /usr/ports/ports-mgmt/portmaster; make install clean"
 echo "- Add the next line to roots crontab"
 echo "  0 1 * * * /usr/sbin/portsnap cron"
 echo ""
 echo "1. Update the ports tree"
 echo "2. List ports that need to be upgraded"
 echo "3  Update the installed ports"
 echo "4. Cross-check and update dependency information for all ports"
 echo "5. Delete stale distfiles of ports not installed anymore"
 echo "6. Delete stale packages in the package directory"
 echo "7. Delete stale ports that used to be depended on"
 echo "8. Delete stale entries in /var/db/ports"
 echo ""
 echo "Q - Quit"
 echo ""
 echo -n "Please enter option [1 - 8, Q] "
 read opt
 case $opt in
  1) echo "- Updating the ports directory";
         old_time=`ls -lt /usr/ports/UPDATING | awk '{print $6" "$7" "$8}'`
         /usr/sbin/portsnap update
         new_time=`ls -lt /usr/ports/UPDATING | awk '{print $6" "$7" "$8}'`
         if [ "$old_time" != "$new_time" ]
         then
             echo ">>>>>> A T T E N T I O N ---------------------------------------------------"
             echo " /usr/ports/UPDATING has Changed"
             echo "    PLEASE READ BEFORE CONTINUING"
             echo " You might have an installed port that needs attention"
             echo " When you are sure everything is OK, start PMM again and proceded to step 2"
             echo ">>>>>> A T T E N T I O N ---------------------------------------------------"
             exit 1;
         fi
         echo ""
         echo ">>>> Your Next Option is '2' <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  2) echo "- Listing of ports that need to be upgraded";
         /usr/sbin/pkg_version -o -I -L= | awk '{print $1}'
         echo ""
         echo '>>>> Your Next Option is "3" <<<<';
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  3) echo "- Updating the installed ports";
         # /root/bin/update_ports
         /usr/local/sbin/portmaster -a
         echo ""
         echo ">>>> Your Next Option is "4" <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  4) echo "- Cross-checking and updating dependency information for all ports";
         /usr/local/sbin/portmaster --check-depends
         echo ""
         echo ">>>> Your Next Option is "5" <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  5) echo "- Deleting stale distfiles of ports not installed anymore";
         /usr/local/sbin/portmaster --clean-distfiles
         echo ""
         echo ">>>> Your Next Option is "6" <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  6) echo "- Deleting stale packages in the package directory";
         /usr/local/sbin/portmaster --clean-packages
         echo ""
         echo ">>>> Your Next Option is "7" <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  7) echo "- Deleting stale ports that used to be depended on";
         /usr/local/sbin/portmaster -s
         echo ""
         echo ">>>> Your Next Option is "8" <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  8) echo "- Deleting stale entries in /var/db/ports";
         /usr/local/sbin/portmaster --check-port-dbdir
         echo ""
         echo ">>>> Your ports tree should be updated <<<<";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
  Q) echo "Bye $USER";
         exit 1;;
  *) echo "$opt is an invaild option. Please select option between 1-8, or Q only";
         echo "Press [enter] key to continue. . .";
         read enterKey;;
esac
done
 

vermaden

Son of Beastie

Reaction score: 1,125
Messages: 2,729

@rbelk

Nice scripts mate, here are my thoughts:

diff(1) between old and new UPDATING instead of ATTENTION
add addtional options like
3.1 (do not ask for anything - automatic mode)
3.2 (use packages if available: -PP option)
3.3.(other usefull portmaster options)
consistency, use name 'ports' or 'packages' everywhere instead of mixing, example:
echo "6. Delete stale **packages** in the package directory"
echo "7. Delete stale **ports** that used to be depended on"
 
Top