script – Sheep Guarding Llama https://sheepguardingllama.com Scott Alan Miller :: A Life Online Tue, 12 Jan 2010 05:57:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.1 Testing Socket Connections Programmatically https://sheepguardingllama.com/2010/01/testing-socket-connections-programmatically/ https://sheepguardingllama.com/2010/01/testing-socket-connections-programmatically/#respond Tue, 12 Jan 2010 05:57:40 +0000 http://www.sheepguardingllama.com/?p=4972 Continue reading "Testing Socket Connections Programmatically"

]]>
Often we have to use “telnet remotehost.somewhere.com 80” to test if a remote socket connection can be established.  This is fine for one time tests but can be a problem when it comes time to test a number of connections – especially if we want to test them programmatically from a script.  Perl to the rescue:

#!/usr/bin/perl
use IO::Socket;

my $sock = new IO::Socket::INET (
                   PeerAddr => $ARGV[0],
                   PeerPort => $ARGV[1],
                   Proto => tcp,
);

if ($sock) { print "Success!\n" } else { print "Failure!\n" };
close($sock);

Just copy this code into a file called “sockettest.pl” and “chmod 755 sockettest.pl” so that it is executable and you are ready to go.  (This presumes that you are using UNIX.  As the script is Perl, it should work anywhere.)
To use the code to test, for example, a website on port 80 or an SSH connection on port 22 just try these:

./sockettest.pl www.yahoo.com 80
./sockettest.pl myserver 22

You aren’t limited to known services.  You can test any socket that you want.  Very handy.  Now, if you have a bunch of servers, you could test them from a simple, one line BASH command like so (broken so as not to be one line here for ease of reading…)

for i in myserver1 myserver2 yourserver1 yourserver2 someoneelsesserver1
do
  echo $i $(./sockettest.pl "$i" 80)
done
]]>
https://sheepguardingllama.com/2010/01/testing-socket-connections-programmatically/feed/ 0
Simple Ruby Twitter Client – Tweet [Ruby] https://sheepguardingllama.com/2008/10/simple-ruby-twitter-client-tweet-ruby/ https://sheepguardingllama.com/2008/10/simple-ruby-twitter-client-tweet-ruby/#comments Fri, 31 Oct 2008 18:41:58 +0000 http://www.sheepguardingllama.com/?p=2833 Continue reading "Simple Ruby Twitter Client – Tweet [Ruby]"

]]>
This is my simple, Ruby based Twitter client using Curl designed for UNIX systems like Linux, Mac OSX, FreeBSD, Solaris, etc.  The only requirements are Curl and Ruby.

In order to use Tweet, simply copy all of the included code into your favourite text editor (I use vi) and save as ‘Tweet’.  Don’t forget to “chmod a+x tweet” so that it is executable.  I suggest moving Tweet into your path (perhaps you should consider /usr/local/bin as a recommended directory) to make it easier to use.  I have designed Tweet to be useful to users on a multi-user UNIX system.  It is a command-line utility that simply accepts text input and posts that text, maximum of 144 characters, to your Twitter account.  An existing Twitter account is necessary so sign up if you do not have one already.

There is very little to know in order to use Tweet [Ruby].  (Should I name this RTweet perhaps?)  The one thing that is needed is to set your username and password.  Tweet [Ruby] is designed to accept username and password data from the system environmental variables $tweetuser and $tweetpass.  This design decision was made because it makes it extremely simple to have multiple users on the same system be able to use Tweet [Ruby] transparently from one another.  If you desire, you can bypass this setting by changing the “unset” user and pass settings in the code to your username and password.  This hardcoding is not recommended but is available if needed.

Once you have your username and password set (you can see what your settings currently are by using the -t option) all you need to do is enter the text that you want to publish.  Here is an example:

tweet ‘This is my first post from Tweet [Ruby].  Thanks Scott, this is great.’

Here is the code, go crazy.

#!/usr/bin/ruby -w
#Scott Alan Miller's "Tweet" - Twitter Command Line Script

text = ARGV[0].chomp
user = "unset"         #Supplied Username
pass = "unset"         #Supplied Password
url  = "http://twitter.com/statuses/update.xml"
ver  = "1.0"

user = ENV['tweetuser'] if ENV['tweetuser']
pass = ENV['tweetpass'] if ENV['tweetpass']

if    text.length <= 0
  puts "Please enter text to post."
elsif text.length >= 144
  puts "Please limit post to 144 chars."
elsif text == "-v"  # Version Message
  puts "Current Version of Tweet [Ruby] is " + ver
elsif text == "-h"  # Help Message
  puts "Tweet [Ruby] Help: \n"
  puts "To set environmental username and password:"
  puts "  export tweetuser=yourusername"
  puts "  export tweetpass=yourpassword\n"
  puts "Usage:"
  puts "  tweet \'This is my message.\'"
elsif text == "-t"  # Variable Test
  puts "Username: " + user
  puts "Password: " + pass
else
  result = %x[curl -s -S -u #{user}:#{pass} -d status="#{text}" #{url}]
  puts "Update Failure" if result.grep(/text/) == nil
end

If you end up using my little Twitter client, please send me a Tweet to let me know!

tweet ‘@scottalanmiller Using Tweet, best Twitter client ever.  Ruby rulz.’

]]>
https://sheepguardingllama.com/2008/10/simple-ruby-twitter-client-tweet-ruby/feed/ 5