Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl6
- # osx brew libssh needs to know where it lives
- %*ENV<PERL6_LIBSSH_LIB>:delete;
- %*ENV.push: (PERL6_LIBSSH_LIB => 'libssh.4.dylib');
- use SSH::LibSSH;
- use Config::INI;
- sub runCommand( $host, $user, $port, $password, $private-key-file, *@command) {
- my $session = await SSH::LibSSH.connect(:$host, :$user, :$port, :$private-key-file, :$password);
- my $channel = await $session.execute(@command.join(' '));
- my $exit-code;
- my (@stdin, @stdout, @stderr);
- react {
- unless $*IN.t {
- whenever $channel.print($*IN.slurp-rest) {
- $channel.close-stdin;
- }
- }
- whenever $channel.stdout(:enc<utf8>) -> $chars {
- # $*OUT.print: $chars;
- @stdout.append: $chars;
- }
- whenever $channel.stderr(:enc<utf8>) -> $chars {
- # $*ERR.print: $chars;
- @stderr.append: $chars;
- }
- whenever $channel.exit -> $code {
- $exit-code = $code;
- }
- }
- $channel.close;
- $session.close;
- # exit $exit-code;
- CATCH {
- when X::SSH::LibSSH::Error {
- note .message;
- exit 1;
- }
- }
- return (@stdout, @stderr, $exit-code);
- }
- sub copyFile($host, $user, $local, $remote, Int $port, Str $password) {
- my $session = await SSH::LibSSH.connect(:$host, :$user, :$port, :$password);
- await $Session.scp-upload($local, $remote);
- $session.close;
- }
- sub MAIN($host, $user, Str $password, $local='.ssh/authorized_keys', $remote='.ssh/authorized_keys', Int :$port=22, Str :$private-key-file ) {
- my (@stdout, @stderr, $exit-code) =
- runCommand( $host, $user, $port, $password, $private-key-file, <grep -E '^(ID|VERSION)' /etc/os-release>);
- say @stdout[0];
- my %hash = Config::INI::parse(@stdout[0].Str);
- say %hash<_><root_property_key>;
- #say "hey: " , @stdout;
- # copyFile($host, $user, $local, $remote, $port, $password);
- }
- =finish
- (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