Posts Tagged ‘perl’

Listing all users from your Google Apps Gmail Service

Pulls the list of users from Gmail and executes a Perl scripts called g2.pl for internal processing.

#!/usr/bin/python
 
import os, sys, string, re
 
# need a way to check if lib/python exists then sys.path.append
sys.path.append("lib/python")
 
import gdata.apps.service
 
if len(sys.argv) != 2:         
	print 'Usage: gdata_user.py <domain>'     
        sys.exit(1)
 
a_domain_name   = sys.argv[1]
a_email         = "useraccount@"+a_domain_name
a_pass          = "userpassword"
 
try:
	service = gdata.apps.service.AppsService(email=a_email, domain=a_domain_name, password=a_pass)
	service.ProgrammaticLogin()
except Exception:
	sys.exit("Error: Could not login")
	pass
 
# all users
list = service.RetrieveAllUsers()
# 100 users for debugging
#list = service.RetrievePageOfUsers(start_username="jdoe")
 
for person in list.entry:
	if "'" in person.name.given_name:
		person.name.given_name = re.sub(r"'", "\'", person.name.given_name)
	if "'" in person.name.family_name: 
		person.name.family_name = re.sub(r"'", "\'", person.name.family_name)
	g2_update = "./g2.pl " +person.login.user_name+"@"+a_domain_name+" \""+person.name.given_name+"\" \""+person.name.family_name+"\""
	os.system(g2_update)
Posted: April 1st, 2011
Categories: Perl, programming, Python
Tags: , , ,
Comments: No Comments.

Automating SSH public key pushes with Perl

The code below uses Expect and SSH to create & permission the .ssh directory. Followed by SSH copying the local temp file as the authorized_keys file to the .ssh subdir on the target server .

Be sure to set StrictHostKeyChecking=no in the SSH client side config.

 
#!/usr/bin/perl
 
use strict;
use warnings;
use English;
use Expect;
use Net::Ping;
my $username = "USER";
my $password = "PASS";
my $homedir  = "\/export\/home\/$username";
my $ssh_dir  = "$homedir\/.ssh";
my $ssh_pub  = "
ssh-dss BLAH BLAH use your own public key file entry here.
";
if ( ! $ARGV[0] ) {
    print "$0  \n";
exit
}
my $host = "$ARGV[0]";
my $p  = Net::Ping->new();
if ( $p->ping($host) ) {
    print "Deploying public key to $host \n";
    create_ssh_dir();
    chmod_ssh_dir();
    push_ssh_key();
} else {
    print "Seems $host is not reachable \n";
}
$p->close();
sub create_ssh_dir {
    my $ssh_cmd  = "/usr/bin/ssh $username\@$host 'mkdir $ssh_dir'";
    my $timeout  = '5';
    my $exp      = Expect->spawn($ssh_cmd) or die "Cannot spawn ssh command \n";
    $exp->expect($timeout, ["Password:"]);
    $exp->send("$password\n");
    $exp->soft_close();
}
sub chmod_ssh_dir {
    my $ssh_cmd  = "/usr/bin/ssh $username\@$host 'chmod 755 $ssh_dir'";
    my $timeout  = '5';
    my $exp      = Expect->spawn($ssh_cmd) or die "Cannot spawn ssh command \n";
    $exp->expect($timeout, ["Password:"]);
    $exp->send("$password\n");
    $exp->soft_close();
}
sub push_ssh_key {
    my $tmp_file  = "/tmp/authorized_keys.$$";
    chomp $ssh_pub;
    open  ( AUTHKEY, ">$tmp_file") || die ("Unable to create $tmp_file\n");
    print AUTHKEY "$ssh_pub" . "\n";
    close ( AUTHKEY );
    my $scp_cmd  = "/usr/bin/scp $tmp_file $username\@$host:$ssh_dir/authorized_keys";
    my $timeout = '5';
    my $exp = Expect->spawn($scp_cmd) or die "Cannot spawn scp command \n";
    $exp->expect($timeout, ["Password:"]);
    $exp->send("$password\n");
    $exp->soft_close();
    unlink("$tmp_file");
}
Posted: January 4th, 2010
Categories: automation, Perl, programming
Tags: , ,
Comments: No Comments.

Perl Broken Pipe

The code was producing “Broken Pipe” errors intermittently. The only workaround I could figure out was to ignore the sigpipe.

$SIG{PIPE} = 'IGNORE';
Posted: October 29th, 2009
Categories: Perl, programming
Tags:
Comments: No Comments.

Removing Whitespace using Perl

$whitespace_beginning_of_string =~ ( s/^\s+// );
$whitespace_ending_of_string  =~ ( s/\s+$// );
Posted: October 26th, 2009
Categories: Perl, programming
Tags:
Comments: No Comments.

Using Perl to count the characters of string

#!/usr/bin/perl
my $string = "abcdefg";
my $count  = length ( $string );
print "Total characters: $count \n";
Posted: October 10th, 2009
Categories: Perl, programming
Tags:
Comments: No Comments.