Terminal-Identify
view release on metacpan or search on metacpan
lib/Terminal/Identify.pm view on Meta::CPAN
my ($shell) = $line =~ /^\/.*\/(.*)$/;
# Add the login shell to the array.
push(@login_shells, $shell);
};
};
# Remove the double entries from the array.
my %login_hash = map({$_, 1} @login_shells);
@login_shells = keys %login_hash;
# Return the array with the unique login shells.
return @login_shells;
};
# ============================================================================ #
# Subroutine get_ppid #
# #
# Description: #
# Determine the PPID of the calling Perl script using the Linux command ps. #
# #
# @argument: $_[0] => $pid PID (scalar) #
# @return: $ppid PPID (scalar) #
# ============================================================================ #
sub get_ppid {
# Assign the subroutine argument to the local variable $pid.
my $pid = $_[0];
# Store the output of the Linux command ps in the local variable $ppid.
my $ppid = `ps --no-headers -o ppid:1 -p $pid --sort lstart 2>/dev/null`;
# Split a multiline Perl scalar with the PPID's in parts.
my @parts = split /\s+/, $ppid;
# If there is more than one PPID, use the first PPID.
$ppid = $parts[0];
# Trim the Perl scalar with the PPID.
$ppid = trim($ppid);
# Return the PPID.
return $ppid;
};
# ============================================================================ #
# Subroutine get_termpath #
# ============================================================================ #
sub get_termpath {
# Initialise the local variables.
my $fileno = fileno(STDIN);
my $term_path = "";
# Get the terminal path.
# $term_path = TermPath();
$term_path = ttyname($fileno);
# Check the terminal path
$term_path = (defined $term_path ? $term_path : "");
# Return the terminal path.
return $term_path;
};
# ============================================================================ #
# Subroutine get_user_name #
# ============================================================================ #
sub get_username {
# Initialise the username.
my $username = "";
# Define the methods for getting the username.
my $method1 = getlogin();
my $method2 = (getpwuid($<))[0];
my $method3 = $ENV{LOGNAME};
my $method4 = $ENV{USER};
# Extract the username.
$username = ($method1 || $method2 || $method3 || $method4);
# Return the username.
return $username;
};
# ============================================================================ #
# Subroutine term_user_shell #
# #
# Description: #
# Create a multi dimensional array with term, user and shell. #
# #
# @arguments: $_[0] => $passwd_content Content of passwd (scalar) #
# @{$_[1]} => @user_array User array (array) #
# @{$_[2]} => @shell_array Shell array (array) #
# @return: @result_array Array with the result (array) #
# ============================================================================ #
sub term_user_shell {
# Assign the subroutine arguments to the local variables.
my $passwd_content = $_[0];
my @user_array = @{$_[1]};
my @shell_array = @{$_[2]};
# Initialise variable $term.
my $term = "";
# Get the username related to the terminal.
my $username = get_username();
# Declare the return array.
my @result_array = ();
# Get the terminal path.
my $term_path = get_termpath();
# If $term_path is not defined or empty set $term to ?.
if (!defined $term_path || $term_path eq "") {
# Set variable $term_path.
$term = "?";
} else {
# Check on pts and tty.
if ($term_path =~ /^.*\/dev\/(pts\/\d+)$/) {
# Extract the pseudo terminal slave.
($term) = $term_path =~ /^.*\/dev\/(pts\/\d+)$/;
} elsif ($term_path =~ /^.*\/dev\/(tty\d+)$/) {
# Extract the terminal TeleTYpewriter.
($term) = $term_path =~ /^.*\/dev\/(tty\d+)$/;
} else {
$term = "?";
};
};
# Loop over the array with the lines of $passwd_content.
foreach (split '\n', $passwd_content) {
# Get user and shell from each line of the content.
my ($user) = $_ =~ /^(.*?):.*/;
my ($shell) = $_ =~ /^.*\/(.*)$/;
if ($shell ne "nologin" && $shell ne "sync" && $shell ne "false") {
# Check user and shell against the given arrays.
if (grep(/^$user$/, @user_array) &&
grep(/^$shell$/, @shell_array)) {
# Assemble a new list.
my @tmp = ($term, $user, $shell);
# Add data to array.
( run in 1.690 second using v1.01-cache-2.11-cpan-39bf76dae61 )