Skip to content

Instantly share code, notes, and snippets.

View rsperl's full-sized avatar

Richard rsperl

  • North Carolina, United States
View GitHub Profile
@rsperl
rsperl / alias.pl
Last active July 25, 2022 13:25
alias a package name #perl #snippet
#!/usr/bin/env perl
BEGIN {
# alias all references to the package to A
*A:: = \*ThisIsAVeryLongPackageName::;
}
package ThisIsAVeryLongPackageName;
sub spam {
@rsperl
rsperl / bind_to_ldap.pl
Last active August 4, 2021 20:02
bind to ldap #perl #ldap
#!/usr/bin/env perl
use Net::LDAP;
my $user = $ENV{LDAP_USER};
my $pass = $ENV{LDAP_PASS};
my $host = $ENV{LDAP_HOST};
my $ldap = Net::LDAP->new($host);
die "can't connect to $host $!" unless $ldap;
@rsperl
rsperl / capture_stdout_stderr.pl
Last active August 4, 2021 20:02
capture stderr and stdout #perl
#!/usr/bin/env perl
use strict;
use Capture::Tiny qw(capture);
my ($stdout, $stderr, $exitcode) = capture {
system("ls", "/", "/nosuchdir");
};
print "STDOUT: $stdout\n";
@rsperl
rsperl / interactive.pl
Last active August 4, 2021 20:02
check for an interactive session #perl
#!/usr/bin/env perl
# Checking for STDIN in Perl
# Use -t to test STDIN and STDOUT:
sub i_am_interactive { return -t STDIN && -t STDOUT;}
@rsperl
rsperl / clone_ds.pl
Last active August 4, 2021 20:02
clone a data structure #perl
#!/usr/bin/env perl
use strict;
use Test::More;
use Storable qw(dclone);
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
@rsperl
rsperl / count_occurrences.pl
Last active August 4, 2021 20:02
counting the number of occurrences of a string inside another string #perl
#!/usr/bin/env perl
use strict;
use Data::Dumper;
use Test::More;
diag "\n>>> Run as 'prove -v $0'<<<\n";
my $doc =<<EOF;
@rsperl
rsperl / create_cpan_file.pl
Last active August 4, 2021 20:02
create cpan file using latest version of modoules #perl
#!/usr/bin/env perl
use CPAN;
my $file = shift @ARGV;
open my $fh, $file or die "Can't open $file: $!\n";
my @modules = <$fh>;
chomp @modules;
close $fh;
my $n = scalar @modules;
@rsperl
rsperl / get_ldap_connection.pl
Last active August 4, 2021 20:01
get ldap connection base #perl #ldap
#!/usr/bin/env perl
# Getting the base from an ActiveDirectory connection
use Net::LDAP;
my $ldap = ...; # connect to ldap
my $dse = $ldap->root_dse();
my @contexts = $dse->get_value('namingContexts');
my $base;
@rsperl
rsperl / list_installed_modules.pl
Last active August 4, 2021 20:01
list installed modules #perl
#!/usr/bin/env perl
#
# You can also do this (but you don't get the versions):
#
# perl -MExtUtils::Installed -e '$e = ExtUtils::Installed->new(); print join("\n", $e->modules())'
#
use strict;
@rsperl
rsperl / netcat_check_udp.sh
Last active July 25, 2022 16:16
use netcat to see if a remote UDP port is open #networking #shell #snippet
#!/bin/sh
# check if a upd port is open with netcat
nc -v -u -z -w 3 localhost 5000
# send a test packet
# -n - Tells the echo command to not output the trailing newline.
# -4u Use IPV4 addresses only. Use UDP instead of TCP.
# -w1 Silently close the session after 1 second of idle time. That way, we’re not stuck waiting for more data.
echo -n “foo” | nc -4u -w1 <host> <udp port>