Alceu Rodrigues de Freitas Junior

NAME

Net::SSH::Putty - Perl module to execute SSH sessions with Putty in batch mode

SYNOPSIS

    use Net::SSH::Putty;
    
    my $ssh = Net::SSH::Putty->new({ host => $fqdn, user => $user, password => $password});
    $ssh->exec(['. .bash_profile', $cmd]);

DESCRIPTION

This module implements a Moo based class to interact with Putty program for non-interactive SSH sessions from MS Windows hosts.

MOTIVATIONS

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).

ATTRIBUTES

These are the attributes defined for the Net::SSH::Putty class:

user

Read-only and required for object instantiation.

The SSH login to be used for authentication.

password

The SSH login password to be used for authentication.

host

Read-only and required for object instantiation.

The FQDN to connect through SSH.

putty_path

Read-only.

The complete pathname to the location where Putty program is installed. By default, it is C:\Program Files (x86)\Putty.

output

Read-only.

Stores an array reference containing the last command (executed through the SSH connection) output.

METHODS

get_user

Getter for user.

get_password

Getter for password.

get_putty_path

Getter for putty_path.

get_output

Getter for output.

exec

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.

download

This methods allow a instance to download a single file with psftp.exe program.

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:

  1. cd

  2. lcd

  3. get

  4. del

In that sequence. Beware that the remote file will be removed from the SSH server then.

Returns true or false (in Perl sense) if the commands were executed successfully or not.

read_log

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 test.

Expects as parameter the complete path to this "binary" log file.

LIMITATIONS

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.

Also, since c<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.

SEE ALSO

AUTHOR

Alceu Rodrigues de Freitas Junior, <arfreitas@cpan.org>

COPYRIGHT AND LICENSE

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/>.