#!/usr/bin/env perl use strict; use warnings; use diagnostics; use Net::OpenSSH; use DBI; use feature 'say'; my $local_root = '/home/christoph/Notes/TU/MastersThesis/FailNix'; my $remote_host = 'mars'; # smchurla@mars.cs.tu-dortmund.de # The mars db is bound to local port 3306 over SSH. # - This requires using the configured 'mars' # remote_host instead of smchurla@mars # or setting up another tunnel here my $db_host = "127.0.0.1"; my $db_port = "3306"; my $db_user = "smchurla"; # Database password open( my $fhandle, '<', "$local_root/mars-db.conf" ) or die "Failed to read mars-db.conf: $!"; chomp( my $db_password = <$fhandle> ); close($fhandle); # Initialize SSH connection # - This connection also sets up the database tunnel my $ssh = Net::OpenSSH->new( $remote_host, timeout => 30, master_opts => [ -o => 'BatchMode=yes', -o => 'StrictHostKeyChecking=accept-new', ], ); $ssh->error and die 'SSH connection failed: ' . $ssh->error; say 'Connected to mars.cs.tu-dortmund.de'; # Initialize db connection my $dbh = DBI->connect( "DBI:MariaDB:host=$db_host;port=$db_port", $db_user, $db_password ) or die 'Failed to connect to database: ' . $DBI::errstr; say 'Connected to database'; while (1) { say 'Existing databases:'; my @db_names = sort map { s/DBI:MariaDB://r } grep { !/information_schema|smchurla_ll/ } $dbh->data_sources(); foreach (@db_names) { say " - $_"; } print 'Enter single name or comma-separated list to delete: '; my $db_sel = ; chomp $db_sel; my @selected_dbs = $db_sel eq "all" ? @db_names : split( ',', $db_sel ); foreach (@selected_dbs) { $dbh->do("drop database `$_`") or die "Failed to drop database: " . $dbh->errstr; } } $dbh->disconnect or warn $dbh->errstr;