ldd2chroot – A script to extract packages

At last I got some time to blog, and I did something with ‘chroot’. Here is a script which will extract ldd dependency library packages(.debs) to ‘chroot’ directory. Here is the steps it follows,

1. For the given command, get the shared libraries.
2. Then get the packages which will give the shared libraries.
3. and download those shared libraries and extract in the given ‘chroot’ directory.

I hope it will be useful to package maintainers.

#!/bin/bash
# ldd2chroot - A script to extract packages
# Author: mohan43u


USAGE="[usage]
	ldd2chroot [-i chrootdir] [-p] [-h] cmd_or_pkg [...]"
HELP="${USAGE}
[description]
	-i chrootdir	Install pkgs in 'chrootdir'
	-p		'chd_or_pkg' will be package name
	-h		Print this help
	cmd_or_pkg	Command name or package name(only for -p)"

executeldd()
{
ldd $(which "${1}")
}

getsharedlibs()
{
tr '\t' ' ' |
tr -s ' ' '\n' |
grep '^/'
}

getlibpkgs()
{
while read LIB; do dpkg -S "${LIB}"; done
}

getpkgs()
{
cut -d':' -f1 | sort | uniq
}

extractpkg()
{
for PKG
do
	echo "[starting apt-get for ${PKG}]"
	apt-get clean
	apt-get --reinstall --download-only --yes install "${PKG}"
	DEBFILE=$(find /var/cache/apt/archives -name '*.deb')
	cp -p ${DEBFILE} $(pwd)
	test ! -z "${CHROOTDIR}" && echo -e "[extracting ${DEBFILE} to ${CHROOTDIR}]" &&
	dpkg -X "${DEBFILE}" "${CHROOTDIR}"
done
}

while getopts 'i:ph' OPTIONS
do
	case "${OPTIONS}" in
		i) CHROOTDIR="${OPTARG}";;
		p) PKGARGS="1";;
		h) echo -e "${HELP}" && exit 0;;
		\?) echo -e "${USAGE}" && exit 1;;
	esac
done
shift $((OPTIND - 1))
test -z "${@}" && echo -e "${USAGE}" && exit 1

for ARG
do
	if test -z "${PKGARGS}"
	then
		LDDOUTPUT=$(executeldd "${ARG}")
		echo -e "[ldd]\n${LDDOUTPUT}"
		SHAREDLIBS=$(echo -e "${LDDOUTPUT}" | getsharedlibs)
		SHAREDLIBS="$(which ${ARG})\n${SHAREDLIBS}"
		echo -e "[sharedlibs]\n${SHAREDLIBS}"
		LIBPKGS=$(echo -e "${SHAREDLIBS}" | getlibpkgs)
		echo -e "[required shared library packages]\n${LIBPKGS}"
		PKGS=$(echo -e "${LIBPKGS}" | getpkgs)
		echo -e "[required pkgs for apt-get]\n${PKGS}"
	else
		PKGS="${ARG}"
	fi

	extractpkg ${PKGS}
done

Download: ldd2chroot

If someone improving this, let me know, so that I can also use the changes.

urlencoder

Hi friends,

Previously I had a post regarding url encoding and decoding, This is much improved version of that script. To know more about urlencoding, try this link, http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

#!/bin/bash
# urlencode - program to urlencode/urldecode
# Author: Mohan Raman
# License: Its in public domain.

USAGE="[USAGE]
	urlencode [-e|-d] [-h]
"

HELP="${USAGE}
[DESCRIPTION]
	-e	URLEncode standard input(default)
	-d	URLDecode standard input

[EXAMPLE]
	$ urlencode
	http://www.google.co.in
	http%3A%2F%2Fwww.google.co.in

	$ urlencode -d
	http%3A%2F%2Fwww.google.co.in
	http://www.google.co.in
"

