This program is used to transfer a file to a server and is written in perl/Tk. The idea of the file is to enter all the information and then hit the connect button. The button then says Stop which is suppost to end the sftp subroutine. The program then creates a thread and connects to the server and transfers the file repeatedly until the stop button is clicked. What I am having trouble in doing is once you click "Connect" the file should keep transfering until the user clicks the 'Stop" button. As of now the file only transfers once. If anyone has ideas on how to do this I would be eternally greatful.
#!/usr/bin/perl -w use strict; use warnings; use Tk; use Net::SFTP::Foreign; use threads; use threads::shared; my $user = 'jlahowet'; my $server = 'mistral.unl.edu'; my $lcl = '/home/deadpickle/Desktop/Virtual Cockpit/'; my $rmt = '/home/jlahowet/UAV/COORDS/'; my$uav = 'uavposition'; my $mw = MainWindow->new(); my $go = 0; my $monitor: shared; $mw->configure(-title => 'VIRTUAL COCKPIT:SFTP', -background => 'blue' +); $mw->geometry('+400+300'); #User entry my $usr = $mw->Frame(-relief => 'groove', -borderwidth => 3, -backgrou +nd => 'blue')->pack(-side => 'top', -fill => 'x'); my $usrlbl = $usr->Label(-text => 'Username', -background => 'blue', - +foreground => 'white')->pack(-side => 'left'); my $usreny = $usr->Entry(-width=> 10, -textvariable => \$user)->pack(- +side => 'left', -pady => 3); #Server entry #my $svr = $mw->Frame(-relief => 'groove', -borderwidth => 3, -backgro +und => 'blue')->pack(-side => 'top', -fill => 'x'); my $svrlbl = $usr->Label(-text => 'Server', -background => 'blue', -fo +reground => 'white')->pack(-side => 'left'); my $svreny = $usr->Entry(-width=> 15, -textvariable => \$server)->pack +(-side => 'left', -pady => 3); #File locations my $loc = $mw->Frame(-relief => 'groove', -borderwidth => 3, -backgrou +nd => 'blue')->pack(-side => 'top', -fill => 'x'); my $uavlbl = $loc->Label(-text => 'UAV: ', -background => 'blue', -fo +reground => 'white')->pack(-side => 'left'); my $filelbl = $loc->Label(-text => 'File', -background => 'blue', -for +eground => 'white')->pack(-side => 'left'); my $fileeny = $loc->Entry(-width=> 8, -textvariable => \$uav)->pack(-s +ide => 'left', -pady => 3); my $lcllbl = $loc->Label(-text => 'Local', -background => 'blue', -for +eground => 'white')->pack(-side => 'left'); my $lcleny = $loc->Entry(-width=> 25, -textvariable => \$lcl)->pack(-s +ide => 'left', -pady => 3); my $rmtlbl = $loc->Label(-text => 'Remote', -background => 'blue', -fo +reground => 'white')->pack(-side => 'left'); my $rmteny = $loc->Entry(-width=> 25, -textvariable => \$rmt)->pack(-s +ide => 'left', -pady => 3); #Connect button my @setup = ($user, $server, $lcl, $rmt, $uav); my $btn = $mw->Button(-text => 'Connect', -background => 'gray', -comm +and => \&sub1)->pack(-side => 'bottom', -padx => 3, -pady => 3, -anch +or => 'e'); MainLoop; sub sub1 { if($btn->cget(-text) eq 'Connect'){ $go = 1; $btn->configure(-text=> 'Stop'); my $thr = threads->new(\&sftp, @setup); $monitor = 0; } else{ $go = 0; $btn->configure(-text=> 'Connect'); $monitor = 1; } print "$go\n"; } sub sftp { my $host = $_[1]; my $usrname = $_[0]; my $port = 22; my $seconds = 20; my %args = (user=>$usrname, port=>$port, timeout=>$seconds); my $local = $_[2]; my $remote = $_[3]; my $uavfile = $_[4]; my $sftp = Net::SFTP::Foreign->new($host, %args); print $monitor; eval { $sftp->put("$local$uavfile", "$remote$uavfile", callback => sub { die "aborted" if $monitor; # $monitor +has to be set from the other thread }) }; if ($@ and $@ =~ /^aborted/) { undef $sftp; print "Stopped"; $monitor = 0; } sleep (2); exit(1); }

In reply to Looping File transfer using SFTP in TK by deadpickle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.