#!/usr/bin/env ruby #-- # Name : vm_mac_fixer # Author : Jerod Santo # Contact : "moc.liamg@otnas.dorej".reverse # Date : 2008 September 15 # About : Debian-based vmware virtual machines lose their eth0 after being copied to a new # host. This script will check to see if a machine has lost its eth0, fix it, and reboot #-- if_status = `ifconfig eth0 2>&1` # 2>&1 redirects stderr to stdout so we can capture it config_file = "/etc/udev/rules.d/z25_persistent-net.rules" if if_status =~ /Device not found/ config_text = Array.new File.open(config_file,"r") { |file| config_text = file.readlines } relevant_text = config_text.select { |line| line =~ /^SUBSYSTEM==/ } output = relevant_text.last.gsub(/eth\d/,"eth0") File.open(config_file,"w"){ |file| file.puts output } system("reboot") end