encode()
{
sed -e's/./&\n/g' -e's/ /%20/g' | grep -v '^$' | while read CHAR; do test "${CHAR}" = "%20" && echo "${CHAR}" || echo "${CHAR}" | grep -E '[-[:alnum:]!*.'"'"'()]|\[|\]' || echo -n "${CHAR}" | od -t x1 | tr ' ' '\n' | grep '^[[:alnum:]]\{2\}$' | tr '[a-z]' '[A-Z]' | sed -e's/^/%/g'; done | sed -e's/%20/+/g' | tr -d '\n'; echo
}

decode()
{
sed -n -e's/%\([0-9A-F][0-9A-F]\)/\\x\1/g' -e's/+/ /g' -e's/.*/echo -e "&"/g' -ep | "${SHELL}"
}

DECODE="0"

while getopts "edh" OPTION
do
	case "${OPTION}" in
	d) DECODE="1";;
	h) echo "${HELP}"; exit 0;;
	\?) echo "${USAGE}"; exit 1;;
	esac
done

test "${DECODE}" = "0" && encode
test "${DECODE}" = "1" && decode

Download: urlencoder

freebsdman – script to display ‘man’ pages from freebsd.org

Hi friends,

I love FreeBSD’s Documentation(FreeBSD too), Its one of the top class documentations available for UNIX. I think they are capturing nearly every thing about UNIX.

Previously I’ll go to their man page site “http://www.freebsd.org/cgi/man.cgi”, give the command name and get the man page whatever format I want(pdf is damn good!!). But I thought It will be useful if I have an alternative to that *crap* man command in my ‘Ubuntu’ so that I can get all the ‘man’ pages available for Entire Unix family.

Somewhere I saw a small script which gets input from user and generating a URL with GET method parameters and connecting with the server using ‘curl’. Then I decided to do the same thing with my freebsd server, here is the result, a script which will get command name and display the man page, it can be used to get the pdf document also.

USAGE:
        freebsdman [-a 0|1] [-s section] [-d distro]
                [-f format] [-o] [-h]

DESCRIPTION:
        -a 0|1          do 'apropos' search(1-enable, 0-disable)
        -s section      select man section(1-8)
        -d distro       distribution name
        -f format       appropriate format
        -o              list available values for other options.
        -h              print this help

EXAMPLES:
        $ freebsdman ps
                will display man page of 'ps' command from latest
                FreeBSD version in terminal(format: ascii).

        $ freebsdman -d 'solaris' -f pdf mpstat > mpstat.pdf
                will generate 'mpstat.pdf' file which contains
                manual page of 'mpstat' from solaris.

        $ freebsdman -o
                will list all valid values for 'freebsdman' options.

        $ freebsdman -d 'x11' X
                will display 'X' manual (note: manual will not be displayed
                without -d 'x11' option)

Download: freebsdman

One day I’ll have FreeBSD in my Lappy :)

attachlicense – script to add license into the source files

Hello mates,

Its has been some kind of twisted weekend to me, Not enough time to post, But I got some time with my lappy to create a script which will automatically add my license text,  Yes, its like the previous post, but this one is a bash script, instead of vi script(Ooh, its a damn interesting to do vi scripting.. defenately I’ll try something new with it).

Here is the script,

attachlicense

And here is my customized gplv2,

/*
* <description>
* Copyright (C) <year>  <name> <email>   
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see .
* 
*/

This script will not attach if it see a keyword already present in the source file, so forgive me to put this stupidity in place, I just wrote it for no purpose, I need to update this script to do more, but don’t have enough time to do it. To attach the license to a list of .c and .h files, all you need to do is,

$ find "${MYDEVDIR}" -name '*.c' -o -name '*.h' | xargs attachlicense -f gplv2.txt -n 'Mohan Raman' -e 'mohan43u@gmail.com' -w -v  

I just showed only two options, there is more options to customize,

Finally, its just for fun script, and not yet fully completed, so please don’t try this with production sources, also if you have suggessions, let me know.

attachlicense

Mail with Attachments (Unix shellscript)

Hi friends,

