Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,33 @@ public Answer execute(final GetVmIpAddressCommand command, final LibvirtComputin
}
String sanitizedVmName = sanitizeBashCommandArgument(vmName);
String networkCidr = command.getVmNetworkCidr();
List<String[]> commands = new ArrayList<>();
final String virt_ls_path = Script.getExecutableAbsolutePath("virt-ls");
final String virt_cat_path = Script.getExecutableAbsolutePath("virt-cat");
final String virt_win_reg_path = Script.getExecutableAbsolutePath("virt-win-reg");
final String tail_path = Script.getExecutableAbsolutePath("tail");
final String grep_path = Script.getExecutableAbsolutePath("grep");
final String awk_path = Script.getExecutableAbsolutePath("awk");
final String sed_path = Script.getExecutableAbsolutePath("sed");
if(!command.isWindows()) {
final String virsh_path = Script.getExecutableAbsolutePath("virsh");

// first run virsh domiflist to get the network interface name from libvirt. This is set if qemu guest agent is running in the VM
// and is the most reliable and least intrusive
List<String[]> commands = new ArrayList<>();
commands.add(new String[]{virsh_path, "domifaddr", sanitizedVmName, "--source", "agent"});
String output = Script.executePipedCommands(commands, 0).second();
if (output != null) {
String[] lines = output.split("\n");
for (String line : lines) {
ip = parseDomIfListOutput(line, networkCidr);
if (ip != null) {
break;
}
}
}
Comment thread
rg9975 marked this conversation as resolved.
Outdated

commands.clear();
commands.add(new String[]{grep_path, ".*\\*"});
if(ip == null && !command.isWindows()) {
Comment thread
rg9975 marked this conversation as resolved.
Outdated
//List all dhcp lease files inside guestVm
commands.add(new String[]{virt_ls_path, sanitizedVmName, "/var/lib/dhclient/"});
commands.add(new String[]{grep_path, ".*\\*.leases"});
Expand Down Expand Up @@ -106,4 +124,20 @@ public Answer execute(final GetVmIpAddressCommand command, final LibvirtComputin
}
return new Answer(command, result, ip);
}

private String parseDomIfListOutput(String line, String networkCidr) {
String ip = null;
if (line.contains("ipv4")) {
String[] parts = line.split(" ");
if (parts.length > 2) {
String[] ipParts = parts[2].split("/");
if (ipParts.length > 0) {
if (NetUtils.isIpWithInCidrRange(ipParts[0], networkCidr)) {
ip = ipParts[0];
}
}
}
}
return ip;
}
}