Here is two small functions to convert IP addresses from string representation to integer and vice versa.
def inet_aton(ip) ip.split(/\./).map{|c| c.to_i}.pack("C*").unpack("N").first end def inet_ntoa(n) [n].pack("N").unpack("C*").join "." end
Here is two small functions to convert IP addresses from string representation to integer and vice versa.
def inet_aton(ip) ip.split(/\./).map{|c| c.to_i}.pack("C*").unpack("N").first end def inet_ntoa(n) [n].pack("N").unpack("C*").join "." end
Here is a completely useless filesystem based on MySQL database storage – mysqlfuse, implemented with Fuse.
I didnt find any way how i can use it, but meanwhile, this fs working. Not perfect of course, in that case its not maintained for a long time. Doesnt support information about free drive space, so any filemanager keeps saying ‘Error: No space left on device’. Such case making it more useless.
It`s really easy to set it up.
First, we need to install developer headers for fuse:
$ apt-get install libfuse-dev
Next, getting sources (32bit only, not working in 64bit):
$ wget http://voxel.dl.sourceforge.net/sourceforge/mysqlfs/mysqlfs-0.4.0-rc1.tar.bz2
Unpack it, and compile:
$ tar -xjvf mysqlfs-0.4.0-rc1.tar.bz2 $ cd mysqlfs-0.4.0-rc1 $ ./configure && make && make install
Next, we need to setup the database
CREATE DATABASE mysqlfs; GRANT SELECT, INSERT, UPDATE, DELETE ON mysqlfs.* TO mysqlfs@"%" IDENTIFIED BY 'password'; FLUSH PRIVILEGES;
And create database schema. SQL file located in root folder of the sources
$ mysql -uroot -p mysqlfs < schema.sql
And finally, mount filesystem to some folder:
$ mysqlfs -ohost=MYSQLHOST -ouser=MYSQLUSER -opassword=MYSQLPASS -odatabase=mysqlfs MOUNT_DIR
Now, its gonna be working. To use automatic configuration parameters you can create section [mysqlfs] in your mysql configuration file (my.cnf)
Parameters:
-ohost=MySQL server host -ouser= MySQL username -opassword= MySQL password -odatabase= MySQL database name
That`s it. Anyway, using FUSE there is a way to create so weird filesystems proxy. For example, there is SQLite over FUSE. And it is too old. Next time i`ll write about Amazon S3 over FUSE projects.
For a long time i was thinking that Amazon`s Simple Storage Service (S3) is very complicated thing. But, it was before i tried it. Couple days ago, i got account to S3 and started exploring API`s and architecture. Now i see how stupid i was
It`s really easy to handle all operations with files and buckets. Pricing also comfortable.
Welcome to cloud computing!
I started using it with Ruby. Regular gem and docs can be found at http://amazon.rubyforge.org/
So, the first useful tool i decided to created – simple uploader of local files to amazons server.
First, we need to create bucket and make it public:
Bucket.create('NAME_HERE',:access => :public_read)
Here`s the client ruby script:
#!/usr/bin/ruby require 'rubygems' require 'aws/s3' include AWS::S3 $s3_bucket = "BUCKET_NAME" $s3_key = "API_KEY" $s3_secret = "API_SECRET" def s3_store(localfile) if File.exists?(localfile) && File.readable?(localfile) puts "Uploading file [#{localfile}]. Size: #{File.size(localfile)} bytes." name = File.basename(localfile) Base.establish_connection!(:access_key_id => $s3_key, :secret_access_key => $s3_secret) S3Object.store(name, open(localfile), $s3_bucket, :access => :public_read) puts "Download link: http://s3.amazonaws.com/#{$s3_bucket}/#{name}" else puts "File not exists or not accessible. Please check file and try again!" end end path = ARGV[0] if !path "Please specify the file to upload." else s3_store(path) end
Download script: http://files.sosedoff.com/036cfedd/
BTW, I found cool firefox add-on to manage S3 objects/files. It`s pretty easy.
Link to extension – http://www.s3fox.net
Screenshot:

