I recently needed to identify the remote host of the user who is running a script. I knew w could tell me this for all users, so I ran an strace to see where it was getting the info. That led me to the utmp file (caveat: I'm running on Linux, and this script will stay there. POSIX supplies the utmpx spec, but I just went with what works here). Well, I had dabbled in Inline::C to try to outdo the builtin substr (couldn't!), so I thought, "Let's try it."

#!/usr/bin/perl use strict; use warnings; use Inline C => 'EOC'; #include<utmp.h> /* Inline will export this to Perl. * Takes a scalar, returns a scalar. */ SV* hostordisplay( SV* sv_tty ) { /* Good C, declare variables up front */ /* utmp.h deals with struct utmp, which holds * the info we want. We need a pointer to hold * the info we get out, and a real struct to * specify the info to match against. */ struct utmp *up; struct utmp ut; char * tty; /* grab the string (char *) version of our scalar */ tty = SvPV_nolen( sv_tty ); /* and put it into our matching struct utmp */ strncpy(ut.ut_line, tty, UT_LINESIZE); /* open the utmp file at the beginning */ setutent(); /* and get a pointer to matching struct utmp */ up = getutline( &ut ); /* gotta check if it succeeded */ if ( up == NULL ) { /* return undef if it failed */ return &PL_sv_undef; } /* close the utmp file again */ endutent(); /* return a new scalar with the value of * of the remote host or the display (on my * system). Using 0 for length param makes perl * use strlen to find the length. */ return newSVpv( up->ut_host, 0 ); } (my $tty = readlink('/proc/self/fd/0') || 'notty') =~ s{^/dev/}{}; print "You are logged in from ", hostordisplay($tty) || 'nowhere', "\n";

I thought it was cool, and I hadn't seen Sys::Utmp, which I'm sure works better, but I was constrained to not install another module. I just hope someone can learn something from my experience.

print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

In reply to Get remote host of login shell with Inline::C by bv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.