Skip to content

Instantly share code, notes, and snippets.

@psychowood
Forked from gatlin/keepassalong.pl
Last active December 7, 2017 10:31
Show Gist options
  • Save psychowood/1de5a1538deeaa6951ce0f7c74b4a055 to your computer and use it in GitHub Desktop.
Save psychowood/1de5a1538deeaa6951ce0f7c74b4a055 to your computer and use it in GitHub Desktop.
A script to convert a CSV exported from KeePass(X) to a .kdb format.
#!/usr/bin/env perl
#You need to install File::KeePass module, with cpan or manually
#have a look here if you need help: http://www.thegeekstuff.com/2008/09/how-to-install-perl-modules-manually-and-using-cpan-command/
use v5.14;
use strict;
use warnings;
use File::KeePass;
use Text::CSV;
use Data::Dump qw(pp);
use Term::ReadPassword;
# Initialize the CSV data structures
my $csv = Text::CSV->new ({ binary => 0 })
or die "Cannot use CSV: " . Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", $ARGV[0] or die "csv: $!";
my ($filename, $fileext) = split /\./, $ARGV[0];
# Initialize the KeePass data structures
my $k = File::KeePass->new;
my $group = $k->add_group({
title => 'Default',
});
# Loop: parse a CSV row, add entry to database
while (my $row = $csv->getline($fh)) {
my $e = $k->add_entry({
title => $row->[0],
url => $row->[1],
username => $row->[2],
password => $row->[3],
comment => $row->[4],
});
}
$csv->eof or $csv->error_diag();
close $fh;
# Acquire database password and save
while (1) {
my $password = read_password("Choose database password: ");
redo unless defined $password;
# Now generate the KeePassX database
$k->save_db("$filename.kdb",$password);
last;
}
1;
__END__
=pod
=head1 NAME
keepassalong - read KeePass CSV files, spit out a KeePassX compatible database
=head1 SYNOPSIS
$> keepassalong somedb.csv
This will produce C<somedb.kdb>.
=head1 LICENSE
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment