为什么InetAddress.getLocalHost()。getHostName()返回一个与bash“hostname”不同的值? [英] Why does InetAddress.getLocalHost().getHostName() return a value different from bash "hostname"?

查看:114
本文介绍了为什么InetAddress.getLocalHost()。getHostName()返回一个与bash“hostname”不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个build.gradle任务,它在我的开发箱中像一个冠军一样工作,生成一个属性文件,记录生成的机器的名称。逻辑很简单... ...

  def hostname = InetAddress.getLocalHost()。getHostName(); 

在我的开发箱中,这总是会产生与我在bash shell中使用hostname相同的值。 / b>

  bobk-mbp:DM_Server bobk $主机名
bobk-mbp.local

然而,在我们的jenkins CI服务器上,bash主机名返回一个内容,但是我调用InetAddress.getLocalHost()。getHostName();返回别的东西。在jenkins机器上需要更改以使这两个返回相同的值?解析方案

假设你在linux上,使用命名服务(DNS)对服务器的IP地址进行反向查询(DNS),然后使用命名服务(DNS)对服务器的IP地址执行反向查找。 )在您的操作系统中配置。

如果您需要o / s理解的主机名,通过System.getenv从环境变量获取它可能是最简单的选择。这不是一个完全可靠的方法,但它可能已经足够,无需深入研究网络或系统管理员。


I've got a build.gradle task that works like a champ on my dev box at producing a properties file that records the name of the machine that the build was generated on. The logic is simple enough...

def hostname = InetAddress.getLocalHost().getHostName();

On my dev box this always produces the same value as if I did hostname from the bash shell.

bobk-mbp:DM_Server bobk$ hostname
bobk-mbp.local

On our jenkins CI server, however, bash hostname returns one thing, but my call to InetAddress.getLocalHost().getHostName(); returns something else. What needs to change on the jenkins machine to get these two returning the same value?

解决方案

Assuming you're on linux, the hostname command executed from the o/s returns the kernel's configured hostname.

InetAddress.getHostName() is doing a reverse lookup on the server's IP address using the naming service (DNS) configured in your O/S.

If you need the hostname as understood by the o/s, getting it from an environment variable via System.getenv may be the simplest option. It isn't a completely robust way to do this but it may be enough without needing to delve into network or system admin.

这篇关于为什么InetAddress.getLocalHost()。getHostName()返回一个与bash“hostname”不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