Ruby:使用脚本内的外部脚本进行比较

发布于 01-06 09:45 字数 631 浏览 2 评论 0原文

所以我得到了一个脚本 (A),它为新的虚拟服务器找到合适的 IP 地址。首先,它会查看数据库,看看他选择的第一个 IP 是否尚未被其他服务器占用。如果该 IP 尚未使用,脚本会对其执行 ping 操作。如果 ping 没有响应,那么我们将进入下一步,这就是我遇到问题的地方。

在下一步中,我必须检查 IP 地址是否已在 Netscaler(路由器)中注册。为此,我必须在同一台计算机上使用另一个脚本 (B)。这个其他脚本返回 netscaler 中定义的所有 ip 的列表。当我运行它时,输出看起来像这样,

x.x.x.x
x.x.x.x
x.x.x.x (and so on..).

我发现了很多从脚本 A 中执行脚本 B 的方法,但我发现的方法都不允许我做我想做的事情。

我的目标是将我的脚本找到的 IP 与列出的所有 IP 进行比较,而不将最后的 IP 打印在屏幕上。

因此,为了更清楚一点,假设脚本 A 找到了 IP: 1.2.3.4

然后它会调用脚本 B,脚本 B 将这个列表返回给脚本 A

1.2.3.5 1.2.4.5 1.2.5.1 等等。

然后 A 会将 1.2.3.4 与脚本 B 返回的所有内容进行比较,而不实际将它们显示在屏幕上。

非常感谢!

So I got a script (A) that finds a suitable IP address for a new virtual server. At first, it takes a look in the database to see if the first ip he chose isn't already taken by another server. If the IP is not already in use, the script pings it. If there is no response from the ping, then we get to the next step and this is where I'm having a problem.

In the next step, I have to check if the IP address is already registred in the netscaler (router) or not. To do this, I must use another script on the same machine (B). This other script return the list of all the ips defined in the netscaler. When I run it, the output looks like this

x.x.x.x
x.x.x.x
x.x.x.x (and so on..).

I found many ways to execute the script B from whiting the script A, but none of what I found allow me to do what I'd like to.

My goal is to compare the ip my script found with all of those that are listed, without having those last ones printed on the screen.

So, to make it a bit clearer, let's say that the scrip A found the IP : 1.2.3.4

It would then call script B that would return to script A this list

1.2.3.5
1.2.4.5
1.2.5.1
and so on.

and then A would compare 1.2.3.4 with all those returned by script B without actually showing them on screen.

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市2025-01-13 09:45:04

我会将 scriptB 业务逻辑与 scriptB ui (CLI) 逻辑分开:

scriptA.rb
scriptB.rb
netscaler.rb # extract logic here

将 netscaler 中定义的所有 ip 列表提取到单独的类中 逻辑/方法:

#netscaler.rb
class Netscaler
  def self.list_ips
    # return array of ips here
  end
end

#scriptB.rb
require_relative 'netscaler'

ips = Netscaler.list_ips
puts ips # script B may show these ips on the screen
...    

#scriptA.rb
require_relative 'netscaler'

ips = Netscaler.list_ips
# script A will not show them. Instead it will operate on the returned result.
...

I would separate scriptB business logic from scriptB ui (CLI) logic:

scriptA.rb
scriptB.rb
netscaler.rb # extract logic here

Extract your list of all the ips defined in the netscaler logic into separate class/method:

#netscaler.rb
class Netscaler
  def self.list_ips
    # return array of ips here
  end
end

#scriptB.rb
require_relative 'netscaler'

ips = Netscaler.list_ips
puts ips # script B may show these ips on the screen
...    

#scriptA.rb
require_relative 'netscaler'

ips = Netscaler.list_ips
# script A will not show them. Instead it will operate on the returned result.
...
待"谢繁草2025-01-13 09:45:04

您可以使用反引号来执行脚本 B 并返回输出:

ip_list = `scriptB`.split("\n")

这可以插入到 Alex 的组织建议中。如果脚本 B 是您无法控制的非 Ruby 脚本,我会这样做。

请注意,如果有任何前导或尾随空格,您可以在末尾添加 .map(&:strip)

You can use backticks to execute script B and return the output:

ip_list = `scriptB`.split("\n")

This can be plugged into Alex's organizational suggestion. I would do this if script B is a non-Ruby script that you don't control.

Note that if there is any leading or trailing whitespace you can add a .map(&:strip) to the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文