Skip to content

Instantly share code, notes, and snippets.

@earnhardt3rd
Created June 19, 2023 14:03
Show Gist options
  • Select an option

  • Save earnhardt3rd/68c8a3ba4eaf57741adeb154d21a0b69 to your computer and use it in GitHub Desktop.

Select an option

Save earnhardt3rd/68c8a3ba4eaf57741adeb154d21a0b69 to your computer and use it in GitHub Desktop.
require 'getopts.pl';
Getopts('m:d:t');
my $MODE = uc($opt_m) || "";
my $DEBUG = $opt_d || 0;
if (defined $opt_t) { my %TEST = &_ROBO();}
1;
sub _ROBO {
my $src = shift || "";
my $usr = shift || "";
my $pas = shift || "";
my $svr = shift || "";
my $drv = shift || "";
if (lc($^O) =~ /win/) {
if (exists($OS_TOOLS{"dos83"})) {
if (-f $OS_TOOLS{"dos83"}) {
my $aLongCmd = $OS_TOOLS{"dos83"} . " -l \"$src\"";
my $aLongCmdX = `$aLongCmd`;
chomp($aLongCmdX);
print " $aLongCmd\n" if $DEBUG > 1;
print " $aLongCmdX:\n" if $DEBUG > 1;
if (-f $aLongCmdX) {
$src=$aLongCmdX;
} else {
print " WARNING! Unable to find Long Name Conversion\n";
print " ORG:$src\n";
print " NEW:$aLongCmdX\n";
}
} else {
print " WARNING!!! Could not find OS_TOOLS{dos83} as " . $OS_TOOLS{"dos83"} . "\n";
}
} else {
print " WARNING!!! OS_TOOLS MISSING CONFIG FOR 'dos83'\n";
}
}
print " _ROBO_SRC:$src\n" if $DEBUG > 0;
print " _ROBO_USR:$usr\n" if $DEBUG > 0;
print " _ROBO_PAS:$pas\n" if $DEBUG > 0;
print " _ROBO_SVR:$svr\n" if $DEBUG > 0;
print " _ROBO_DRV:$drv\n" if $DEBUG > 0;
if ($drv eq "") {
print " _ROBO:Did not receive Drive, Trying E:\n";
print " _ROBO:If this fails, contact Frank Earnhardt\n";
$drv = "e";
}
my $srcDrv = uc(substr($src,0,1));
if (lc($srcDrv) ne lc($drv)) {
print " _ROBO:[$HOST] Source Drive ($srcDrv) <> [$svr] Target Drive ($drv)\n";
my $chgDrv="Yes";
if (uc($MODE) ne "CRON") {
$chgDrv = &Ask("Do you want to change Target to $srcDrv","No","Yes");
}
if ($chgDrv eq "Yes") {
print " _ROBO:[$svr]Target drive changed to $srcDrv: !\n";
$drv=$srcDrv;
} else {
print " _ROBO:[$svr]Target drive unchanged.\n";
}
}
my $roboLog = $TMP . $SLASH . ".robo-${svr}_${DT}_${TM}.log";
print " --DEBUG_R00:roboLog:=$roboLog\n";
my $robo="robocopy /r:1 /w:1 /TEE /V ";
my %ROBO_ERRORS;
my %ROBO_EXEC;
print " USER_ACCESS=$USER_ACCESS\n" if $DEBUG > 0;
my $freeDrv = &_GetFreeDrive();
my $net_drv = "\\\\${svr}\\${drv}\$";
my $net_useP = "net use ${freeDrv}: $net_drv";
my $net_useMsg = $net_useP;
my $paFlag="Y";
if ($USER_ACCESS =~ /PUSH_ANONYMOUS/ or $USER_ACCESS =~ /ADMIN/) {
if (uc($MODE) ne "CRON") {
if ($USER_ACCESS =~ /ADMIN/) {
my $paq = "Do you want to push as USER:$usr";
$paq.= " $pas" if $DEBUG > 1;
my $pa = &Ask($paq,"No","Yes");
if (uc($pa) eq "NO") {
$paFlag="N";
}
}
}
if ($paFlag eq "Y") {
$net_useP.= " /user:${svr}\\${usr} ${pas}";
$net_useMsg.= " /user:${svr}\\${usr}";
}
}
if ($USER_ACCESS =~ /ADMIN/) {
if ($DEBUG > 1) {
print " _ROBO:$net_useP\n";
} else {
print " _ROBO:$net_useMsg\n";
}
} else {
print " _ROBO:$net_useMsg\n";
}
my $reCount=0;
NET_USE_RETRY:
print " _ROBO(net_use):${net_useP}:\n" if $DEBUG > 1;
my $netU = `$net_useP 2>&1`;
print " _ROBO(net_use):${netU}:\n" if $DEBUG > 1;
my $keepMDFlag=0;
if ($netU =~ /Multiple connections/ or $netU =~ /The local device name is already in use/) {
print " Unable to establish another network connection to same server($svr)!\n";
my $mapped = `net use|findstr $svr`;
chomp($mapped);
my $mappedDrive = $mapped;
$mappedDrive =~ s/\:.*//g;
$mappedDrive =~ s/^.* //g;
print " MAPPED($mappedDrive):$mapped\n";
my $delMap = &Ask("Should I remove exising mapping","Yes","No");
if ($delMap eq "Yes") {
my $rmMapDriveCmd = "net use $mappedDrive: /DEL";
print " $rmMapDriveCmd\n";
my $rmMapDriveCmdX = `$rmMapDriveCmd 2>&1`;
print " $rmMapDriveCmdX\n";
&_SLEEPER(5);
$reCount++;
if ($reCount <= 3) {
goto NET_USE_RETRY;
} else {
print " _ROBO:ERR: Unable to make connection!\n";
return;
}
} else {
$keepMDFlag++;
$freeDrv=$mappedDrive;
}
} else {
if ($netU =~ /The network path was not found/) {
my $SH = uc($svr);
$SH =~ s/\..*//g;
my $IP_FILTER = "INT,CSV,NOHEADER,HOST=${SH},IP";
print " _ROBO:IP_FILTER:$IP_FILTER\n" if $DEBUG > 1;
my @IP = &_SHOW_HASH_AS_ARRAY($IP_FILTER,%SERVERS);
if (scalar(@IP) > 0) {
if (scalar(@IP) == 1) {
my $ip = $IP[0];
if ($ip ne "") {
print " _ROBO:ERR:$netU\n";
print " _ROBO:Let's try by IP:$ip\n";
$net_useP = "net use ${freeDrv}: \\\\${ip}\\${drv}\$";
if ($paFlag eq "Y") {
$net_useP.= " /user:${svr}\\${usr} ${pas}";
}
print " _ROBO:$net_useP\n" if $DEBUG > 1;
&_SLEEPER(5);
$reCount++;
if ($reCount <= 3) {
goto NET_USE_RETRY;
} else {
print " _ROBO:ERR: Unable to make connection!\n";
return;
}
}
} else {
print " _ROBO:UNABLE TO DETERMINE DISTINCT IP ADDRESS FOR $SH WITH $IP_FILTER\n";
for (my $ii=0;$ii<=scalar(@IP)-1 ;$ii++) {
print " _ROBO:$ii $IP[$ii]\n";
}
}
} else {
print " _ROBO:UNABLE TO DETERMINE IP ADDRESS FOR $SH WITH $IP_FILTER\n";
}
} else {
print " _ROBO:$netU\n";
}
}
print " _ROBO:=====================\n";
if (-f $src) {
my $folder = &Parent($src);
print " _ROBO:FOLDER=$folder\n";
#$roboLog = $folder . $SLASH . $roboLog;
my $unc = $folder;
$unc =~ s/.*://g;
$unc = "${freeDrv}:" . $unc;
my $file = &File($src);
$robo.= " \"${folder}\" \"${unc}\" \"${file}\" /LOG:\"$roboLog\"";
}
if (-d $src) {
#$roboLog = $src . $SLASH . $roboLog;
my $unc = $src;
$unc =~ s/.*://g;
$unc = "${freeDrv}:" . $unc;
$robo.= " /E \"${src}\" \"${unc}\" /LOG:\"$roboLog\"";
}
print " DEBUG:_ROBO:$robo\n";
system($robo);
if (-f $roboLog) {
print "\n *****\n";
print " INFO! See Log: $roboLog\n";
print " *****\n";
} else {
print "\n *****\n";
print " WARN! Unable to locate LOG ($roboLog)\n";
print " *****\n";
}
if ($keepMDFlag == 0) {
my $net_delP = "net use ${freeDrv}: /DEL";
print " DEBUG:_ROBO:$net_delP\n";
my $netUX = `$net_delP 2>&1`;
print " DEBUG:_ROBO:$netU\n";
}
return $roboLog;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment