Home > Archive > PERL Beginners > February 2005 > Environment variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Environment variables
|
|
| Jason Wozniak 2005-02-21, 8:56 pm |
| Hello,
I'm trying to write a paging script that connects to several
databases in succession, and was attempting to write the script such
that I would not have to hard code any user names, or passwords, by
using an externally identified user.
I can connect to the first database in the list ok, but I can't figure
out how to change my ORACLE_SID environment variable, so it simply
reconnects to the same database each time through the loop for every sid
in the file. I tried setting $ENV{ORACLE_SID} to no effect. I even
tried using:
System("export ORACLE_SID = $_"); and verified $_ was being read in
correctly, as seen below. Is there no way to modify your shell
environment from within a perl program? I saw a module called
Env-Bash-0.04, but it doesn't seem to say if you can update an
environment variable, it just makes sure you can access them all.
use DBI;
my $database;
my $address = "xxxxxxxxxx\@page.nextel.com";
my %attr;
my $dbh;
open MAIL, "|mail $address";
open DBFILE, "</u01/app/oracle/check_list.txt";
while (<DBFILE> ) {
system("export ORACLE_SID = $_");
$dbh = DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
$DBI::errstr\n";
}
close MAIL;
| |
| Jason Wozniak 2005-02-21, 8:56 pm |
| That's what I thought, but it doesn't work, which is why I tried system.
The below code:
use DBI;
my $database;
#my $address =3D "2157788078\@page.nextel.com";
my $address =3D "jwozniak\@henkels.com";
my %attr;
my $dbh;
open MAIL, "|mail $address";
open DBFILE, "</u01/app/oracle/check_list.txt";
while (<DBFILE> ) {
$ENV{ORACLE_SID} =3D=3D chomp($_);
print "$ENV{ORACLE_SID}\n";
$dbh =3D DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
$DBI::errstr\n";
}
close MAIL;
produces the following output:
P01
P01
P01
P01
P01
The file /u01/app/oracle/check_list.txt contains several different sids,
and if I print $_ it is reading them in.
-----Original Message-----
From: Scott Pham [mailto:scott.pham@gmail.com]=20
Sent: Monday, February 21, 2005 4:05 PM
To: Jason Wozniak
Subject: Re: Environment variables
You want use ENV hash (%ENV). The reason why system or `` doesn't work
is due to them being forked off.
$ENV{ORACLE_SID} =3D "whateva";
will do it for you.
On Mon, 21 Feb 2005 15:51:06 -0500, Jason Wozniak <jwozniak@henkels.com>
wrote:
> Hello,
>=20
> I'm trying to write a paging script that connects to
several
> databases in succession, and was attempting to write the script such
> that I would not have to hard code any user names, or passwords, by
> using an externally identified user.
>=20
> I can connect to the first database in the list ok, but I can't figure
> out how to change my ORACLE_SID environment variable, so it simply
> reconnects to the same database each time through the loop for every
sid
> in the file. I tried setting $ENV{ORACLE_SID} to no effect. I even
> tried using:
>=20
> System("export ORACLE_SID =3D $_"); and verified $_ was being read in
> correctly, as seen below. Is there no way to modify your shell
> environment from within a perl program? I saw a module called
> Env-Bash-0.04, but it doesn't seem to say if you can update an
> environment variable, it just makes sure you can access them all.
>=20
> use DBI;
>=20
> my $database;
>=20
> my $address =3D "xxxxxxxxxx\@page.nextel.com";
>=20
> my %attr;
>=20
> my $dbh;
>=20
> open MAIL, "|mail $address";
>=20
> open DBFILE, "</u01/app/oracle/check_list.txt";
>=20
> while (<DBFILE> ) {
>=20
> system("export ORACLE_SID =3D $_");
>=20
> $dbh =3D DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
>=20
> or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
> $DBI::errstr\n";
>=20
> }
>=20
> close MAIL;
>=20
>
| |
| Graeme St. Clair 2005-02-21, 8:56 pm |
| The following works for me on a Windows XP box talking to a Solaris server:-
BEGIN {
if (($^O eq 'MSWin32') or ($^O =~ /cygwin/i)) {
# $ENV{ORACLE_HOME} = q{C:/Oracle/Ora81};
# But Oracle::DBD will find it from the Windows registry anyway
} else {
$ENV{ORACLE_HOME} = q{/path/to/product/8.1.7};
}
$host = q{BLAH.BLAH.COM};
.....
eval { ...
$dbh = DBI->connect("dbi:Oracle:$host", $uid, $pw)
or die qq{Unable to connect to $host:<br/>$DBI::errstr\n};
...
There is some Oracle s/w pre-installed on my XP box about which I know
nothing except that I can see the magic string BLAH.BLAH.COM inside
somewhere. I've never tried disconnecting and reconnecting to some other
d-b.
HTH, GStC.
-----Original Message-----
From: Jason Wozniak [mailto:jwozniak@henkels.com]
Sent: Monday, February 21, 2005 4:15 PM
To: beginners@perl.org
Subject: RE: Environment variables
That's what I thought, but it doesn't work, which is why I tried system.
The below code:
use DBI;
my $database;
#my $address = "2157788078\@page.nextel.com"; my $address =
"jwozniak\@henkels.com"; my %attr; my $dbh; open MAIL, "|mail $address";
open DBFILE, "</u01/app/oracle/check_list.txt";
while (<DBFILE> ) {
$ENV{ORACLE_SID} == chomp($_);
print "$ENV{ORACLE_SID}\n";
$dbh = DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
$DBI::errstr\n"; } close MAIL;
produces the following output:
P01
P01
P01
P01
P01
The file /u01/app/oracle/check_list.txt contains several different sids, and
if I print $_ it is reading them in.
-----Original Message-----
From: Scott Pham [mailto:scott.pham@gmail.com]
Sent: Monday, February 21, 2005 4:05 PM
To: Jason Wozniak
Subject: Re: Environment variables
You want use ENV hash (%ENV). The reason why system or `` doesn't work is
due to them being forked off.
$ENV{ORACLE_SID} = "whateva";
will do it for you.
On Mon, 21 Feb 2005 15:51:06 -0500, Jason Wozniak <jwozniak@henkels.com>
wrote:
> Hello,
>
> I'm trying to write a paging script that connects to
several
> databases in succession, and was attempting to write the script such
> that I would not have to hard code any user names, or passwords, by
> using an externally identified user.
>
> I can connect to the first database in the list ok, but I can't figure
> out how to change my ORACLE_SID environment variable, so it simply
> reconnects to the same database each time through the loop for every
sid
> in the file. I tried setting $ENV{ORACLE_SID} to no effect. I even
> tried using:
>
> System("export ORACLE_SID = $_"); and verified $_ was being read in
> correctly, as seen below. Is there no way to modify your shell
> environment from within a perl program? I saw a module called
> Env-Bash-0.04, but it doesn't seem to say if you can update an
> environment variable, it just makes sure you can access them all.
>
> use DBI;
>
> my $database;
>
> my $address = "xxxxxxxxxx\@page.nextel.com";
>
> my %attr;
>
> my $dbh;
>
> open MAIL, "|mail $address";
>
> open DBFILE, "</u01/app/oracle/check_list.txt";
>
> while (<DBFILE> ) {
>
> system("export ORACLE_SID = $_");
>
> $dbh = DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
>
> or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
> $DBI::errstr\n";
>
> }
>
> close MAIL;
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
commands, e-mail: beginners-help@perl.org <http://learn.perl.org/>
<http://learn.perl.org/first-response>
| |
|
| On Mon, 21 Feb 2005 16:14:43 -0500, Jason Wozniak <jwozniak@henkels.com> wrote:
> That's what I thought, but it doesn't work, which is why I tried system.
>
> The below code:
>
> use DBI;
>
> my $database;
> #my $address = "2157788078\@page.nextel.com";
> my $address = "jwozniak\@henkels.com";
> my %attr;
> my $dbh;
> open MAIL, "|mail $address";
> open DBFILE, "</u01/app/oracle/check_list.txt";
>
> while (<DBFILE> ) {
> $ENV{ORACLE_SID} == chomp($_);
> print "$ENV{ORACLE_SID}\n";
> $dbh = DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
> or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
> $DBI::errstr\n";
> }
> close MAIL;
>
> produces the following output:
>
> P01
> P01
> P01
> P01
> P01
>
> The file /u01/app/oracle/check_list.txt contains several different sids,
> and if I print $_ it is reading them in.
>
Jason,
I'm not sure about this, but it seems to me that ORACLE_SID is
probably set by some system process after you log into the database,
and can't be reset by the user. Have you tried resetting from the
shell using export (or whatever command is appropriate for your
shell)? Allowing what you're trying to do--a combination of
passwordless login and masquerading as multiple users--would be a very
insecure. I certainly wouldn't want my data stored anywhere you could
get away with this.
That said, try actually assigning the variable in the loop. "==" is a
test for numerical equality. "=" is for variable assignment. Turn on
use strict and use warnings to help you clean up your code; you may
find there aren't as many problems as you think there are. On the
other hand, the shell has read only variables; this may be one of
them. Getting this to work may require writing the script to run as
root (or the oracle user) using Perl::IPC or similar to manipulate the
tty/pty info. You'll probably have to feed it passwords at some
point, though. A server that just takes your word for your user id is
a server you want to steer very clear of.
--jay
| |
| Jason Wozniak 2005-02-23, 3:56 am |
| The following works:
use DBI;
my $database;
my $address =3D "jwozniak\@henkels.com";
my %attr;
my $dbh;
my $sid;
open MAIL, "|mail $address";
open DBFILE, "</u01/app/oracle/check_list.txt";
while ($sid =3D <DBFILE> ) {
chomp($sid);
$ENV{ORACLE_SID} =3D $sid;
print "$ENV{ORACLE_SID}\n";
$dbh =3D DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
$DBI::errstr\n";
}
close MAIL;
The =3D=3D was wrong, I couldn't remember the rule, and =3D produced a =
result
of 1, ie. I realized now the chomp evaluated to true or 1. I'm not
masquerading as multiple users, only 1 user externally identified. This
simply means the user is authenticated by the OS, and not the database.
This also allows the script to be run by any externally identified user.
Once connected normal database security takes over.
The problem now is that the script won't connect to any remote
databases. I assume this is probably because of the external
identification. While the oracle user is externally identified on all
servers, I probably have to log in to each server before connecting.
-----Original Message-----
From: Jay [mailto:daggerquill@gmail.com]=20
Sent: Tuesday, February 22, 2005 10:41 AM
To: Jason Wozniak
Cc: beginners@perl.org
Subject: Re: Environment variables
On Mon, 21 Feb 2005 16:14:43 -0500, Jason Wozniak <jwozniak@henkels.com>
wrote:
> That's what I thought, but it doesn't work, which is why I tried
system.
>=20
> The below code:
>=20
> use DBI;
>=20
> my $database;
> #my $address =3D "2157788078\@page.nextel.com";
> my $address =3D "jwozniak\@henkels.com";
> my %attr;
> my $dbh;
> open MAIL, "|mail $address";
> open DBFILE, "</u01/app/oracle/check_list.txt";
>=20
> while (<DBFILE> ) {
> $ENV{ORACLE_SID} =3D=3D chomp($_);
> print "$ENV{ORACLE_SID}\n";
> $dbh =3D DBI->connect( "dbi:Oracle:$database", "", "", \%attr)
> or print MAIL "Could not connect to database: $ENV{ORACLE_SID}
> $DBI::errstr\n";
> }
> close MAIL;
>=20
> produces the following output:
>=20
> P01
> P01
> P01
> P01
> P01
>=20
> The file /u01/app/oracle/check_list.txt contains several different
sids,
> and if I print $_ it is reading them in.
>=20
Jason,
I'm not sure about this, but it seems to me that ORACLE_SID is
probably set by some system process after you log into the database,
and can't be reset by the user. Have you tried resetting from the
shell using export (or whatever command is appropriate for your
shell)? Allowing what you're trying to do--a combination of
passwordless login and masquerading as multiple users--would be a very
insecure. I certainly wouldn't want my data stored anywhere you could
get away with this.
That said, try actually assigning the variable in the loop. "=3D=3D" is =
a
test for numerical equality. "=3D" is for variable assignment. Turn on
use strict and use warnings to help you clean up your code; you may
find there aren't as many problems as you think there are. On the
other hand, the shell has read only variables; this may be one of
them. Getting this to work may require writing the script to run as
root (or the oracle user) using Perl::IPC or similar to manipulate the
tty/pty info. You'll probably have to feed it passwords at some
point, though. A server that just takes your word for your user id is
a server you want to steer very clear of.
--jay
|
|
|
|
|