Python查找并替换具有计算机主机名的通用HOSTID

时间:2018-05-24 13:59:18

标签: python macos terminal hostname

我需要“HOSTID”来解析机器主机名。我的目标是使用这个简单的命令,在终端中手动运行,并使其在Python脚本中可移植。

Sudo /library.nessusagent.run.sbin.nessuscli agent link -- 
key=5e30508800865f87a8dbe8993fa75d21f9e2acc7db12165050cf48b5ccbafb84 -- 
name=HOSTID --groups=Mac_OS_Systems --Host=Cloud.tenable.com --port=443

以下代码将为我提供HOSTID,如何将其指向正确的位置?

import platform
platform.node() 

1 个答案:

答案 0 :(得分:0)

这应该有效:

import platform, subprocess

# Get the HOSTID
host_id = str(platform.node())

# Make the command string and replace HOSTID with host_id's value
command = "sudo /library.nessusagent.run.sbin.nessuscli agent link -- key=5e30508800865f87a8dbe8993fa75d21f9e2acc7db12165050cf48b5ccbafb84 -- name={0} --groups=Mac_OS_Systems --Host=Cloud.tenable.com --port=443".format(host_id)

# Run the command
execute = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)

# Save the output
out, err = execute.communicate()

# Print the output (assuming there is an output)
print (out)