Here is php version of ruby class that i made a long time ago. The same functionality and results.
<? class ImageScale { private function changeGeometry($sz,$value) { if ($sz['width'] > $sz['height']) { // horizontal image $newsz['width'] = $value; $newsz['height'] = ceil(($newsz['width'] * $sz['height']) / $sz['width']); } else { // vertical image $newsz['height'] = $value; $newsz['width'] = ceil(($newsz['height'] * $sz['width']) / $sz['height']); } return $newsz; } private function changeGeometry2($sz,$value) { $newsz['width'] = $value; $newsz['height'] = ceil(($newsz['width'] * $sz['height']) / $sz['width']); return $newsz; } /** * Make thumbnail of specified maximum side size */ public function processThumb($source_path, $dest_path, $sidesize, $quality=85, $scale_method=1) { if (file_exists($source_path) && is_readable($source_path)) { $image = new Imagick($source_path); $geometry = $image->getImageGeometry(); // ['width', 'height'] if ($geometry['width'] > $sidesize) { if ($scale_method == 1) $geometry = $this->changeGeometry($geometry,$sidesize); if ($scale_method == 2) $geometry = $this->changeGeometry2($geometry,$sidesize); } $image->cropThumbnailImage($geometry['width'],$geometry['height']); $image->setCompression(Imagick::COMPRESSION_JPEG); $image->setCompressionQuality($quality); return $image->writeImage($dest_path); } return false; } /** * Make square thumbnail */ public function processRectThumb($source_path, $dest_path, $size=150, $quality=85) { if (file_exists($source_path) && is_readable($source_path)) { $image = new Imagick($source_path); if ($image) { $image->cropThumbnailImage($size,$size); $image->setCompression(Imagick::COMPRESSION_JPEG); $image->setCompressionQuality($quality); return $image->writeImage($dest_path); } } return false; } } ?>
Download file – http://files.sosedoff.com/1167c8db/
Here is general init.d nginx script that working on Debian and RHEL/CentOS systems. It`s set to default configuration with main path to nginx = /usr/local/nginx
#!/bin/sh # # Init file for nginx # # chkconfig: 2345 55 25 # description: Nginx web server # # processname: nginx # config: /usr/local/nginx/nginx.conf # pidfile: /usr/local/nginx/nginx.pid # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and # run 'sudo update-rc.d nginx defaults', or use the appropriate command on your # distro. For CentOS/Redhat run: '/sbin/chkconfig --add nginx' # # Author: Ryan Norbauer <ryan.norbauer@gmail.com> # Modified: Geoffrey Grosenbach http://topfunky.com # Modified: David Krmpotic http://davidhq.com # Modified: Vishnu Gopal http://vish.in # Modified: Dan Sosedov <dan.sosedoff@gmail.com> set -e PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME CONFIGFILE=/usr/local/nginx/conf/nginx.conf PIDFILE=/usr/local/nginx/logs/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME # Gracefully exit if the package has been removed. test -x $DAEMON || exit 0 d_start() { $DAEMON -c $CONFIGFILE || echo -en "\n already running" } d_stop() { kill -QUIT `cat $PIDFILE` || echo -en "\n not running" } d_reload() { kill -HUP `cat $PIDFILE` || echo -en "\n can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" d_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" d_stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." d_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" d_stop # One second might not be time enough for a daemon to stop, # if this happens, d_start will fail (and dpkg will break if # the package is being upgraded). Change the timeout if needed # be, or change d_stop to have start-stop-daemon use --retry. # Notice that using --retry slows down the shutdown process somewhat. sleep 1 d_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2 exit 3 ;; esac exit 0
Usage:
$ sudo /etc/init.d/nginx (start|stop|restart|reload)
You also can download it – http://files.sosedoff.com/e570e29f/
Simple class that providing scaling (rectangle and thumbnails) for images using RMagick and Ruby.
Code:
class ImageScale def change_geometry(sz,value) w = sz[0] ; h = sz[1] if w > h sz[0] = value sz[1] = ((value * h) / w).floor else sz[1] = value sz[0] = ((value * w) / h).floor end return sz end def make_rect(file_in,file_out, width, quality=85, sharp=false) if FileTest.exists?(file_in) begin img = Magick::Image.read(file_in).first img.crop_resized!(width,width, Magick::CenterGravity) img = img.sharpen(0.5, 0.5) if sharp img.write(file_out) { self.quality = quality } return true if FileTest.exists?(file_out) rescue Magick::ImageMagickError return false end end return false end def make_thumb(file_in,file_out, width_to, quality=85, sharp=false) if FileTest.exists?(file_in) begin img = Magick::Image.read(file_in).first info = [img.columns,img.rows] sz = change_geometry(info, width_to) img = img.resize(sz[0],sz[1]) img = img.sharpen(0.5, 0.5) if sharp img.write(file_out) { self.quality = quality } return true if FileTest.exists?(file_out) rescue Magick::ImageMagickError return false end end return false end end
Ok, let`s see how this class working. For example, we have source image:

Function ImageScale.make_rect(src,dest,64) will produce such image:

Function ImageScale.make_thumb(src,dest,200) will produce thumbnail:
![]()
There is optional parameter sharp to use sharping. Optional parameter quality is set to 85% compression value.
Need to identify web crawlers and mobile devices with your web-app ? Here is the list of couple regular expressions you can use (most common values):
Mobile Devices:
/(blackberry|motorokr|motorola|sony|windows ce|240x320|176x220|palm|mobile|iphone|ipod|symbian|nokia|samsung|midp)/i
Web Spyders:
/(google|yahoo|baidu|bot|webalta|archiver|crawler|spyder)/i
Since i got trouble while connecting to wireless network with given parameters in linux, i tried a lot of ways to get my internet working.
So, the problem is: Windows Network manager have special option – Key index. The index is transmitted with the encrypted message. The receiver then looks-up the key corresponding to the transmitted index and uses it to decrypt the message. But linux (ubuntu) network managers i`ve tried have no key index field, so there is no way to set it up properly with gui. Connection won`t work if index value set to incorrect value.
After giving up to find any useful gui program i wrote small shell script only for one network (because i have no access to router and no chance to improve some security settings):
#!/bin/bash # settings interface="ath0" # wireless interface, default to wlan0 essid="NETWORK_NAME_HERE" key="YOUR_KEY_HERE" index="4" # can be [1..4] # check permission if [ "$(id -u)" != "0" ]; then echo "Run this script under root" 1>&2 exit 1 fi # show information echo "Settings:" echo "-> Interface: $interface" echo "-> Wifi ESSID: $essid" echo "-> Key: $key" echo "-> Key Index: $index" # perform association ifconfig $interface down iwconfig $interface essid $essid iwconfig $interface key $key [$index] iwconfig $interface key [$index] ifconfig $interface up dhclient $interface
Download shell script – http://files.sosedoff.com/9591756c/
For example, in your bash shell script you`re going to use some root-specific commands like network operations, mounting devices and so on. There are couple easy ways to check if your script is executing under root privileges.
#!/bin/bash # ... if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1 fi # ...
Another way: use EUID. When user account created a user ID is assigned to each user. Bash shell stores the user ID in $UID variable. Your effective user ID is stored in $EUID variable.
#!/bin/bash # ... if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi # ...
As previous post was about fetching covers media from Amazon Web Services, this post will be about fetching covers from popular music site – Last.fm. API documentation page
#!/usr/bin/ruby require 'rubygems' require 'net/http' require 'cgi' require 'xmlsimple' # key from API documentation $lastfm_key = "b25b959554ed76058ac220b7b2e0a026" $lastfm_host = "ws.audioscrobbler.com" def fetch_cover(artist, album) artist = CGI.escape(artist) album = CGI.escape(album) path = "/2.0/?method=album.getinfo&api_key=#{$lastfm_key}&artist=#{artist}&album=#{album}" data = Net::HTTP.get($lastfm_host, path) xml = XmlSimple.xml_in(data) if xml['status'] == 'ok' then album = xml['album'][0] cover = { :small => album['image'][1]['content'], :medium => album['image'][2]['content'], :big => album['image'][3]['content'] } return cover end return nil end puts fetch_cover('Nickelback', 'Dark Horse').inspect
Download ruby script