Net::S...ysFile(3pm)

πŸ“¦
version 0.18 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

🌸 NAME 🌸

Net::SSH::AuthorizedKeysFile</p> β€” the module that reads and modifies ssh's authorized_keys files. That's the whole vibe. This doc page is <hotpink>Net::S...ysFile(3pm)</hotpink> from the User Contributed Perl Documentation corner of the internet, and bestie, it's about to be your new fave thing ever πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's the whole idea in one go, look:

    use Net::SSH::AuthorizedKeysFile;

        # Reads $HOME/.ssh/authorized_keys by default
    my $akf = Net::SSH::AuthorizedKeysFile->new();

    $akf->read("authorized_keys");

        # Iterate over entries
    for my $key ($akf->keys()) {
        print $key->as_string(), "\n";
    }

        # Modify entries:
    for my $key ($akf->keys()) {
        $key->option("from", 'quack@quack.com');
        $key->keylen(1025);
    }
        # Save changes back to $HOME/.ssh/authorized_keys
    $akf->save() or die "Cannot save";

✨ no cap. read, vibe check, tweak, save. that's it ✨


πŸ’– DESCRIPTION πŸ’–

Net::SSH::AuthorizedKeysFile reads and modifies "authorized_keys" files. And what even are those, you ask? πŸ₯Ί "authorized_keys" files hold public keys plus meta info, and ssh on the remote host uses them to let users in without having to type their password. it's giving keycard energy, but for ssh πŸŽ€


πŸ‘‘ METHODS πŸ‘‘

new

Creates a brand new Net::SSH::AuthorizedKeysFile object and reads in the authorized_keys file, all in one go. main character behavior fr fr.

The filename defaults to $HOME/.ssh/authorized_keys unless you override it like this:

Net::SSH::AuthorizedKeysFile->new( file => "/path/other_authkeys_file" );

Now here's the tea 🍡 β€” normally the "read" method (right below, bestie) just silently ignores faulty lines and only gobbles up keys that either one of the two parsers accepts. adorable. but if you want it to be stricter, set this:

Net::SSH::AuthorizedKeysFile->new( file   => "authkeys_file",
                                   abort_on_error => 1 );

and then read will immediately abort after the first faulty line. πŸ’€

Also (there's more!!), the key parsers are fairly lenient in default mode. Adding

strict => 1

adds sanity checks before a key is accepted. bougie behavior ✨

read

Reads in the file defined by new(). By default, strict mode is off and read() will silently ignore faulty lines. If it's on (peep new() above for that), read() will immediately abort after the first faulty line. A textual description of the last error will be available via error(). so if something goes sideways, that's where the receipts live 🧾

content

Contains the original file content, read by "read()" earlier. You can also use it to set arbitrary content:

$keysfile->content( "some\nrandom\nlines\n" );

and have "parse()" operate on a string instead of an actual file this way. slay, honestly 🌈

keys

Returns a list of Net::SSH::AuthorizedKey objects. All their methods are described over in Net::SSH::AuthorizedKey. go peek, iykyk πŸ’œ

as_string

String representation of all keys β€” ultimately the content that gets written out when you call the "save()" method. Note that comments from the original file are lost. 😭 she's honest about it.

save

Write changes back to the authorized_keys file using the as_string() method described above. And yes, note that comments from the original file are lost. again. tragic but true.

It optionally takes a file name parameter, so calling $akf->save("foo.txt") will save the data in the file foo.txt instead of the file the data was read from originally. 🫢

Returns 1 if successful, and undef on error. In case of an error, error() contains a textual error description. periodt.

sanity_check

Run a sanity check on the currently selected authorized_keys file. If it contains insanely long lines, then parsing with read() (and potential crashes because of out-of-memory errors) should be avoided. bestie. please. do the vibe check first πŸ™ˆ

ssh_dir( [$user] )

Locate the .ssh dir of a given user. If no user name is given, ssh_dir will look up the .ssh dir of the effective user. Typically returns something like "/home/gonzo/.ssh". πŸŽ€

path_locate( [$user] )

Locate the authorized_keys file of a given user. Typically returns something like "/home/gonzo/.ssh/authorized_keys". See "ssh_dir()" for how the containing directory is located with and without a given user name. πŸ’«

error

Description of last error that occurred. your little error diary πŸ““


⭐ LEGALESE ⭐

Copyright 2005-2009 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. iconic legal boilerplate, we respect it πŸ‘‘


πŸ’Œ AUTHOR πŸ’Œ

2005, Mike Schilli <m@perlmeister.com>


and that's the whole page bestie!! πŸ¦‹ this was perl v5.36.0, dated 2022-10-13, and yes the footer literally says Net::S...ysFile(3pm) πŸŽ€ go forth and manage those keys, you absolute legend πŸ’–βœ¨

πŸ“¦ where this page comes from