Re: [Ssh-sftp-perl-users] Automating sftp using perl sftp plugin
Brought to you by:
dbrobins
From: Morgan S. <mo...@du...> - 2007-07-25 19:44:49
|
Oops, I replied but forgot to send it to the list. Ralf Wiegand wrote: > Morgan - Her is a better example.=20 >=20 > my $svt =3D 'ftp.mydomain.com'; > my %cfg =3D (user=3D>'test_user', password =3D> 'secure', debug=3D= >1); With the password in your script, be careful of the script's file permissions as it is a potential security issue. Even though my sshd is configured to use only ssh protocol version 2 I like to let my script know what to expect by putting the following inside my %cfg: protocol=3D>2 > my $sftp =3D Net::SFTP::Recursive->new($svr, %cfg); It may be worth putting statements like this inside an eval so that you can gracefully handle unforeseen errors with connections and commands: unless (my $sftp =3D eval{Net::SFTP::Recursive->new($svr, %cfg) { die "Connection failed: $@"; } I've come across issues where $@ contained errors such as "permission denied" for invalid passwords or disabled accounts, "Can't connect to" for network issues, and I've also rarely come across "connection closed" errors on buggy systems. Seems to me without the eval{} then the script would just die and I had no idea why. My script parsed $@ and was able to clue the user in as to what was going on. Your other script used "or die" to handle errors. With Net::SFTP it didn't work right for me, but eval{} did. > $sftp->rput('/local/mydir', '/remote/dir', \&my_cb); > # with file and dir filters > $sftp->rput('/local/mydir', '/remote/dir', \&my_cb, > {file_pat=3D>'pdf$', dir_pat=3D>'^f', cb4put=3D>\&myput_cb}= ); >=20 > Return: $msg - number of files transferred My suggestion for evaling the the connection would also apply to any commands ($sftp->rput) as well. I like to check the results of every command and this method has helped me to quickly debug/troubleshoot and inform users of all sorts of errors (quota/disk space, permission issues, transfer issues, etc.). Just make sure you keep in mind what is inside of eval{} and what will be returning in order to keep your logic straight. It took me a bit to get the hang of it. As always, 0 and null are different so be sure to check the returned values properly. It may be worth noting that my script used Net::SFTP instead of Net::SFTP::Recursive, so there may be some minor differences. --=20 Morgan Smith Dutro Company 675 North 600 West Logan, UT 84321 (435) 752-3921 x146 GPG Key: 76E09074 Keyserver: http://www.keyserver.net/ |