Need help with my logic

Hi Experts,

We have quite a lot of servers but they all are like bellow format

(3 server in a pair)

DK-vk-2.domain.lan
DK-vh-2.domain.lan
DK-vm-2.domain.lan

MK-vk-3.domain.lan
MK-vh-4.domain.lan
MK-vm-3.domain.lan

So basically the different is : k,h,v

Now Problme is :
I need to run a script from one of the server and it will connect to
rest 2 by ssh and will run a command, which i know how to do

but i dont know ,how will i know from which server i am running and how
to get hostname of the rest 2 servers.
example :

def name_check(name)
#I Assuming I am running the script from DK-vk-2.domain.lan, So
i need to connect to rest 2 server
@hostname1 = " DK-vm-2.domain.lan "
@hostname2 = " DK-vh-2.domain.lan "

problem is : i need set the hostname1 and hostname2 Automatically
depends on the server i am running the script.

i can get the hostname of the server by using hostname

so it might be
p=`hostname’
puts p
=>DK-vk-2.domain.lan
So if its vk , then rest 2 server will be vm,vh

so the rest of 2 server would be , DK-vm-2.domain.lan ,
DK-vh-2.domain.lan

only differerence is “m” and “h”

other way round
if i run the script from
DK-vh-2.domain.lan , the other 2 server will be
DK-vm-2.domain.lan
DK-vk-2.domain.lan

CAn any one help me …

i am quite stuck now …I would be really grateful if any one can help me
with this

Thanks

server_group = [
“DK-vk-2.domain.lan”,
“DK-vh-2.domain.lan”,
“DK-vm-2.domain.lan”
]

#p = hostname
p = “DK-vk-2.domain.lan”

other_two = server_group - [p]
p other_two

–output:–
[“DK-vh-2.domain.lan”, “DK-vm-2.domain.lan”]

Or:

other_two = server_group.select {|server| server != p}

sharmin malik wrote in post #1075228:

but i dont know ,how will i know from which server i am running

require ‘socket’
myname = Socket.gethostname

and how
to get hostname of the rest 2 servers.

I guess the easiest way is to make the names of all three, and remove
the one you don’t want (i.e. your own)

require ‘socket’
myname = Socket.gethostname
unless myname =~ /\A(\w±v)([khv])(-\d+…+)\z/
raise “Bad hostname format: #{myname.inspect}”
end
hosts = [$1+“k”+$3, $1+“h”+$3, $1+“v”+$3]
unless hosts.delete myname
raise “Can’t find my hostname #{myname.inspect} to remove from
#{hosts.inspect}”
end
hosts.each do |host|
puts “Please connect to #{host}”
end

7stud – wrote in post #1075229:

server_group = [
“DK-vk-2.domain.lan”,
“DK-vh-2.domain.lan”,
“DK-vm-2.domain.lan”
]
Thanks
but i cant define SErver name Statically

WE have quite a lot of server… example 100*3 :300

thats list has to be populate dynamically …

only thing i have is current hostname :
DK-vk-2.domain.lan

then get the 2

DK-vh-2.domain.lan",
DK-vm-2.domain.lan"

like if current host name has vk , then the rest 2 server would be vh
and vm
hope that make sense

given_server = “DK-vk-2.domain.lan”
server_types = [‘vk’, ‘vh’, ‘vm’]

start, type, the_rest = given_server.split(’-’)
puts type

–output:–
vk

other_two = (server_types - [type]).map do |new_type|
[start, new_type, the_rest].join(’-’)
end

p other_two

–output:–
[“DK-vh-2.domain.lan”, “DK-vm-2.domain.lan”]

unless myname =~ /\A(\w±v)([khv])(-\d+…+)\z/
raise “Bad hostname format: #{myname.inspect}”
end
hosts = [$1+“k”+$3, $1+“h”+$3, $1+“v”+$3]
unless hosts.delete myname
raise “Can’t find my hostname #{myname.inspect} to remove from
#{hosts.inspect}”
end
hosts.each do |host|
puts “Please connect to #{host}”
end

HI this is nice way to do …
but system hostname is like this (i forgot put -dk )

dk-vk-2-dk.domain.lan

I tried to change the Regix… but not working
Can you please modify it little bit ??
also would be able to explain me little bit how the regix is related to
[$1+“k”+$3, $1+“h”+$3, $1+“v”+$3] ??
thanks

7stud – wrote in post #1075239:

given_server = “DK-vk-2.domain.lan”
server_types = [‘vk’, ‘vh’, ‘vm’]

start, type, the_rest = given_server.split(’-’)
puts type

–output:–
vk

other_two = (server_types - [type]).map do |new_type|
[start, new_type, the_rest].join(’-’)
end

p other_two

–output:–
[“DK-vh-2.domain.lan”, “DK-vm-2.domain.lan”]

Hi this works fine
thanks

7stud – wrote in post #1075239:

given_server = “DK-vk-2.domain.lan”
server_types = [‘vk’, ‘vh’, ‘vm’]

start, type, the_rest = given_server.split(’-’)
puts type

–output:–
vk

other_two = (server_types - [type]).map do |new_type|
[start, new_type, the_rest].join(’-’)
end

p other_two

–output:–
[“DK-vh-2.domain.lan”, “DK-vm-2.domain.lan”]

Hi
also could you please explain to me , how it working ??

other_two = (server_types - [type]).map do |new_type|
[start, new_type, the_rest].join(’-’)
end

how it deleting vk from the list ??

sharmin malik писал 10.09.2012 01:55:

end
[$1+“k”+$3, $1+“h”+$3, $1+“v”+$3] ??
thanks

Parts of strings which are matched by capturing groups (parenthesized
groups of symbols in regexen) are assigned to $-variables. For example,
look at this code:

“test 123 abc” =~ /test (…)3 ([ba]+)/
puts “$1,$2” # outputs 12,ab

The $N numeric variables are function-local and always refer to the
last
regex executed in the current function. There is an effectively
infinite
number of them.

You can make regex groups non-capturing by placing ?: in the beginning
of
the group. E.g. ([ba]+) is capturing, and (?:[ba]+) is non-capturing.
Non-capturing groups behave in exactly same way as capturing ones, but
they do not have a corresponding $N-variable.

sharmin malik wrote in post #1075245:

7stud – wrote in post #1075239:

given_server = “DK-vk-2.domain.lan”
server_types = [‘vk’, ‘vh’, ‘vm’]

start, type, the_rest = given_server.split(’-’)
puts type

–output:–
vk

other_two = (server_types - [type]).map do |new_type|
[start, new_type, the_rest].join(’-’)
end

p other_two

–output:–
[“DK-vh-2.domain.lan”, “DK-vm-2.domain.lan”]

Hi
also could you please explain to me , how it working ??

other_two = (server_types - [type]).map do |new_type|
[start, new_type, the_rest].join(’-’)
end

how it deleting vk from the list ??

p server_types - [type]

–output:–
[“vh”, “vm”]

And it’s more efficient to just do this:

other_two = (server_types - [type]).map do |new_type|
“#{start}-#{new_type}-#{the_rest}”
end

Perhaps this is clearer:

given_server = “DK-vk-2.domain.lan”
server_types = [‘vk’, ‘vh’, ‘vm’]

start, type, the_rest = given_server.split(’-’)
puts type

–output:–
vk

other_two_types = server_types - [type]
p other_two_types

–output:–
[“vh”, “vm”]

other_two_servers = (other_two_types).map do |type|
“#{start}-#{type}-#{the_rest}”
end

p other_two_servers

–output:–
[“DK-vh-2.domain.lan”, “DK-vm-2.domain.lan”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs