Guest

charly

By: a guest on May 16th, 2008  |  syntax: Perl  |  size: 0.96 KB  |  hits: 76  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4.  
  5. use DBI;
  6.  
  7. exit unless (grep(/mysql/,DBI->available_drivers));
  8.  
  9. my $db = "mysql";
  10. my $host = "localhost";
  11. my $port = "8889";
  12.  
  13. my $dsn = "DBI:mysql:database=$db;host=$host;port=$port";
  14.  
  15. my $username = "root";
  16. my $passwd = "root";
  17.  
  18. my $dbh = DBI->connect($dsn,$username,$passwd) or die "connexion impossible $DBI::errstr";
  19. my $sth = $dbh->prepare("SELECT Host, User, Password FROM user;");
  20.  
  21. $sth->execute() or die "$DBI::errstr";
  22.  
  23. open(OUTFILE,">DBI.csv") or die "ouverture en écriture impossible: $!";
  24.  
  25. while(my($uhost,$user,$upasswd) = $sth->fetchrow_array()) {
  26.         print OUTFILE "$uhost,$user,$upasswd\n";
  27. }
  28.  
  29. $sth->finish();
  30. $dbh->disconnect();
  31.  
  32. close OUTFILE;
  33.  
  34. open(INFILE,"DBI.csv") or die "ouverture en lecture impossible: $!";
  35.  
  36. while(<INFILE>) {
  37.         next unless ($_ =~ /^.*,.*,.*$/);
  38.        
  39.         my ($uhost,$user,$upass) = split /,/, $_;
  40.         print "Host: $uhost, User: $user, Password: $upass\n";
  41. }
  42.  
  43. close INFILE;