Posts Tagged ‘Bash Scripting’

iphone backup via ssh

I recently upgraded my iphone firmware, but since i’m really a paranoid man, i wanted to backup all of my critical stuffs stored on the phone first.
Yes, i know itunes keeps backups of the iphone contents but really, i didn’t want to trust an application and take the risk to loose my contacts, sms, and so on, especially when i was going to do something not expected by that application ( yes, i’m talking of the jailbreak :P ).
So, since my iphone was already out of jail, i was able to login via ssh into the device system. It simply keep all of your stuffs in (sqlite) databases and folders, so if you know where-is-what the backup process is really simple. So i wrote a simple shell script to do the backup, and here it is. In order to use it, you should:

  • Mkdir the backup folder you wish to use and copy the script inside
  • Connect your iphone to a network reachable from the machine you are going to run the script from
  • Fill the IPHONE_ADDRESS field with the ip address on the given network assigned to the iphone
  • Toggle flags to decide what you want to backup or not
  • Run the script :P

You should also ( if you did not before ) append your public rsa id to your iphone root ‘authorized_keys’ file in order to avoid being prompted for password at each step, as explained in this previous entry.

I did this as root, but you may try running it as mobile if you wish, just replace any root@ with mobile@ using vim or sed ‘s/root@/mobile@/g’.

Also, this is a good chance ( to add the security tag :P ) to remember you, jailbreaked iphone users, to change the default ssh “alpine” password, because otherwise, if you connect to untrusted (public) networks with the ssh daemon running, stealing all of your personal data may be easy as running this script :P ( If your first thought is “they won’t know my ip!”, give up: a simple nmap scan will reveal you quickly and easily )

You can download the script here

Read more


Advanced Shell Scripting

Yesterday I delivered a lesson for the advanced Linux course arranged by LUG Roma3 at Roma Tre University on voluntary base. Here are the slides I sketched out for this occasion.




It’s not really “advanced” stuff, but sounds just better than “basics +1″ ;)


Web app backup simple shell script

Backup Recently, the unofficial discussion board for students of my university department went down because of a misunderstanding with their hosting provider who also reassigned their machine to another customer wiping out all of their data. Now they restore the board but the last backup was dated back to last year so lots of data have been lost. Anyway, when one of the admins announced on facebook the board was up again, i ironically commented writing a small shell script that can be used to backup a simple mysql-based web application such as a discussion board or a CMS installation such as drupal, joomla, wp or whatever… But then i thought this happend more frequently than expected to people i know, so i decided to post that script here :)

#!/bin/bash
#-----------------------------------
# Web App. Dumb Backup Script
# http://www.n0on3.net
#-----------------------------------
$user='your-username-here'
$server='your-domain-name-here'
$appname='your-webapp-name-here'
$apppath='your-webapp-path-here'
$mysqluser='your-mysql-user-here'
$mysqlpassword='your-mysql-pwd-here'
$dbname='your-mysql-db-name-here'
#-----------------------------------
d=`date +'%d-%m-%y'`
ssh $user@$server "tar cjvf backup-$appname-$d-www.tar.bz2 $apppath"
ssh $user@$server "mysqldump -u $mysqluser
                  --password=$mysqlpassword $dbname
                  > backup-$appname-$d-db.sql"
scp $user@$server:$HOME/$user/backup-$appname-$d-www.tar.bz2 \
                  backup-$appname-$d-www.tar.bz2
scp $user@$server:$HOME/$user/backup-$appname-$d-db.sql \
                  backup-$appname-$d-db.sql
ssh $user@$server 'rm backup-$appname-$d-*'

Please notice that here you are using ssh login without password, that means you have to append your client machine public rsa id to your server authorized keys file.
But more important, here you are writing your database password in plaintext because the script must use it, so if you keep such a script on your client machine remember to encrypt it or to take any proper precaution ;)


NTFS read/write on Mac Os X

I’m restoring my osx machine, and it seems Snow Leopard is capable to read/write NTFS Filesystems without any ntfs-3g or whatever 3rd stuffs. It just, by default, mount the volumes read only. Searching the web, it turns out that for each volume, you can get the volume name or UUID, by plugging the drive and running:

