Guest User

learning

a guest
Mar 3rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.06 KB | None | 0 0
  1. #!/usr/bin/env perl6
  2.  
  3. # osx brew libssh needs to know where it lives
  4. %*ENV<PERL6_LIBSSH_LIB>:delete;
  5. %*ENV.push: (PERL6_LIBSSH_LIB => 'libssh.4.dylib');
  6.  
  7. use SSH::LibSSH;
  8. use Config::INI;
  9.  
  10. sub runCommand( $host, $user, $port, $password, $private-key-file, *@command) {
  11.  
  12.     my $session = await SSH::LibSSH.connect(:$host, :$user, :$port, :$private-key-file, :$password);
  13.     my $channel = await $session.execute(@command.join(' '));
  14.     my $exit-code;
  15.     my (@stdin, @stdout, @stderr);
  16.     react {
  17.         unless $*IN.t {
  18.             whenever $channel.print($*IN.slurp-rest) {
  19.                 $channel.close-stdin;
  20.             }
  21.         }
  22.         whenever $channel.stdout(:enc<utf8>) -> $chars {
  23. #            $*OUT.print: $chars;
  24.          @stdout.append: $chars;
  25.         }
  26.         whenever $channel.stderr(:enc<utf8>) -> $chars {
  27. #            $*ERR.print: $chars;
  28.             @stderr.append: $chars;
  29.         }
  30.         whenever $channel.exit -> $code {
  31.             $exit-code = $code;
  32.         }
  33.     }
  34.     $channel.close;
  35.     $session.close;
  36. #    exit $exit-code;
  37.  
  38.     CATCH {
  39.         when X::SSH::LibSSH::Error {
  40.             note .message;
  41.             exit 1;
  42.         }
  43.     }
  44.     return (@stdout, @stderr, $exit-code);
  45. }
  46.  
  47. sub copyFile($host, $user, $local, $remote, Int $port, Str $password) {
  48.     my $session = await SSH::LibSSH.connect(:$host, :$user, :$port, :$password);
  49.     await $Session.scp-upload($local, $remote);
  50.     $session.close;
  51. }
  52.  
  53.  
  54.  
  55. sub MAIN($host, $user, Str $password, $local='.ssh/authorized_keys', $remote='.ssh/authorized_keys', Int :$port=22, Str :$private-key-file ) {
  56.     my (@stdout, @stderr, $exit-code) =
  57.     runCommand( $host, $user, $port, $password, $private-key-file, <grep -E '^(ID|VERSION)' /etc/os-release>);
  58.     say @stdout[0];
  59.     my    %hash = Config::INI::parse(@stdout[0].Str);
  60.     say %hash<_><root_property_key>;
  61.    
  62. #say "hey: " , @stdout;
  63. #    copyFile($host, $user, $local, $remote, $port, $password);
  64. }
  65.  
  66. =finish
  67.  
  68. (qq{VERSION_ID="9"\n},qq{VERSION="9 (stretch)"\n}, qq{ID=raspbian\n}, qq{ID_LIKE=debian\n})
Add Comment
Please, Sign In to add comment