sub-routine: gettime();
Over the years working as a systems administrator I have a large sub-routine collection on my belt. One that I use quite frequently is gettime();. This is a simple routine someone in my group wrote to return a nicely formatted timestamp. It is use mostly for logging and such.
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
my $retval = sprintf("$year\-%02d\-%02d %02d:%02d:%02d",$mon,$mday,$hour,$min,$sec);
return $retval;
}
Example:
returns:
Popularity: 13% [?]
Hiding your STDIN
Ever write a script that you have to enter a elevated users password? or even your own? Check this out.
use Term::ReadKey;
print "Please enter your username: ";
chomp(my $username = <STDIN>);
print "Please enter your password for $username: ";
ReadMode('noecho');
chomp(my $password = ReadLine(0));
ReadMode('normal');
#for example only. You shouldn't print this...
print "\nYou entered $username for a username and $password for a password!!\n";
Using Term:ReadKey you can disable echo for STDOUT. This way no one will see your password from over your shoulder.
Popularity: 3% [?]
Accessing gallery2 using the API to return information about a gallery
I designed a website for a friend to sell a litter of puppies. He wanted a simple site that had galleries for each dog and one for the litter. I didn't want to maintain the site at all so I used a lot of pre built software. I chose to use Gallery2 to do all the picture stuff. It did everything I wanted (resize,rotate,etc). I didn't like how it looked though. I wanted to just have it do the back end stuff and I wanted to design the layout. To do this all I needed was the urls to access the images. Below is a function I wrote that will output all the information of a picture and its uri.
The script is located here
which outputs:
Array
(
[0] => Array
(
[title] => IMG_0028.JPG
[width] => 2272
[height] => 1704
[large_picture] => /p/d/71-1/IMG_0028.JPG
[thumb_picture] => /p/d/72-2/IMG_0028.JPG
[resized_picture] => Array
(
[0] => /p/d/73-2/IMG_0028.JPG
[1] => /p/d/74-2/IMG_0028.JPG
[2] => /p/d/75-2/IMG_0028.JPG
)
)
[1] => Array
(
[title] => IMG_0029.JPG
[width] => 2272
[height] => 1704
[large_picture] => /p/d/77-1/IMG_0029.JPG
[thumb_picture] => /p/d/78-2/IMG_0029.JPG
[resized_picture] => Array
(
[0] => /p/d/79-2/IMG_0029.JPG
[1] => /p/d/80-2/IMG_0029.JPG
[2] => /p/d/81-2/IMG_0029.JPG
)
)
)
You will need to modify the paths in the function to your installation.
A side note... To find gallery ids you will need to to run the following query.
Popularity: 2% [?]
Howto access an array that is in a hash
Say you have a hash element that contains an array and you want to loop through the array set. You usually have these types of data sets when you are trying to gather data.
example:
We will use the following code to build the hash:
#!/usr/bin/perl
use Data::Dumper;
my %data_set;
$data_set{'users'} = ();
$data_set{'users'}[0] = "testA";
$data_set{'users'}[1] = "testB";
$data_set{'users'}[2] = "testC";
$data_set{'users'}[3] = "testD";
$data_set{'users'}[4] = "testE";
$data_set{'users'}[5] = "testF";
print Dumper(%data_set);
Output:
$VAR1 = 'users';
$VAR2 = [
'testA',
'testB',
'testC',
'testD'
];
Now that we know the hash element is populated we want to access each array element. This can be done with making some crazy counter in a while loop. Check it out.
foreach my $user (@{$data_set{'users'}}){
print $user,"\n";
}
Now knowing you can access a array with in a hash using @{
Here is the same script above but using push:
#!/usr/bin/perl
use Data::Dumper;
my %data_set;
$data_set{'users'} = ();
push(@{$data_set{'users'}},"testA");
push(@{$data_set{'users'}},"testB");
push(@{$data_set{'users'}},"testC");
push(@{$data_set{'users'}},"testD");
foreach my $user (@{$data_set{'users'}}){
print $user,"\n";
}
Popularity: 2% [?]
Howto push a hash onto an array
This information was giving to me by my friend Matt
use Data::Dumper;
my @array;
push @array, {'key1' => 'value1', 'key2' => 'value2'};
push @array, {'key1' => 'value1', 'key2' => 'value2'};
push @array, {'key1' => 'value1', 'key2' => 'value2'};
print Dumper(@array);
Will give you:
$VAR1 = {
'key2' => 'value2',
'key1' => 'value1'
};
$VAR2 = {
'key2' => 'value2',
'key1' => 'value1'
};
$VAR3 = {
'key2' => 'value2',
'key1' => 'value1'
};
Popularity: 2% [?]
Finding a hostheader in IIS using Perl and Win32::OLE
There is no easy way to find a host header in IIS. Unless you think clicking on each site is easy. I have found it difficult to find them in large installations. I wrote a script that will go through each site on the server and look at the host headers and then if found it will print the Site comment. You then use the site comment to find the main site in the MMC.
Here is the script.
Popularity: 2% [?]
Changing all directory paths in Microsoft IIS/Active Directory using perl and WIN32::OLE
I was put in charge of a filer migration from a old netapp f810 to a new netapp 3040 cluster. One of the systems we have that used the netapp backend was a web cluster running MS Windows 2003. When it was first built it was using a cifs shares by system name (ie: \\f810.
Bulk IIS path change script: Link
Bulk Active Directory change script: Link
Popularity: 2% [?]
Making your own ringtones for your Iphone (mp3 to m4r)
I have done much searching on the internet on how to convert mp3 files to Itunes m4r files. I really dont like using Itunes if I dont have to so I thought I would write something that will use my unix box.
This perl script will convert most mp3s to m4r.
I used 4 applications that can be installed on any GNU based system.
- mp3info - An MP3 technical info viewer and ID3 1.x tag editor
- mp3splt - Mp3Splt-project is a utility to split mp3 and ogg files selecting a begin and an end time position, without decoding.
- Mplayer
- Faac - FAAC is an open source MPEG-4 and MPEG-2 AAC encoder, it is licensed under the LGPL license.
or if you are running debian/ubuntu:
Popularity: 18% [?]
Nothing motivates like having another programmer tell you they're rewriting your code because it sucks.
Popularity: 1% [?]