Question

I am using pgScript and I have to pull information about the server that a database is residing on. I know I can get the IP Address and Ports but I need the hostname of the server. Currently I have:

declare @machineName;
declare @I;
set @I = 0;
create temp table foo (t text);
copy foo from '/etc/hosts';
set @machineName = 
    select * from foo;
print @machineName;
set @machineNameCount = select count(*) from foo;

I get back:

("127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4")
("::1         localhost localhost.localdomain localhost6 localhost6.localdomain6")
("xx.xx.x.xx  HOSTNAME")

I know I have three lines to work with, in this case, so I was going to run a while loop over @machineName using @machineNameCount and I just want to remove any line that has "127.0.0.1" or "::1" in it, but I can't figure out how to parse for those. If I can see if a line contains those I can then just remove that line. Any ideas?

Was it helpful?

Solution 2

So I took a different approach to this then looking in the hosts file in /etc; I pulled the file /etc/sysconfig/network and looked specifically for the line that contained 'HOSTNAME'.

create temp table machineNameTable (col1 text);
copy machineNameTable from '/etc/sysconfig/network';
set @machineName = 
    select * from machineNameTable
    where col1 ilike '%HOSTNAME%';
print @machineName;
drop table machineNameTable;

which gives me back:

[PGSCRIPT ] ("HOSTNAME=SERVER_NAME")

Hope this can help someone else.

OTHER TIPS

You can use this bash command for drop lines that contains 127.0.0.1 or ::1 of output file

sed -i -e '/127\.0\.0\.1/d' -e '/::1/d' file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top