how to connect sql server with linux? - perl

Whenever i will run Perl code i got following error:
DBI connect('SQLSERVER','rti_dbuser',...) failed: [unixODBC][Driver Manager]Data source name not found, and no default driver specified (SQL-IM002) at /usr/WINAST/RTI/AGI/M_TEST.pl line 14
Coudn't Connect SQL at /usr/WINAST/RTI/AGI/M_TEST.pl line 14.
The configuration file are given below:
odbc.ini
[SQLSERVER]
description = Asterisk ODBC for SQL SERVER
;driver = FreeTDS
Driver = /usr/local/lib/libtdsodbc.so
server = 10.2.20.42\APSHCL
Database = RTI
port = 1433
tds_version = 9.0
try domain login = yes
try server login = yes
nt domain = DOMAIN
language = us_english
odbcinst.ini
[FreeTDS]
Description = FreeTDS ODBC driver for MSSQL
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsS.so
FileUsage = 1
My code
use DBI;
use Data::Dumper;
use Asterisk::AGI;
my $agi = new Asterisk::AGI;
$first_param = $ARGV[0];
my $DSN = q/dbi:ODBC:SQLSERVER/;
my $uid = q/username/;
my $pwd = q/password/;
my $DRIVER = "Freetds";
my $dbh = DBI->connect($DSN,$uid,$pwd) or die "Coudn't Connect SQL";
my $servernumber = 2;
my $sql = "EXEC usp_check_black_list '".$first_param."'" ;
my $sth = $dbh->prepare($sql);
$sth->execute();
$return = $sth->fetchrow();
$agi->set_variable('result',$return);
print Dumper($return);
$sth->finish;
$dbh->disconnect;
whenever i'm executing my perl code, the driver is not found.I have already define the driver but it is not use that driver....
please help me...

Try using DBD::Sybase. MSSQL is a fork from the Sybase code, and DBD::Sybase still works pretty well, though YMMV.

Related

Perl Mojolicious Model

I am creating a
Mojolicious
application but I can't understand the documentation for creating a model.
Could someone provide an example of how to create a model, run a raw SQL query and get the results in a controller to pass to the view?
I am thinking of something like this:
Model
package LS::Model::Dt;
use Mojo::Base;
use DBI;
# Here is what I don't understand
# Do I need to create a subroutine that connects to the database like this?
sub connect_db {
my $user = 'user_sql';
my $pass = 'pass_sql';
my $connection = "dbi:Sybase:server=db.sql-srv.com;database=Adventure";
my $dbh = DBI->connect($connection, $user, $pass) or die 'Cannot connect';
}
sub queries{
my $query_selectall = "select * from foo";
my $all_query = $dbh->selectall_arrayref($query_selectall, {Slice => {}});
}
Controller
package LS::Controller::Home;
use Mojo::Base 'Mojolicious::Controller';
use LS::Model::Dt
sub home {
my $self = shift;
# Somehow get the query results here
my $query_res = #somehow get the query results here
$self->render(res=>$query_res);
}
1;
Later edit: I have managed somehow via this tutorial:
http://oliverguenther.de/2014/04/applications-with-mojolicious-part-four-database-schemas-with-dbixclass/
Github code for when you are now sure where the author is creating a file is available here:
https://github.com/oliverguenther/Moblo
If you encounter problems also check this:
Mojolicious Deploying database schema

perl LDAP entry not recognised

We are writing a Perl code (to be run from Unix) which will reset the password of a Windows AD User. (We are not using powershell as we have been asked not to use Windows scripts).
With the following Perl code, we are able to connect to the AD User directory and query the correct user.
#!/usr/bin/perl -w
#########################
#This script resets the password in active user directory
#########################
use strict;
use warnings;
use DBI;
use Net::LDAP;
use Net::LDAPS;
use Authen::SASL qw(Perl);
use Net::LDAP::Control::Paged;
use Time::Local;
my $CERTDIR = "<cert path>";
my $AD_PASS = "$CERTDIR/.VDIAD_pass";
my $sAN = "vahmed";
### Generate Random Password ###
my $randompass = askPasswd();
my $uninewpass;
my $mail;
my $fullname;
my $name;
my $distName;
my $finalresult;
my #AD_passwords = get_domain_pass();
my $result = reset_AD_Password();
#Reset AD user password
sub reset_AD_Password {
my $ad = Net::LDAP->new($AD_passwords[0]);
my $msg = $ad->bind(dn => "cn=$AD_passwords[2],$AD_passwords[1]",
password => $AD_passwords[3],
version => 3);
if ($msg->code)
{
print "Error :" . $msg->error() . "\n";
exit 2;
}
my $acc_name = 'sAMAccountName';
my $acc_fullname = 'displayName';
my $acc_base = 'manager';
my $acc_distName = 'distinguishedName';
my $acc_mail = 'mail';
my $act = $ad->search(
base => "$AD_passwords[1]",
filter => "(&(objectCategory=person)(sAMAccountName=$sAN))",
attrs => [$acc_name, $acc_fullname, $acc_distName, $acc_mail]);
die 1 if ($act->count() !=1 );
my $samdn = $act->entry(0)->dn;
$fullname = $samdn->get_value($acc_fullname);
$mail = $samdn->get_value($acc_mail);
}
}
However we get an error on the line:
$fullname = $samdn->get_value($acc_fullname);
$mail = $samdn->get_value($acc_mail);
The error states "Can't locate object method "get_value" via package (distinguished Name) (perhaps you forgot to load (distinguished Name))"
However the code works correctly when we replace $samdn with the following code:
foreach my $entry ($act->entries){
$name = $entry->get_value($acc_name);
$fullname = $entry->get_value($acc_fullname);
$distName = $entry->get_value($acc_distName);
$mail = $entry->get_value($acc_mail);
}
It would appear that the code is unable to identify $samdn as a Net::LDAP::Entry record.
We have tried typecasting $samdn but got the same error.
Could someone help in resolving this issue as we would not prefer to use the for loop just in case more that one record is returned by the search? Thanks in advance.
You are not assigning a Net::LDAP::Entry to $samdn. You are assigning the dn of the first entry.
# VVVV
my $samdn = $act->entry(0)->dn;
Get rid of that ->dn and it should work, if $act->entry(0) returns a Net::LDAP::Entry.

