Search code examples
perlsamba

Parse server name from UNC path using Perl


I'm looking for a way in Perl to get the server name from a UNC path when it is in either of the following formats:

$unc = '//server.domain.com/share';

$unc = '\\\\server.domain.com\\share';


Solution

  • for my $unc ('//server.domain.com/share', '\\\\server.domain.com\\share') {
        my ($server) = $unc =~ m{([^/\\]+)};
        print $server, "\n";
    }
    

    output

    server.domain.com
    server.domain.com