| 1 | # ----------------------------------------------------------------------------- | ||||||||||||
| 2 | |||||||||||||
| 3 | =encoding utf8 | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Quiq::Ssh - Führe Kommando per SSH aus | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||
| 10 | |||||||||||||
| 11 | L<Quiq::Hash> | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||
| 14 | |||||||||||||
| 15 | Ein Objekt der Klasse repräsentiert eine SSH-Verbindung zu einem Host. | ||||||||||||
| 16 | Über die Verbindung können Shell-Kommandos ausgeführt werden. Die | ||||||||||||
| 17 | Klasse ist ein Wrapper für die Klasse Net::SSH::Perl, die das | ||||||||||||
| 18 | SSH Netzprotokoll direkt spricht. | ||||||||||||
| 19 | |||||||||||||
| 20 | =head2 Installation | ||||||||||||
| 21 | |||||||||||||
| 22 | Sollte es bei der Installation von Net::SSH::Perl in | ||||||||||||
| 23 | Crypt::Curve25519 zu dem Fehler kommen | ||||||||||||
| 24 | |||||||||||||
| 25 | curve25519-donna-c64.c:99:1: error: conflicting types for ‘fmul’ | ||||||||||||
| 26 | fmul(felem output, const felem in2, const felem in) { | ||||||||||||
| 27 | ^~~~ | ||||||||||||
| 28 | In file included from /home/fs2/sys/opt/perlbrew/perls/perl-5.30.0/lib/5.30.0/x86_64-linux/CORE/perl.h:2068, | ||||||||||||
| 29 | from Curve25519.xs:3: | ||||||||||||
| 30 | /usr/include/x86_64-linux-gnu/bits/mathcalls-narrow.h:30:20: note: previous declaration of ‘fmul’ was here | ||||||||||||
| 31 | __MATHCALL_NARROW (__MATHCALL_NAME (mul), __MATHCALL_REDIR_NAME (mul), 2); | ||||||||||||
| 32 | ^~~~~~~~~~~~~~~ | ||||||||||||
| 33 | |||||||||||||
| 34 | kann dieser mit | ||||||||||||
| 35 | |||||||||||||
| 36 | $ grep -rl "fmul" ./ | xargs sed -i 's/fmul/fixedvar/g' | ||||||||||||
| 37 | |||||||||||||
| 38 | behoben werden. Siehe: L<https://github.com/ajgb/crypt-curve25519/issues/9#issuecomment-447845725> | ||||||||||||
| 39 | |||||||||||||
| 40 | =head1 EXAMPLE | ||||||||||||
| 41 | |||||||||||||
| 42 | Zeige den Inhalt des Homeverzeichnisses auf Host dssp an: | ||||||||||||
| 43 | |||||||||||||
| 44 | $ perl -MQuiq::Ssh -E 'print Quiq::Ssh->new("dssp")->exec("ls")' | ||||||||||||
| 45 | |||||||||||||
| 46 | =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | # ----------------------------------------------------------------------------- | ||||||||||||
| 49 | |||||||||||||
| 50 | package Quiq::Ssh; | ||||||||||||
| 51 | 1 | ▶ | use base qw/Quiq::Hash/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | |||||||||||||
| 53 | 1 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | |||||||||||||
| 57 | 1 | our $VERSION = '1.240'; | |||||||||||
| 58 | |||||||||||||
| 59 | 1 | ▶ | use Quiq::Parameters; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 60 | 1 | ▶ | use Net::SSH::Perl (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 1 | ▶ | use Quiq::Shell; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | 1 | ▶ | use Quiq::Exit; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | 1 | ▶ | use Encode (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | |||||||||||||
| 65 | # ----------------------------------------------------------------------------- | ||||||||||||
| 66 | |||||||||||||
| 67 | =head1 METHODS | ||||||||||||
| 68 | |||||||||||||
| 69 | =head2 Klassenmethoden | ||||||||||||
| 70 | |||||||||||||
| 71 | =head3 new() - Konstruktor | ||||||||||||
| 72 | |||||||||||||
| 73 | =head4 Synopsis | ||||||||||||
| 74 | |||||||||||||
| 75 | $ssh = $class->new($host,@opt); | ||||||||||||
| 76 | |||||||||||||
| 77 | =head4 Arguments | ||||||||||||
| 78 | |||||||||||||
| 79 | =over 4 | ||||||||||||
| 80 | |||||||||||||
| 81 | =item $host | ||||||||||||
| 82 | |||||||||||||
| 83 | Hostname | ||||||||||||
| 84 | |||||||||||||
| 85 | =back | ||||||||||||
| 86 | |||||||||||||
| 87 | =head4 Options | ||||||||||||
| 88 | |||||||||||||
| 89 | =over 4 | ||||||||||||
| 90 | |||||||||||||
| 91 | =item -debug => $bool (Default: 0) | ||||||||||||
| 92 | |||||||||||||
| 93 | Schreibe Debug-Information über die SSH-Kommunikation nach STDERR. | ||||||||||||
| 94 | |||||||||||||
| 95 | =item -loginShell => $bool (Default: 1) | ||||||||||||
| 96 | |||||||||||||
| 97 | Default für Methode exec(): Führe Kommandos per Login-Shell aus. | ||||||||||||
| 98 | |||||||||||||
| 99 | =item -user => $user (Default: I<Wert von $USER>) | ||||||||||||
| 100 | |||||||||||||
| 101 | Name des Benutzers. | ||||||||||||
| 102 | |||||||||||||
| 103 | =back | ||||||||||||
| 104 | |||||||||||||
| 105 | =head4 Returns | ||||||||||||
| 106 | |||||||||||||
| 107 | Objekt | ||||||||||||
| 108 | |||||||||||||
| 109 | =head4 Description | ||||||||||||
| 110 | |||||||||||||
| 111 | Instantiiere ein Objekt für die Ausführung von Kommandos via SSH | ||||||||||||
| 112 | auf Host $host über den Benutzer $user. | ||||||||||||
| 113 | |||||||||||||
| 114 | =cut | ||||||||||||
| 115 | |||||||||||||
| 116 | # ----------------------------------------------------------------------------- | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 119 | 0 | my $class = shift; | |||||||||||
| 120 | # @_: $host,@opt | ||||||||||||
| 121 | |||||||||||||
| 122 | # Argumente und Optionen | ||||||||||||
| 123 | |||||||||||||
| 124 | my ($argA,$opt) = Quiq::Parameters->extractToObject(\@_,1,1, | ||||||||||||
| 125 | -debug => 0, | ||||||||||||
| 126 | -loginShell => 1, | ||||||||||||
| 127 | 0 | -user => $ENV{'USER'}, | |||||||||||
| 128 | ); | ||||||||||||
| 129 | 0 | my $host = shift @$argA; | |||||||||||
| 130 | |||||||||||||
| 131 | # Operation ausführen | ||||||||||||
| 132 | |||||||||||||
| 133 | 0 | my $obj = Net::SSH::Perl->new($host, | |||||||||||
| 134 | # interactive => 0, | ||||||||||||
| 135 | debug => $opt->debug, | ||||||||||||
| 136 | ); | ||||||||||||
| 137 | |||||||||||||
| 138 | # Login | ||||||||||||
| 139 | |||||||||||||
| 140 | 0 | eval {$obj->login($opt->user)}; | |||||||||||
| 141 | 0 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 0 | $@ =~ s/ at .*//; | |||||||||||
| 143 | 0 | $class->throw( | |||||||||||
| 144 | 'SSH-00099: Login failed', | ||||||||||||
| 145 | Reason => $@, | ||||||||||||
| 146 | ); | ||||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | # Objekt instantiieren | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | return $class->SUPER::new( | |||||||||||
| 152 | loginShell => $opt->loginShell, | ||||||||||||
| 153 | obj => $obj, | ||||||||||||
| 154 | ); | ||||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | # ----------------------------------------------------------------------------- | ||||||||||||
| 158 | |||||||||||||
| 159 | =head2 Objektmethoden | ||||||||||||
| 160 | |||||||||||||
| 161 | =head3 exec() - Führe Kommando aus | ||||||||||||
| 162 | |||||||||||||
| 163 | =head4 Synopsis | ||||||||||||
| 164 | |||||||||||||
| 165 | ($stdout,$stderr) = $ssh->exec($cmd,@opt); | ||||||||||||
| 166 | ($stdout,$stderr,$exit) = $ssh->exec($cmd,-sloppy=>1,@opt); | ||||||||||||
| 167 | |||||||||||||
| 168 | =head4 Arguments | ||||||||||||
| 169 | |||||||||||||
| 170 | =over 4 | ||||||||||||
| 171 | |||||||||||||
| 172 | =item $cmd (String) | ||||||||||||
| 173 | |||||||||||||
| 174 | Kommandozeile | ||||||||||||
| 175 | |||||||||||||
| 176 | =back | ||||||||||||
| 177 | |||||||||||||
| 178 | =head4 Options | ||||||||||||
| 179 | |||||||||||||
| 180 | =over 4 | ||||||||||||
| 181 | |||||||||||||
| 182 | =item -loginShell => $bool (Default: I<Wert der Option beim Konstruktor>) | ||||||||||||
| 183 | |||||||||||||
| 184 | Führe das Remote-Kommando unter einer Login-Shell aus. Als Shell | ||||||||||||
| 185 | wird die bash verwendet. | ||||||||||||
| 186 | |||||||||||||
| 187 | =item -sloppy => $bool (Default: 0) | ||||||||||||
| 188 | |||||||||||||
| 189 | Wirf keine Exception, wenn das Remote-Kommando fehlschlägt, sondern | ||||||||||||
| 190 | liefere den Exitcode als dritten Returnwert zurück. | ||||||||||||
| 191 | |||||||||||||
| 192 | =back | ||||||||||||
| 193 | |||||||||||||
| 194 | =head4 Returns | ||||||||||||
| 195 | |||||||||||||
| 196 | =over 4 | ||||||||||||
| 197 | |||||||||||||
| 198 | =item $stdout | ||||||||||||
| 199 | |||||||||||||
| 200 | (String) Ausgabe des Kommandos auf stdout. Wenn keine Ausgabe, Leerstring. | ||||||||||||
| 201 | |||||||||||||
| 202 | =item $stderr | ||||||||||||
| 203 | |||||||||||||
| 204 | (String) Ausgabe des Kommandos auf stderr. Wenn keine Ausgabe, Leerstring. | ||||||||||||
| 205 | |||||||||||||
| 206 | =item $exit | ||||||||||||
| 207 | |||||||||||||
| 208 | (Integer) Exitcode des Kommandos. Wird gesetzt, wenn C<< -sloppy=>1 >> ist, | ||||||||||||
| 209 | sonst konstant 0. | ||||||||||||
| 210 | |||||||||||||
| 211 | =back | ||||||||||||
| 212 | |||||||||||||
| 213 | =cut | ||||||||||||
| 214 | |||||||||||||
| 215 | # ----------------------------------------------------------------------------- | ||||||||||||
| 216 | |||||||||||||
| 217 | 0 | ▶ | sub exec { | ||||||||||
|
Subroutine
exec: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 218 | 0 | my $self = shift; | |||||||||||
| 219 | # @_: $cmd,@opt | ||||||||||||
| 220 | |||||||||||||
| 221 | # Argumente und Optionen | ||||||||||||
| 222 | |||||||||||||
| 223 | 0 | my $loginShell = $self->get('loginShell'); | |||||||||||
| 224 | 0 | my $sloppy = 0; | |||||||||||
| 225 | |||||||||||||
| 226 | 0 | my $argA = $self->parameters(1,1,\@_, | |||||||||||
| 227 | -loginShell => \$loginShell, | ||||||||||||
| 228 | -sloppy => \$sloppy, | ||||||||||||
| 229 | ); | ||||||||||||
| 230 | 0 | my $cmd = shift @$argA; | |||||||||||
| 231 | |||||||||||||
| 232 | # Operation ausführen | ||||||||||||
| 233 | |||||||||||||
| 234 | 0 | ▶ | if ($loginShell) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 235 | # Login-Shell | ||||||||||||
| 236 | |||||||||||||
| 237 | 0 | $cmd =~ s/'/\\'/g; # Single Quotes schützen | |||||||||||
| 238 | 0 | $cmd = "/bin/bash -lc '$cmd'"; | |||||||||||
| 239 | } | ||||||||||||
| 240 | |||||||||||||
| 241 | # Wenn wir Encoden gibt es bei </dev/null folgende Meldung: | ||||||||||||
| 242 | # "sysread() is deprecated on :utf8 handles. This will be a | ||||||||||||
| 243 | # fatal error in Perl 5.30 at .../Net/SSH/Perl/Channel.pm line 156." | ||||||||||||
| 244 | 0 | $cmd = Encode::encode('utf-8',$cmd); | |||||||||||
| 245 | # $cmd = Encode::decode('utf-8',$cmd); | ||||||||||||
| 246 | 0 | my ($stdout,$stderr,$exit) = $self->obj->cmd($cmd); | |||||||||||
| 247 | 0 | ▶ | $stdout //= ''; | ||||||||||
|
Condition0%0/2
$stdout //= ""
MC/DC0%0/1
$stdout //= ""
$stdout
Truth table0%0/2
A
A= $stdout
| |||||||||||||
| 248 | 0 | ▶ | $stderr //= ''; | ||||||||||
|
Condition0%0/2
$stderr //= ""
MC/DC0%0/1
$stderr //= ""
$stderr
Truth table0%0/2
A
A= $stderr
| |||||||||||||
| 249 | 0 | ▶ | $exit //= 0; | ||||||||||
|
Condition0%0/2
$exit //= 0
MC/DC0%0/1
$exit //= 0
$exit
Truth table0%0/2
A
A= $exit
| |||||||||||||
| 250 | 0 | ▶ | if (!$sloppy) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 251 | # $exit ist als Exitcode kodiert | ||||||||||||
| 252 | # Quiq::Shell->checkError($exit*256,undef,$cmd); | ||||||||||||
| 253 | 0 | Quiq::Exit->check($exit*256,$cmd); | |||||||||||
| 254 | } | ||||||||||||
| 255 | |||||||||||||
| 256 | 0 | return ($stdout,$stderr,$exit); | |||||||||||
| 257 | } | ||||||||||||
| 258 | |||||||||||||
| 259 | # ----------------------------------------------------------------------------- | ||||||||||||
| 260 | |||||||||||||
| 261 | =head1 VERSION | ||||||||||||
| 262 | |||||||||||||
| 263 | 1.240 | ||||||||||||
| 264 | |||||||||||||
| 265 | =head1 AUTHOR | ||||||||||||
| 266 | |||||||||||||
| 267 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||
| 268 | |||||||||||||
| 269 | =head1 COPYRIGHT | ||||||||||||
| 270 | |||||||||||||
| 271 | Copyright (C) 2026 Frank Seitz | ||||||||||||
| 272 | |||||||||||||
| 273 | =head1 LICENSE | ||||||||||||
| 274 | |||||||||||||
| 275 | This code is free software; you can redistribute it and/or modify | ||||||||||||
| 276 | it under the same terms as Perl itself. | ||||||||||||
| 277 | |||||||||||||
| 278 | =cut | ||||||||||||
| 279 | |||||||||||||
| 280 | # ----------------------------------------------------------------------------- | ||||||||||||
| 281 | |||||||||||||
| 282 | 1 | 1; | |||||||||||
| 283 | |||||||||||||
| 284 | # eof | ||||||||||||