Thread: Re: [Ssh-sftp-perl-users] errors :(
Brought to you by:
dbrobins
From: Seth R. <SRO...@mo...> - 2004-11-23 21:51:51
|
Mark, >>> "Mark Fuller" <mar...@ea...> 11/20/2004 12:01:23 PM >>> >>>If you decide to use Expect.pm let me know and I can carve out an example >>>from what I'm doing. It's really easy. Actually, if it's not much trouble, I would appreciate it. I got perl-sftp to compile and run on some of my systems (Sparcs running Solaris 2.6 and 8). (It runs on one, after updating gcc, perl, and many cpan updates, etc. Doing the same on other systems did not yield good compiles....) The above effort was tabled due to a deadline, so I threw together a script to do the following: list files in local dir sftp + list files in remote build a command file to transfer the missing files check that all the transferred files made it delete those that succeeded All this using the system function and sftp that comes with openssh, I have not looked at expect at all. The above gets run repeatedly in a cron tab. (and I have code to do the opposite, pull down from sftp server). |
From: thefinn <th...@tp...> - 2004-11-23 22:28:06
|
Yeah really odd, I seem to have got it to work on x86 no problems. I think it must have something to do with my version of gmp. It is aurora linux 1.92. Very odd. I now get the "permission denied" and a complete exit of the program when the user/pass pairs are wrong. I'd like to change this to just have it give me some kind of error code. TF On Wednesday 24 November 2004 08:47, Seth Rothenberg wrote: > Mark, > > >>> "Mark Fuller" <mar...@ea...> 11/20/2004 12:01:23 PM > >>> > >>>If you decide to use Expect.pm let me know and I can carve out an > > example > > >>>from what I'm doing. It's really easy. > > Actually, if it's not much trouble, I would appreciate it. > > I got perl-sftp to compile and run on some of my systems (Sparcs > running Solaris 2.6 and 8). > (It runs on one, after updating gcc, perl, and many cpan updates, etc. > Doing the same > on other systems did not yield good compiles....) > > The above effort was tabled due to a deadline, so > I threw together a script to do the following: > list files in local dir > sftp + list files in remote > build a command file to transfer the missing files > check that all the transferred files made it > delete those that succeeded > > All this using the system function and sftp that comes with openssh, > I have not looked at expect at all. > > The above gets run repeatedly in a cron tab. > > (and I have code to do the opposite, pull down from sftp server). > > > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://productguide.itmanagersjournal.com/ > _______________________________________________ > Ssh-sftp-perl-users mailing list > Ssh...@li... > https://lists.sourceforge.net/lists/listinfo/ssh-sftp-perl-users -- http://ghettoshell.net |
From: Mark F. <mar...@ea...> - 2004-11-24 00:15:48
|
> I now get the "permission denied" and a complete exit of the program when the > user/pass pairs are wrong. I'd like to change this to just have it give me > some kind of error code. I think you need to surround your method call within an eval block and catch the die. I posted something to this list last February about how I worked around this. It involves eval and capturing SIG{__WARN__}. It seemed like multiple warnings could occur before a die. So, catching that signal let me accumulate them before the die. The example I posted uses "do_realpath." I don't think it actually dies. But, some other things like connecting and logging in do. I got in the habit of wrapping everything this way. See: http://sourceforge.net/mailarchive/message.php?msg_id=7114680 Seth, I'll post the Expect.pm example later. Mark |
From: Mihai S. <mih...@de...> - 2004-11-24 15:20:49
|
On Wednesday 24 November 2004 00:27, thefinn wrote: > I now get the "permission denied" and a complete exit of the program when > the user/pass pairs are wrong. I'd like to change this to just have it give > me some kind of error code. I needed the same thing and I modified some files to eliminate the call that makes the script die ( fatal_disconnect ) . I placed the error in a session string and it can be retrieved using a new method $ssh->get_error(); If you want I will post this patch here. I talked to the maintainer of the code and he is willing to let me incorporate this patch into the next release. -- +----------------------------------------------------------------------- | Mihai Secasiu | http://denixsolutions.com/ | Complete Unix/Linux Solutions for your Business +----------------------------------------------------------------------- |
From: Mihai S. <mih...@de...> - 2004-11-24 15:21:15
|
On Wednesday 24 November 2004 00:27, thefinn wrote: > I now get the "permission denied" and a complete exit of the program when > the user/pass pairs are wrong. I'd like to change this to just have it give > me some kind of error code. I needed the same thing and I modified some files to eliminate the call that makes the script die ( fatal_disconnect ) . I placed the error in a session string and it can be retrieved using a new method $ssh->get_error(); If you want I will post this patch here. I talked to the maintainer of the code and he is willing to let me incorporate this patch into the next release. -- +----------------------------------------------------------------------- | Mihai Secasiu | http://denixsolutions.com/ | Complete Unix/Linux Solutions for your Business +----------------------------------------------------------------------- |
From: Steve S. <sap...@gs...> - 2004-11-25 12:44:42
|
Mihai Secasiu wrote: > On Wednesday 24 November 2004 00:27, thefinn wrote: > >>I now get the "permission denied" and a complete exit of the program when >>the user/pass pairs are wrong. I'd like to change this to just have it give >>me some kind of error code. > > I needed the same thing and I modified some files to eliminate the call that > makes the script die ( fatal_disconnect ) . I placed the error in a session > string and it can be retrieved using a new method $ssh->get_error(); > If you want I will post this patch here. I talked to the maintainer of the > code and he is willing to let me incorporate this patch into the next > release. You can just put the call in an eval block and check the error code, something like this: eval { $ssh->login($user, $pass) }; if ($@) { warn "Loging failed: $@\n"; } -- Steve Sapovits |
From: Mark F. <mar...@ea...> - 2004-11-24 02:15:53
|
Seth, Below is the example of using Expect.pm to interact with an sftp executable. I hope it's not too much of a mess after all the email line-wrapping. I carved this out of a larger script where many of these functions are in reuseable subroutines (so I could use the same code for sftp, or ftp, or anything else). You'll probably hit a few errors since this isn't the exact code I execute. But, generally, the below is what I do. Notwithstanding the syntax errors you may receive, it should be all you need to do to use Expect.pm. If you can improve this example and post something better, please do. (Also, this isn't meant to imply the ssh-sftp Perl module isn't useful. The expect-driven binary is just another way that might be helpful.) Mark ========================================= #! /usr/bin/perl -w use strict; use Expect; #*************************************************************************** **** # This is an example of using Expect.pm (search CPAN for it. Current version is # 1.15 on Nov. 23 2004. Or, go to http://sourceforge.net/projects/expectperl/ . # The "Io_Tty" module is also shown at that site. I don't recall if the # installation requires that module.) # # The following should recreate this command line execution of sftp: # # sftp -oidentityFile=/home/server_user/.ssh/id_dsa.nopass -oUser=client_user some_host <<EOF # put source_file target_file # EOF # # Just change the hardcoded @expectParms to match your requirements (user, host # identity filename). # # WARNING. I carved the following out of a larger script with more abstract # uses of what follows. I haven't *actually* executed the following. You may # run into typos and other problems that cause the Perl interpreter to fail. # It's only meant to help you start in the right direction without the need to # read the Expect.pm documentation and experiment with it. The following should # work after you do my debugging for me. :) #*************************************************************************** **** #--------------------------------------------------------------------------- ---- # Create the object and set the logging to go to a subroutine where log messages # can be accumulated and later interrogated between subcommands (to navigate and # selectively execute subcommands). #--------------------------------------------------------------------------- ---- my $expect = new Expect; our $expect_log; # used by the subroutine coderef'ed on the next line. $expect->log_file(\&logExpect); #--------------------------------------- # Uncomment the following for debugging # information. #--------------------------------------- #$expect->exp_internal(1); #$expect->debug(2); #--------------------------------------------------------------------------- ---- # Hardcoded command-line parms to sftp. You'll want to change this to whatever # command line you use. #--------------------------------------------------------------------------- ---- my @expectParms; $expectParms[0] = '-oidentityFile=/home/server_user/.ssh/id_dsa.nopass'; $expectParms[1] = '-oUser='client_user'; $expectParms[2] = 'some_hostname'; #--------------------------------------------------------------------------- ---- # Start the command. This is where it starts. The parms are passed as an array. #--------------------------------------------------------------------------- ---- if (!$expect->spawn('sftp', @expectParms )) { print 'Command failed to exectute.' . "\n"; exit 99; } # end failed spawn #--------------------------------------------------------------------------- ---- # Setup the expected results. They can be literal strings (prefixed with -ex), # or regular expressions (prefixed with -re). You have to follow the pattern # below, even if they are all literals or regexes. The prefix is one array # element, the value the next array element. Over and over and over, prefix and # value in alternating array elements. # # The ->expect method call will tell you which element matched. #--------------------------------------------------------------------------- ---- my @expectedResults; $expectedResults[0] = '-ex'; $expectedResults[1] = 'sftp>'; $expectedResults[2] = '-re'; $expectedResults[3] = '^sftp\>\n'; # sftp prompt and newline (as an example. You # may not get a newline after the sftp # prompt.) #--------------------------------------------------------------------------- ---- # Now do the expect, for the specified timeout, looking for the values in the # prefix/value alternating array created above. # # Note that @expect_results is a differnt array. It contains # # [0] The item in @expectResults which matched. # [1] The error code (undef, 1:TIMEOUT, 2: EOF which means connection closed I # think, 3:spawn id($fileno) died, 4:$! indicating whatever error was set in # $ERRNO during the last read on $object's handle) # [2] The string that successully matched. # [3] The string preceeding the match string. # [4] The string following the match string. # # You'll be most interested in whether the error is undef, and which element of # @expectedResults was matched (to make decisions about what to do next). # # Next you'll be interested in the $expect_log variable which was accumulated # with all the result messages. If this was a directory listing, you'd want to # expect for the sftp> prompt to know it received the entire directory listing. # You might also be interested in any strings indicating the directory wasn't # found (indicating an error). Then you'd be interested in the error_log # variable to interrogate the actual directory listing data (to look for a # specific file, perhaps). #--------------------------------------------------------------------------- ---- my @expect_results; @expect_results = $expect->expect(30, @expectedResults); if ($expect_results[1]) { if ($expect_results[1] ne '1:TIMEOUT') { print 'Got a non-timeout error." . "\n"; exit 99; } else { print 'Timed out.' . "\n"; exit 99; } print 'Matched on expected value #: ' . $expect_results[1] . "\n"; #--------------------------------------------------------------------------- ---- # Clear the accumulater. This is the storage each ->expect method call will # search. Clear it before doing another ->expect (unless you have a good reason # not to). #--------------------------------------------------------------------------- ---- $expect->clear_accum(); #--------------------------------------------------------------------------- ---- # Send a subcommand. This doesn't seem to return a success of failure. You just # send the subcommand and let the ->expect method call do the work for you. #--------------------------------------------------------------------------- ---- $expect->send('put source_file target_file' . "\n"); #--------------------------------------------------------------------------- ---- # Perform the ->expect method like above. I won't recreate it here. But, you # want to setup the the array of expected literal(s) and/or regex(es). Call # the ->expect method, and determine how the method returned to you (error, # timeout, which element it matched on). Perhaps process the $expect_log # variable if you want to have greater control over the results of the # subcommand. #--------------------------------------------------------------------------- ---- # The POD has a lot more options. The above seems to work well for sftp. You can # do the ->send and ->expect methods repeatedly until you ->send('quit') and # probably don't care about the result. exit; #*************************************************************************** **** # logExpect # # Used by the expect->log_file method as a callback to capture result messages. #*************************************************************************** **** sub logExpect { foreach (@_) { $expect_log .= $_; } return; } |