Hack 65. Put Screenshots Automatically on the Web
Show off to the world what you're up to. This little hack is fun. It does not improve system performance in any way, but it allows you to share with the world what you're currently doing. This hack is a perfect way for you to demonstrate how cool your Linux desktop is by automatically taking screenshots of your desktop and uploading them to a web server. The script this hack uses to upload the screenshot is written in Perl and requires Net::FTP, a web server, and a simple program called scrot; all of these are freely available online. 8.12.1. Installing scrotYou can find scrot, a command-line screen-capture tool (similar to import, which is included with ImageMagick), at http://linuxbrit.co.uk/scrot/. Extract, compile, and install the software with these few commands: foo@bar:~$ wget http://linuxbrit.co.uk/downloads/scrot-0.8.tar.gz foo@bar:~$ tar -zxvf scrot-0.8.tar.gz foo@bar:~$ cd scrot-0.8 foo@bar:~$ ./configure foo@bar:~$ make foo@bar:~$ su -c "make install" With scrot installed, you can use it to take screenshots anytime you want; just find the nearest terminal and type scrot. For more information on scrot, see the manpage. Perl is likely to be installed already on your Linux system, but if it isn't, you should use your distribution's installation tool to install it. In addition to the stock Perl installation, you also need the Net::FTP module that you can install by using CPAN, Perl's module repository: foo@bar:~# perl -e shell -MCPAN foo@bar:~# install Net::FTP 8.12.2. The CodeThis is where all the magic is. Just write each line into your favorite text editor, whether it is emacs or vim or something else, and save it as autoscreenshot.pl in $localfolder: #!/usr/bin/perl -w use Net::FTP; # Start FTP ## Define your variables $delay = "60"; # Set the screen captures in seconds $quality = "50"; # Set the quality of the screenshot $thumb = "25"; # Set percentage of the thumbnail produced $server = "your.server.com"; # Hostname of the server $username = "username"; # Put your username for the server here $password = "password"; # Put your password for the server here $serverfolder = "/home/me/www"; # This is the folder that you want the pictures to end up in $localfolder = "/home/me/autoscreensnap"; # This is the folder in which you are going to locally save the screenshots while( ) { system("scrot $localfolder/currentscreen.jpg --thumb $thumb --quality $quality"); # Let's take the screenshot $ftp = Net::FTP->new($server, Debug => 0); # Connect to FTP server $ftp->login($username, $password); # Let's log in print "OK Connected \n"; $ftp->cwd($serverfolder); # Change to the directory you want uploaded image to be in print "OK Changed directories \n"; $ftp->binary( ); # Set binary mode so the picture works $ftp->delete("$serverfolder/currentscreen.jpg"); # Delete old screenshot $ftp->delete("$serverfolder/currentscreen-thumb.jpg"); # Delete old thumbnail print "Deleted old screenshots\n"; $ftp->put("$localfolder/currentscreen.jpg"); # Uploading... $ftp->put("$localfolder/currentscreen-thumb.jpg"); #Upload some more.. print "OK Finished uploading files\n"; $ftp->quit; # Close session print "Sleeping for $delay seconds\n\n"; system("sleep $delay"); } The first half of the script is just to define all your variables. It is safe to change these and it is recommended that you do change them to suit your needs. These options include the following:
The second half of the script uses a loop that repeats itself over and over again. The first command tells the script to use scrot to take a screenshot and create a thumbnail. Then it connects to the web server and changes to a directory visible on the Internet. Once the script has completed, it deletes any old screenshots and thumbnails and uploads the new ones. After it completes this job, it sleeps for 60 seconds by default (this is adjustable) and then continues the loop once again. 8.12.3. Running the CodeTo run the script first change the directory to $localfolder. Your autoscreenshot.pl script should be sitting in this folder. Now you can run it in two different ways. You can run the script independently with the following commands: foo@bar:~$ chmod 777 autoscreenshot.pl foo@bar:~$ ./autoscreenshot.pl An alternative is to run it with the Perl interpreter: foo@bar:~$ perl autoscreenshot.pl John Cheng |