Skip to content

Instantly share code, notes, and snippets.

@elcontrastador
Last active August 14, 2017 05:42
Show Gist options
  • Save elcontrastador/1d798aae2b2e5e5ab8af38bd49d566c9 to your computer and use it in GitHub Desktop.
Save elcontrastador/1d798aae2b2e5e5ab8af38bd49d566c9 to your computer and use it in GitHub Desktop.
class FixDHCP
attr_reader :line,
:old_server,
:new_server
def initialize(line, opts)
@line = line
@old_server = opts[:old_server]
@new_server = opts[:new_server]
end
def negate_line
line.sub(/^set/,'delete')
end
def replace_line
line.sub(/#{old_server}/,new_server)
end
def process_line
"#{negate_line}\n#{replace_line}\n"
end
end
old_srv = '10.10.10.10'
new_srv = '10.0.0.1'
ssh = Net::SSH.start(Creds[:host], Creds[:uid], password: Creds[:pwd])
sw = Net::SSH::Telnet.new( 'Timeout' => 10,
'Prompt' => /([#>]\s|-)\z/n,
'Session' => ssh )
sw.cmd('edit')
Hosts.keys.each do |host|
File.open(OutDir + "#{host}.conf","w") do |f|
output = sw.cmd('show | no-more')
f.print output
sleep 3 ### stupid attempt to get stdout to flush
end
output = []
sw.cmd("show system services dhcp | display set | match #{old_srv}") do |c|
next unless c.match(/^(set|delete) /)
output.push(c.chomp)
end
File.open(OutDir + "#{host}.dhcp", "w") do |f|
output.each do |l|
fd = FixDHCP.new(l, old_server: old_srv, new_server: new_srv)
f.print fd.process_line
end
sleep 3 ### stupid attempt to get stdout to flush
end
end
### SAMPLE screwed up output from "*.dhcp" file
display set | match 10.10.
dev@tma-lab-3300-1# ...cp | display set | match 10.10.1 0.10
delete system services dhcp pool 10.0.0.0/8 boot-server 10.10.10.10
set system services dhcp pool 10.0.0.0/8 option 150 array ip-address 10.10.10.10
{master:0}[edit]
dev@tma-lab-3300-1#
display set | match 10.10.
dev@tma-lab-3300-1# ...cp | display set | match 10.10.1 0.10
set system services dhcp pool 10.0.0.0/8 boot-server 10.0.0.1
set system services dhcp pool 10.0.0.0/8 option 150 array ip-address 10.10.10.10
{master:0}[edit]
dev@tma-lab-3300-1#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment