Perl scripts send log messages to Twitter
My Password's Gone!
If you are conscious of security, you've probably noticed that the code includes the password needed by the script to sign on to the Twitter service. The ssl option avoids sending the password in the clear; instead, the Perl Net::Twitter module communicates with Twitter's HTTPS URLs. But with applications popping out of the ground like mushrooms, the question is: How you can avoid disclosing your password to third parties? Imagine if just one weak link in the chain were to reveal your password – time to panic.
A recent addition by Twitter, OAuth, solves this problem. This open protocol gives various applications privileges for a user account. Instead of forwarding your username and password to various providers and hoping that nobody exploits your generosity, OAuth assigns every application a unique token. The token can be revoked at any time without affecting other providers who are using separate tokens.
The Perl Net::Twitter module supports OAuth; however, Twitter insists that you register the application [3] before it gives you the Token Consumer Key and Consumer Secret. That's the theory, anyway, if the service is working – which it was not when I was writing this article.
Logging with Twitter
Listing 2 demonstrates another application for the Twitter API: a Perl module that you can embed in other Perl processes to send messages to Twitter – the twit() function. In typical Perl fashion, the function accepts either a message in the form of a string or a reference to a hash with a message field. A second parameter, home, points to the user's home directory, just in case the root user is running the program (e.g., from a backup script). Twitsend.pm hopes to find a YAML configuration file in the home directory named .twitsend that contains the password for the Twitter account. In this way, the password is in just one place and not in multiple scripts.
Listing 2
TwitSend.pm
On My Balcony
My Perl-controlled irrigation system [4] has provided an invaluable and reliable service to the plants on my balcony for more than two years now. However, I do like to keep an eye on what it's doing while I'm away, so why not Twitter the pump activity so that I can check it from any Internet cafe or via my mobile phone?
After just five minutes, I was able to interface the TwitSend.pm module with the water script for the plant irrigation apparatus I described previously [4]. Then I called the twit() function with a message string and the path to a directory containing a YAML file with the account password for the perlsnapshot Twitter account.
The implementation is shown in the twitsend sample script (Listing 3), which posts the results to Twitter, as shown in Figure 3. The irrigation duration for the plants on a warm day in August took 60 seconds, and the backup on my development system completed at 11:49am. For the latest updates, feel free to check twitter.com/perlsnapshot.
Listing 3
twitsend
This approach is similar to that used recently by my fellow columnist, Charly Kühnast, with the ttytter client [5]. Today's script only needs a .twitsend YAML file in the home directory (e.g., /home/mike), with permissions of 0500, to ensure that the file is only readable and writable by the account owner. It contains a single password: line, which defines the Twitter password (Figure 4).
If you feel like using the free service on identi.ca instead, uncomment the identica directive in line 46 of the TwitSend.pm module. This directive points scripts to the free identi.ca clone rather than Twitter.
Also note that Twitter will silently ignore identical messages. A cron job that sends out a "Backup finished" message every day will not do the trick, but adding a timestamp will.
If other processes tweet on separate service accounts, you can then use your personal account to follow all or some of them. When you get back from vacation, all it takes is an unfollow on the Twitter website to unhitch the log from the message stream supplied by your friends.
The API, which is accessible via Net::Twitter, also lets you delete tweets; perform administrative functions such as follow(), unfollow(), or block(); send private messages; tap into the public message stream; and do many other things. Twitter restricts access to 100 access attempts per hour and monitors both IPs and accounts.
« Previous 1 2 3 Next »
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.