Net::SSH::Putty - Perl module to execute SSH sessions with Putty in batch mode
use Net::SSH::Putty; my $ssh = Net::SSH::Putty->new({ host => $fqdn, user => $user, password => $password}); $ssh->exec(['. .bash_profile', $cmd]);
This module implements a Moo based class to interact with Putty program for non-interactive SSH sessions from MS Windows hosts.
Inspiration from this module came from the necessity to have non-interactive SSH session working on Windows. Initially this was attempted with Net::SSH::Any (using Net::SSH::Any::Backend::Plink_Cmd) with a certain degree of success, but as soon things get more complicated (like creating multiple SSH sessions using threads) sessions started failing without further explanation or error messages.
The combination of MS Windows, Perl and SSH is historically problematic too.
Since the author didn't need any interaction within the SSH sessions (like executing a command, reading the output and taking some conditional action), a simple solution would be fork the plink.exe executable with a set of commands already defined (that I'll name of "batch mode" from now).
fork
plink.exe
These are the attributes defined for the Net::SSH::Putty class:
Read-only and required for object instantiation.
The SSH login to be used for authentication.
The SSH login password to be used for authentication.
The FQDN to connect through SSH.
Read-only.
The complete pathname to the location where Putty program is installed. By default, it is C:\Program Files (x86)\Putty.
C:\Program Files (x86)\Putty
Stores an array reference containing the last command (executed through the SSH connection) output.
Getter for user.
user
Getter for password.
password
Getter for putty_path.
putty_path
Getter for output.
output
Connects to the host with plink.exe and execute the commands passed as parameters.
Expects as parameters an array references with the commands to execute.
Returns true or false (in Perl sense) if the commands were executed successfully or not.
This method also sets the output attribute.
This methods allow a instance to download a single file with psftp.exe program.
psftp.exe
Expects as positional parameters:
The remote path to the directory containing the file.
The filename of the file on the remote location.
The local directory to be used as repository.
The method will fork psftp.exe and execute the commands:
cd
lcd
get
del
In that sequence. Beware that the remote file will be removed from the SSH server then.
A experimental method.
You can setup the plink.exe program to generate a log file of the SSH session, but the log contains binary information.
This method tries to read those contents and return an array reference with only the text from it.
Expects as parameter the complete path to this "binary" log file.
This program is a hack, not a robust solution. Keep that in mind if you're going to execute it with any kind of monitoring.
The download method is not very flexible. If you need to download a series of files, it will be inefficient since multiple SFTP sessions will be open instead of a single one. Maybe in the future this might change.
download
Also, since read_log is experimental and output redirection on MS Windows requires using system invoking the shell, this might be considered insecure if malicious values are used during object creation. Taint mode is not active in this module.
read_log
system
Net::SSH::Any
https://github.com/salva/p5-Net-SSH-Any/issues/2
Dist::Zilla is used to setup this distribution.
http://www.putty.org/
Alceu Rodrigues de Freitas Junior, <arfreitas@cpan.org>
This software is copyright (c) 2017 of Alceu Rodrigues de Freitas Junior, <arfreitas@cpan.org>
This file is part of net-ssh-putty project.
net-ssh-putty is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
net-ssh-putty is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with net-ssh-putty. If not, see <http://www.gnu.org/licenses/>.
To install Net::SSH::Putty, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::SSH::Putty
CPAN shell
perl -MCPAN -e shell install Net::SSH::Putty
For more information on module installation, please visit the detailed CPAN module installation guide.