Perl

Perl – Trim Text, Trim String

Perl – Trim Text, Trim String Left trim- ltrim or lstrip removes white spaces from the left side of a string: [perl] $str =~ s/^\s+//; [/perl] Right trim – rtrim or rstrip removes white spaces from the right side of a string: [perl] $str =~ s/\s+$//; [/perl] Trim Both [perl] $str =~ s/^\s+|\s+$//g [/perl] [perl] […]

Perl

Recursively Expand a NIS Netgroup

Recursively Expand a NIS Netgroup @results=expand_ng(); # Given a netgroup, will return a list of members. Will recurse # netgroups if the $recurse flag is set sub expand_ng { my($netgroup)=@_; my(@returnng); my($ng)=`ypmatch $netgroup netgroup`; $? != 0 and return; #Don’t continue for duff netgroups my($value); $ng =~ s/\s+/ /g; #Compress whitespace $ng =~ s/\s+,/,/g; #Remove […]

Perl UNIX

Add a timestamp to every STDOUT line

Add a timestamp to every STDOUT line I found this useful for debugging and just handy to have in the toolbox. Alias within your profile! Use: ./anyscript_you_choose | ./addtime [perl] #!/bin/perl -w # filename: ./addtime my($old)=select(STDOUT); $|=1; select(STDIN); $|=1; select ($old); while(<>) { my($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)= localtime (time); printf “%02d:%02d:%4d %02d:%02d:%02d %s”,$mday,$mon+1,$year+1900,$hour,$min,$sec,$_; } [/perl]

Perl UNIX

Perl – Get the md5 hash for a file

Perl – Get the md5 hash for a file Get the MD5 hash for a file on the command line, using Digest::MD5 usage: ./md5sum.pl [filename] [-f] -f do not display filename [perl] use warnings; use strict; use Digest::MD5; sub md5sum { my $file = shift; my $digest = “”; eval{ open(FILE, $file) or die “Can’t […]

Perl

Get HTTP Code Response – Perl

Get HTTP Code Response – Perl Pass the script many hosts and obtain the HTTP response codes for the pages. [bash] ./http_check.pl … [/bash] [perl] use LWP::UserAgent; print get_url($url); sub get_url ($) { my $url = shift; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req); return $res->code; } ##Some […]