Thursday, June 25, 2015

Connecting to SQL Server from Linux/Unix using Perl DBI



Connecting to SQL Server from Linux using Perl DBI

Download Software Components

Perl DBI Module   :DBI-1.633.tar
http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.633.tar.gz

Download FreeTDS :freetds-stable.tar
http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz

Perl DBD::Sybase Module :DBD-Sybase-1.15.tar
http://search.cpan.org/CPAN/authors/id/M/ME/MEWP/DBD-Sybase-1.15.tar.gz


Install DBI

1 - DBI-1.633.tar

perl Makefile.PL
make
make install

Install FreeTDS

2 - freetds-stable.tar

./configure --with-tdsver=7.0 --prefix=/usr/local/freetds
perl Makefile.PL
make
make install

Install DBD-Sybase

3 - DBD-Sybase-1.15.tar

# set||export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:/usr/local/freetds/lib
# set||export SYBASE=/usr/local/freetds

perl Makefile.PL
Use 'CHAINED' mode by default (Y/N) [Y]: Y
Use the threaded (lib..._r) libraries [N]: N
Sybase server to use (default: SYBASE): 192.168.0.98
User ID to log in to Sybase (default: sa): sa
Password (default: undef): 12345678
Sybase database to use on 192.168.0.98 (default: undef): TESTDB
make
make install

Configure FreeTDS

# vim /usr/local/freetds/etc/freetds.conf
       
[TESTDB]
       host = 192.168.0.98
       port = 1433
       tds version = 8.0
       dump file = /tmp/freetds.log
       text size = 64512




SCRIPT TO TEST

#!/usr/bin/perl

use DBI;

my $user   = "userdb";
my $passwd = "12345678";
my $server = "TESTDB";

my $dbh = DBI->connect("DBI:Sybase:server=$server", $user, $passwd, {PrintError => 0});
my $sth;

unless ($dbh) {
   die "ERROR: Failed to connect to server ($server).\nERROR MESSAGE: $DBI::errstr";
} else {
   print "\n";
   print "Successful Connection.";
   print "\n\n";
}


print "Current Connection Properties\n";
print "---------------------------------------------------------------------------------------\n";
my $sqlStatement = "select \@\@servername, \@\@version, db_name(), system_user, host_name()";
unless ($sth = $dbh->prepare($sqlStatement)) {
   $dbh->disconnect;
   die "ERROR: Failed to prepare SQL statement.\nSQL: $sqlStatement\nERROR MESSAGE: $DBI::errstr";
}
unless ($sth->execute) {
   $dbh->disconnect;
   die "ERROR: Failed to execute query.\nSQL: $sqlStatement\nERROR MESSAGE: $DBI::errstr";
}
while ((    $serverName
         , $sqlServerVersion
         , $currentDatabase
         , $currentUser
         , $clientMachine) = $sth->fetchrow) {
   ($sqlServerVersion, @dummy) = split(/\n/, $sqlServerVersion);

   print "    SQL Server Instance : $serverName\n";
   print "    SQL Server Version  : $sqlServerVersion\n";
   print "    Current Database    : $currentDatabase\n";
   print "    Current User        : $currentUser\n";
   print "    Client Machine      : $clientMachine\n";
}
print "\n";
$sth->finish;


print "Available Databases\n";
print "-----------------------------------------------------------------\n";
my $sqlStatement = "select
                       isnull(db_name(dbid), 'MSSQL SYSTEM RESOURCE') as Name
                     , str(convert(dec(15),sum(size))* 8192/ 1048576,10,2)+ N' MB' as Size
                   from sys.sysaltfiles
                   group by dbid
                   order by 2 desc";
unless ($sth = $dbh->prepare($sqlStatement)) {
   $dbh->disconnect;
   die "ERROR: Failed to prepare SQL statement.\nSQL: $sqlStatement\nERROR MESSAGE: $DBI::errstr";
}
unless ($sth->execute) {
   $dbh->disconnect;
   die "ERROR: Failed to execute query.\nSQL: $sqlStatement\nERROR MESSAGE: $DBI::errstr";
}
while (($databaseName, $databaseSize) = $sth->fetchrow) {
   printf("    %-40s %20s\n", $databaseName, $databaseSize);  
}
print "\n";
$sth->finish;

print "Disconnecting from SQL Server.\n\n";
$dbh->disconnect;
exit(0);

Sunday, June 7, 2015

How to Enable su root For a Normal User in FreeBSD


root@freebsd:~ # su - andre
When you've made modifications to a file in vi(1) and then find that
you can't write it, type ``<ESC>!rm -f %'' then ``:w!'' to force the
write

This won't work if you don't have write permissions to the directory
and probably won't be suitable if you're editing through a symbolic link.
$ su -
su: Sorry
$ exit
root@freebsd:~ # head /etc/group 
# $FreeBSD: releng/10.1/etc/group 256366 2013-10-12 06:08:18Z rpaulo $
#
wheel:*:0:root
daemon:*:1:
kmem:*:2:
sys:*:3:
tty:*:4:
operator:*:5:root
mail:*:6:
bin:*:7:
root@freebsd:~ # pw usermod andre -G wheel
root@freebsd:~ # head /etc/group
# $FreeBSD: releng/10.1/etc/group 256366 2013-10-12 06:08:18Z rpaulo $
#
wheel:*:0:root,andre
daemon:*:1:
kmem:*:2:
sys:*:3:
tty:*:4:
operator:*:5:root
mail:*:6:
bin:*:7:
root@freebsd:~ # su - andre
If you have a CD-ROM drive in your machine, you can make the CD-ROM that is
presently inserted available by typing 'mount /cdrom' as root.  The CD-ROM
will be available under /cdrom/.  Remember to do 'umount /cdrom' before
removing the CD-ROM (it will usually not be possible to remove the CD-ROM
without doing this.)

Note: This tip may not work in all configurations.
$ su -
Password:

root@freebsd:~ # 

How to install Htop on Freebsd


How to install Htop

#echo ‘linproc /compat/linux/proc linprocfs rw 0 0’  >> /etc/fstab

#echo ‘linux_enable=“YES”’ >> rc.conf

#mkdir -p /usr/compat/linux/proc 

#ln -s /usr/compat /compat

#mount linproc

#cd /usr/ports/emulatost/linux_base-f10 && make install clean

#pkg install sysutils/lsof

#cd /usr/ports/sysutils/htop && make reinstall clean