diskutil info /Volumes/volume_name | egrep "Volume (UUID|Name)"

and then insert one of following lines in your /etc/fstab

UUID=your_volume_uuid none ntfs rw
LABEL=your_volume_name none ntfs rw

That seems really boring to do that each time you want to plug a new volume…
So you can think to edit ‘/System/Library/Filesystems/ntfs.fs/Contents/Info.plist’, adding the ‘-o rw‘ option to the FSMountArguments element, but it seems that is completely ignored…

Well, know what, at least you can wrap the /sbin/mount_ntfs to add the rw option:

  • move /sbin/mount_ntfs to something like mount_ntfs_original
  • write a shell script for /sbin/mount_ntfs, like:

  • #!/bin/bash
    /sbin/mount_ntfs_original -o rw "$@"
  • give it the right permissions and enjoy ;)

Download YouTube videos with a shell script

In my last post I wrote about extracting audio from in-streaming-format videos, that people often may get from content streaming services like eg. YouTube. As said, those videos may be downloaded in several ways that may also be different depending on the hosting service. A pretty effective way is using some plugin for your browser that shows the URL of the source the flash/html5 player download the video from, and allow you to get the content from that. Anyway, just for fun, here is a small shell script that can be used to download videos contents from youtube.

#!/bin/bash
#
################################################################
# --- YouTube Downloader ------------------------------------- #
# --- http://www.n0on3.net ----------------------------------- #
################################################################
#
which wget &>/dev/null; 
if [ $? -ne 0 ];then echo "> Please install wget =)!"; exit; fi
if [ $# -ne 1 ];then 
	read -p "> Enter Youtube video ID: " VID
else	VID=$1; fi
read -p "> Do you want me to try to get the HD version ? [y|N]: " HD
case $HD in Y|y|yes) FMT=22;; N|n|*)FMT=18;; esac
OK=1; PAGE=`tempfile`
wget -q -O $PAGE "http://www.youtube.com/watch?v=$VID"
SIG=`cat $PAGE | egrep -o "\"t\": \"[^\"]+\"" | \
     sed 's/\"//g' | awk '{print $2}'`
URL="http://www.youtube.com/get_video?fmt=$FMT&video_id=$VID&t=$SIG"
TITLE=`cat $PAGE | grep VIDEO_TITLE | awk '{$1="";print $0}' `
TITLE=$(echo $TITLE | sed 's/.\(.*\)../\1/' | sed 's/\\//g')
read -p "> Is the title \" $TITLE \" right for output file ? [Y|n]: " TOK
case $TOK in 	n|N|no) read -p "> Enter filename: " TITLE ;;
		Y|y|yes|*) ;; 
esac; echo "> Saving file to $TITLE.mp4 ... "
wget -q -O "$TITLE.mp4" $URL &>/dev/null
if [ $? -ne 0 ];then echo "> Download failed!";
	OK=0; rm -fr "$TITLE.mp4";
	if [ $FMT == 22 ];then 
		echo "> Maybe the HD version isn't available.."
		FMT=18; echo "> Downloading the non-HD version ..."
	URL="http://www.youtube.com/get_video?fmt=$FMT&video_id=$VID&t=$SIG"; 
	wget -q -O "$TITLE.mp4" $URL &>/dev/null; 
		if [ $? -ne 0 ];then 
			echo "> Download failed!"; rm -fr "$TITLE.mp4";
		else OK=1; fi; 
	fi
fi
if [ $OK -eq 1 ]; then echo "> Successfully Downloaded to $TITLE.mp4 !"
fi;
#

To be said, youtube term of service, at point 6.C, states using YouTube you accept not to access the contents with any technology but the YouTube website player or other stuff explicitly authorized by YouTube. ( That means, yeah, you should really not use this u.u” ).


Tree

Tree Some times you want to recursively check the contents of a given directory right in your shell  asking for its inherent tree.

But sometimes the ‘tree’ utility is not available, and you may have no rights to install it or you  just  don’t want to. Other times you may even have to deal with ports to  get  the simple tree  utility.

In these cases, find and sed are here to help:

#!/bin/bash
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

Return top

About me