Lot of us already know howto send attachments in mails using shellscript. Lot of us already wrote these kind of script. Here is one more script to do the same thing. I already added all the details in the script itself. It is self explainable. If somebody wants, take it.

#!/bin/sh 
# generatemail.sh - script to send mail with multiple attachments.
# Wrote By: me! mohan43u.
# Date: Thu Mar 06 11:56:12 IST 2009
# License: Its in public domain. Use it whatever way you can.

USAGE="USAGE:

	generatemail.sh [-t tolist] [-s subject] [-e encodetype] 
		[-h] ATTACH_FILE1 ATTACH_FILE2 ...
"
HELP="${USAGE}
OPTIONS:
	-t tolist	comma seperated e-mail addresses
				(Default: current userid).

	-s subject	Subject line (Default: 'test mail from generatemail.sh
				 script')

	-e encodetype	Encoding type (x-uuencode/base64). only GNU uuencode
			can encode in base64. If you are using this script
			in Old school unixes(solaris, HP-UX, AIX, etc) use
			only 'x-uuencode'. Linux can handle both types
			(Default: x-uuencode).

	-h		Print this Help text

This script will read standard input as a mail body and takes filenames as 
arguments to generate a preformatted mail content in satndard output. you can
simply pipe the output to any MTA (sendmail, postfix etc.,) to send a mail
with attachments.

Eg:
	$ generatemail.sh -t foo@bar.com -s 'mail with attachments' file1 \
		file2 | /usr/sbin/sendmail -v -i -t
	hi foo,
		I attached file1 and file2 with this mail. Take a look at it.

	Thanks,
	ben.
	[CTRL-D]
	$

	This above command line will attach file1 and file2 and send a mail
	to foo@bar.com. The files file1 and file2 will be encoded in
	'x-uuencode' and attached with this mail.
"

TO="${USER}"
SUBJECT="testmail from generatemail.sh script"
BOUNDARY="`date +'%G%m%d%H%M%S'`"
ENCODE_TYPE="x-uuencode"

while getopts 't:s:e:h' OPTIONS
do
	case "${OPTIONS}" in
	t) TO="${OPTARG}";; 
	s) SUBJECT="${OPTARG}";;
	e) ENCODE_TYPE="${OPTARG}";;
	h) echo "${HELP}" && exit 0;;
	\?) echo "${USAGE}" && exit 1;;
	esac
done

SHIFT_COUNT=`expr "${OPTIND}" - 1`
shift "${SHIFT_COUNT}"

header()
{
	echo "To: ${TO}"
	echo "Subject: ${SUBJECT}"
	echo "Mime-Version: 1.0"
	echo "Content-Type: multipart/mixed; boundary=\"${BOUNDARY}\""
	echo "Content-Disposition: inline"
	echo "User-Agent: generatemail.sh 0.1"
}
create_octet_part()
{
	BASENAME=`basename "${1}"`
	FILE="${1}"

	if test "${ENCODE_TYPE}" = "base64"
	then
		ENCODED_MESSAGE=`uuencode -m "${FILE}" "${BASENAME}" \
		| egrep -v '^begin|^='`
	else
		ENCODED_MESSAGE=`uuencode "${FILE}" "${BASENAME}"`
	fi

	echo "Content-Type: application/octet-stream"
	echo "Content-Disposition: attachment; filename=\"${BASENAME}\""
	echo "Content-Transfer-Encoding: ${ENCODE_TYPE}"
	echo
	echo "${ENCODED_MESSAGE}"
	echo
}
create_text_part()
{
	echo "Content-type: text/plain; charset=utf-8"
	echo "Content-Disposition: inline"
}

# Main

BODY="`cat`"
header
echo
echo "--${BOUNDARY}"
create_text_part
echo
echo "${BODY}"
echo
for FILENAME
do
	echo "--${BOUNDARY}"
	create_octet_part "${FILENAME}"
done
echo "--${BOUNDARY}--"