epoch generation and converting

Problem

You want to generate the current epoch offset for UNIX. The count forward since 8am on 1st Jan 1970.

Or conversely you want to see the date and time, for a given epoch offset.



Solution

You can use perl’s mktime or localtime – or just use my tools below.



Example


Either enter epoch:

Or enter the date (plus time)

daymonthyear
hrminssecs

epoch offset or date will appear here



Reference

[tags]epoch, unix, 2038[/tags]

  1. techno park epoch year 2038 problem


libcurl lookup taking long time

Problem

You notice for some hosts curl and/or libcurl are taking 7 or so secs to lookup the host. But nslookup or dig respond almost immediately.



Solution

Try doing curl -4 -w “time_namelookup: %{time_namelookup}n” …. and try without the -4. If you see drastic differences, you could be falling foul of the IPV6 bug. To work around this, you can either just use -4 with curl or set CURLOPT_IPRESOLVE with libcurl.



Example


I spents hours and hours and hours, trying to get this working with libcurl and perl. It turned out I need to upgrade to WWW::Curl::Easy version 3.0 – as opposed to version 2. Do a search on search.cpan.org for Curl and it will tell you how to check your version.

Then if you like me upgrade, you need to also modify all your code from: Curl::easy::setopt($curl, … to $curl->setopt( …



Reference



HTTP Basic Auth with HTTP headers and libcurl

Problem

Came across an interesting problem with curl.

For curl to perform HTTP Basic Authentication, it is easy to pass –user to the curl command, but harder with libcurl.

Suspect there is an attribute that can be set, but I monitor a multitude of web sites through some perl scripts and libcurl. I did n’t want to have to modify my wrapper scripts, which mesh perl hashes with the code that drives curl (via libcurl).

I do allow for headers though, having needed to pass different things through, like HTTP_REFERER, LAST_MODIFIED, etc.

Therefore I just needed to pass the HTTP BASIC Authentication through as a header.



Solution

First off you need to base64 encode the user and password.

Use Google’s tool



Example


We then strip out the equals and pass following through to curl or libcurl:

@myheaders=('Authorization: Basic YWRtaW46YWRtaW4');Curl::easy::setopt($curl, Curl::easy::CURLOPT_HTTPHEADER, @myheaders);

Or from the command line:

curl ... -H'Authorization: Basic YWRtaW46YWRtaW4'

Obviously you need to put your user, followed by a colon and your password – to obtain the correct base64 encoding back – strip the equals out and away you go.



Reference



Useful Perl script – convert text to ascii

Problem

You want to convert text to ascii values.



Solution

Perl script to convert standard input to ascii values, useful with web code.

You can take the output and insert directly into HTML pages.

#!/usr/bin/perlwhile(sysread(STDIN,$a,1)) {$c=ord($a);if($c==10) { print "n"; } else { print "&#$c;"; }}exit(0);__END__


Example


Here is a screen shot:

convert characters to ascii values in perl



Reference