How can I execute a series of commands using the Net::SSH::Any module?

I want to execute multiple commands in the same session after connecting to a server with Net::SSH::Any.
My sample code is as follows:
use strict;
use warnings;
use Net::SSH::Any;
my $host = "ip address";
my $user = "user";
my $passwd = "pass";
my $cmd1 = 'cd /usr/script';
my $ssh = Net::SSH::Any->new($host, user => $user, password => $passwd);
$ssh->system($cmd1);
my $pwd = $ssh->capture("pwd");
print $pwd;
I expected the following output:
/usr/script
but instead I am getting:
/home/user
How can I execute multiple commands in a single session?
You'll have to chain your commands in the remote shell like this:
my $cwd = $ssh->capture( q{cd /usr/script && pwd} );
You have to do it this way because even though both currently-supported backends to Net::SSH::Any provide other ways to do this (Net::OpenSSH has open2pty and Net::SSH2 has channels), the Net::SSH::Any API doesn't expose these.
For example, system invokes either Net::OpenSSH's system method or creates a Net::SSH2::Channel and invokes process('exec' => $cmd) (limited to one command per channel).

perl sybase connection error on x86pc-solaris

I have a Solaris system with x86 CPU, when I try to connect to sybase db with perl, i got the following error(the error was generated when I run the script in debug mode and step into function DBD::Sybase::db::_login at Sybase.pm in line 94)
ct_con_props(CS_PASSWORD) failed at /usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBD/Sybase.pm line 94.
at /usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBD/Sybase.pm line 94
DBD::Sybase::dr::connect('DBI::dr=HASH(0x8613a5c)', 'server=server1', 'user1, 'password1', 'HASH(0x86b4e5c)') called at /usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBI.pm line 617
DBI::_ANON_/usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBI.pm:679 called at /usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBI.pm line 681
DBI::connect('DBI', 'DBI:Sybase:server=server1', 'user1, 'password1') called at ./test.pl line 28
DBI::CODE(0x83fcdd4)(/usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBI.pm:618):
618: $user = '' if !defined $user;
from the message, it seems there is something wrong with the password? but I am sure the password was correct and the same code works very well on a Spark-solaris system, could anyone tell me what's wrong, or tell me how can I get into the function DBD::Sybase::db::_login($this, $server, $user, $auth, $attr)? looks like this function was in an .xs file, but I can not find where is it, and can not debug into it.
if I run the script withtout debug, i got the following error
ct_con_props(CS_PASSWORD) failed at /usr/local/lib/perl5/site_perl/5.8.8/i86pc-solaris/DBD/Sybase.pm line 94.
DBI connect('server=server1','user1’,...) failed: OpenClient message: LAYER = (1) ORIGIN = (4) SEVERITY = (6) NUMBER = (221)
Server server1, database
Message String: ct__string_extended_encryption: user api layer: internal common library error: error string not available
at ./test.pl line 28
Connect failed at ./test.pl line 28.
here is the code of sub connect in Sybase.pm
sub connect {
my($drh, $dbase, $user, $auth, $attr) = #_;
my $ifile = '';
my $server = $dbase || $ENV{DSQUERY} || 'SYBASE';
my($this) = DBI::_new_dbh($drh, {
'Name' => $server,
'Username' => $user,
'CURRENT_USER' => $user,
});
DBD::Sybase::db::_login($this, $server, $user, $auth, $attr)
or return undef;
return $this;
}
After sending a email to Michael Peppler(the module writer), I got the following answer and the issue was fixed.
The issue is the lib3p directory, which contains libraries that are loaded at run time directly by OpenClient, in particular for things like password encryption, etc.
This directory obviously needs to be in LD_LIBRARY_PATH, or be referenced in the default library search paths.
So basically, I need to add the path of OCS-15 folder to LD_LIBRARY_PATH

File IO on a remote Windows system

Given a Perl script that can be running on unix or Windows, how can I best read/write to a file on a windows host? Is there anything similar to File::Remote?
I would try to mount the remote folder and then use the standard perl functions:
use constant W_REMOTE_FOLDER = '\\server\share';
use constant W_LOCAL_FOLDER = 'x:\share\';
use constant L_REMOTE_FOLDER = 'smb://server/share';
use constant L_LOCAL_FOLDER = '/mnt/share/';
my $localfolder = '';
if ($am_i_windows)
{
system('net use ...');
$localfolder = W_LOCAL_FOLDER;
}
if ($am_i_linux)
{
system('mount ...');
$localfolder = L_LOCAL_FOLDER;
}
die "What am I? if ($localfolder eq '');
open(HANDLE, "$localfolder/$filename");
# read/write (...)
close(HANDLE);