
ogit2
Novice
Mar 16, 2018, 3:42 AM
Post #4 of 7
(1248 views)
|
SFTP in Perl workaround. Please note that this IS a workaround (DRAFT version) but it works! (Step 4). Also note that for public/private keys we used Putty to generate. STEP1. Install WINSCP https://winscp.net/eng/index.php STEP2. Register the COM components as they may not register.
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb STEP 3. Run WinSCP, set up SFTP account and test. Make sure it works. Once it works, click on manage and generate session/url code. The data here will be required in Perl. This is important if you are a basic user. STEP 4. See example Perl code below. Feel free if you are clever to modify and make it better for other users.
#!C:\Perl\bin\perl.exe use File::Copy; use Time::Local; use Win32::OLE; use Win32::OLE::Const; use Win32::OLE::Variant; Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE); my $session = Win32::OLE->new('WinSCP.Session'); my $consts = Win32::OLE::Const->Load($session); my $sessionOptions = Win32::OLE->new('WinSCP.SessionOptions'); my $host = 'FTPSITE'; my $user = 'FTPUSER'; my $privateKey = "PATH_AND_PRIVATE_KEY_NAME"; my $file = "test.txt"; my $passphrase = ""; my $put = 0; my $get = 1; my $proc = $$; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; $mon = $mon + 1; $mon = trim0($mon); $mday = trim0($mday); $hour = trim0($hour); $min = trim0($min); $sec = trim0($sec); my $todaydate = "$year$mon$mday\_$hour$min$sec_"; $sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'}; $sessionOptions->{'HostName'} = $host; $sessionOptions->{'UserName'} = $user; $sessionOptions->{'SshPrivateKeyPath'} = $privateKey; $sessionOptions->{'PrivateKeyPassphrase'} = 'PRIVATEKEY'; $sessionOptions->{'SshHostKeyFingerprint'} = 'FINGERPRINT'; # Connect $session->Open($sessionOptions); # Upload files my $transferOptions = Win32::OLE->new('WinSCP.TransferOptions'); $transferOptions->{'TransferMode'} = $consts->{'TransferMode_Ascii'}; my $cfiledir="WORKDIRECTORY"; print "$cfiledir \n"; chdir($cfiledir) or die "Cant chdir to $path $!"; #START: PUT FILES if ($put == 1) { my $transferResult = $session->PutFiles('test.txt', '/Test/Out/', FALSE, $transferOptions); # Throw on any error $transferResult->Check(); # Print results my $items = Win32::OLE::Enum->new($transferResult->{'Transfers'}); my $item; while (defined($item = $items->Next)) { print $item->{'FileName'} . "\n"; } } #END: PUT FILES #START: GET FILES print "Start: Getting Files from SFTP \n"; if ($get == 1) { #my $transferResult = $session->FileExists(string path); my $transferResult = $session->GetFiles('/Test/Out/',$cfiledir, FALSE, $transferOptions); # Throw on any error $transferResult->Check(); # Print results my $items = Win32::OLE::Enum->new($transferResult->{'Transfers'}); my $item; while (defined($item = $items->Next)) { my $filename = $item->{'FileName'}; print "File Received: " . $filename . "\n"; # START: Change received filename with datestamp my $filename2 = $filename; $filename2 =~s/.*\///; my $filename3 = $todaydate . "_" . $filename2; move($filename2,$filename3); # END : Change received filename with datestamp my $removalResult = $session->RemoveFiles($filename); } } print "End: Getting Files from SFTP \n"; #END: GET FILES exit; sub trim0 { my ($trims) = @_; $trims =~ s/^\s+//; $trims =~ s/\s+$//; $myLength = length($trims); if ($myLength == 1) { $trims = "0$trims"; } return $trims; }
(This post was edited by ogit2 on Mar 16, 2018, 5:01 AM)
|