Perl – Code Execution Time with accuracy – Time::HiRes How long does it take to execute some code? use Time::HiRes qw( time ); my $start = time(); // Code goes here sleep 10; my $end = time(); print “SPRINTF: “; printf(“%.6f\n”, $end – $start);
PERL – Bitwise OR XOR an array [perl] my @data= (“80″,”80″,”7B”,”30″,”32″,”30″,”38″,”31″,”46″,”30″,”39″); my $chk; foreach my $point(@data) { $chk = $chk^$point; ## XOR each array } $chk = hex($chk)|hex(“80”); ## OR the result value my $chk_hex=sprintf(“%x”,$chk); ## convert result to hex print “$chk = $chk_hex\n”; ## display both results side by side [/perl]
SFTP Only for a user – Debian Add the user info the sshd_config file in /etc/ssh/sshd_config Ensure the line is uncommented; Subsystem sftp /usr/lib/openssh/sftp-server Match User userbob ForceCommand internal-sftp ChrootDirectory /home/userbob X11Forwarding no AllowTcpForwarding no Ensure the home directory is not group writeable. It should be 755. /etc/init.d/ssh restart service ssh restart Test the […]
Perl DBH get last id SIMPLES with DBH Mysql my $sth = $dbh->do(“$SQL”); my $id = $dbh->{‘mysql_insertid’};
Perl Hex to Binary to Decimal Conversions Dec to Hex Use function sprintf(“%x”, DECNUMBER) or sprintf(“%X”, DECNUMBER) for hexadecimal numbers with capital letters. $decvalue = 200; $hexvalue = sprintf(“%x”, $decvalue); ## gives c8 $hexvalue = sprintf(“%X”, $decvalue); ## gives C8 Dec to Oct Use sprintf(“%o”, DECNUMBER ) $decvalue = 200; $octval = sprintf( “%o”, $decvalue […]
PERL XOR 2 HEx values Need to XOR to hex values… try this. $NEW_VAL = hex(80) ^ hex(9D);
Map Hex Array to Ascii Where @values are an array of hex values my @ascii_vals = map (chr, map(hex,@values));
Convert IP to HEX in Perl – one liner Strip out the IP.. $IP=”192168016012″; $IP = sprintf “%*vX”, “:”, $IP; Vector hex conversion of IP
Add preceding zero to perl IP string ### Add preceding zero to IP address so that the address is 192.058, not 192.58 $IP=~s/(\d+)/substr”00$1″,-3/eg;
UNIX Perl – Get local IP with ifconfig Replace ETH0 with the interface you need.. my $IP = `ifconfig eth0| awk ‘/dr:/{gsub(/.*:/,””,\$2);print\$2}’`; $IP =~ s/\s+$//;