1
0
mirror of https://git.baguette.netlib.re/Baguette/networkctl synced 2024-12-18 13:03:30 +00:00

dhcp and static IP configuration working

This commit is contained in:
Philippe PITTOLI 2019-10-07 20:22:20 +02:00
parent ec6b9f4c29
commit a4649261f0

View File

@ -61,8 +61,10 @@ OptionParser.parse! do |parser|
case command case command
when /^(list)/ when /^(list)/
STDERR.puts "TODO: list" unless arg.empty? STDERR.puts "TODO: list" unless arg.empty?
when /^(connect)/ when /^(up)/
STDERR.puts "TODO: connect" unless arg.empty? STDERR.puts "TODO: up" unless arg.empty?
when /^(down)/
STDERR.puts "TODO: down" unless arg.empty?
else else
STDERR.puts "Command #{command} not understood" STDERR.puts "Command #{command} not understood"
end end
@ -112,7 +114,7 @@ class NetworkCommands


class IWCommand class IWCommand
def self.get_ssid(ifname : String, ssid) def self.get_ssid(ifname : String, ssid)
unless Do.run(cmd, [ name ]).success? unless Do.run(cmd, [ ifname ]).success?
raise "(#{cmd}) dhcp failed on #{ifname}" raise "(#{cmd}) dhcp failed on #{ifname}"
end end
end end
@ -120,20 +122,16 @@ class NetworkCommands


class UDHCPCCommand class UDHCPCCommand
def self.run(ifname : String) def self.run(ifname : String)
# TODO: verify which dhcp client is installed on the system unless Do.run("udhcpc", [ ifname ]).success?
cmd = "udhcpc" raise "(udhcpc) dhcp failed on #{ifname}"
unless Do.run(cmd, [ name ]).success?
raise "(#{cmd}) dhcp failed on #{ifname}"
end end
end end
end end


class DHClientCommand class DHClientCommand
def self.run(ifname : String) def self.run(ifname : String)
# TODO: verify which dhcp client is installed on the system unless Do.run("dhclient", [ ifname ]).success?
cmd = "udhcpc" raise "(dhclient) dhcp failed on #{ifname}"
unless Do.run(cmd, [ name ]).success?
raise "(#{cmd}) dhcp failed on #{ifname}"
end end
end end
end end
@ -156,7 +154,7 @@ class NetworkCommands
end end


def self.set_ip(name : String, ip : IPAddress) def self.set_ip(name : String, ip : IPAddress)
unless Do.run("ifconfig", [ name, "add", ip.to_string ]).success? unless Do.run("ifconfig", [ name, ip.to_string ]).success?
raise "(ifconfig) Cannot set ip address #{ip.to_string} for #{name}" raise "(ifconfig) Cannot set ip address #{ip.to_string} for #{name}"
end end
end end
@ -164,8 +162,8 @@ class NetworkCommands
# currently, aliasses with ifconfig: ifconfig add ip/mask # currently, aliasses with ifconfig: ifconfig add ip/mask
# same command as the ip setup # same command as the ip setup
def self.set_alias(name : String, ip : IPAddress) def self.set_alias(name : String, ip : IPAddress)
unless Do.run("ifconfig", [ name, "add", ip.to_string ]).success? unless Do.run("ifconfig", [ name, "add", ip.to_s ]).success?
raise "(ifconfig) Cannot set ip address alias #{ip.to_string} for #{name}" raise "(ifconfig) Cannot set ip address alias #{ip.to_s} for #{name}"
end end
end end


@ -180,6 +178,18 @@ class NetworkCommands
raise "(ifconfig) Cannot set description #{description} for #{name}" raise "(ifconfig) Cannot set description #{description} for #{name}"
end end
end end

def self.flush(name : String)
unless Do.run("ifconfig", [ name, "0.0.0.0" ]).success?
raise "(ifconfig) Cannot flush #{name} ip addresses"
end
end

def self.down(name : String)
unless Do.run("ifconfig", [ name, "down" ]).success?
raise "(ifconfig) Cannot set down #{name}"
end
end
end end


class IPCommand class IPCommand
@ -217,6 +227,18 @@ class NetworkCommands
end end
end end


def self.flush(name : String)
unless Do.run("ip", [ "address", "flush", "dev", name ]).success?
raise "(ip) Cannot flush #{name} ip addresses"
end
end

def self.down(name : String)
unless Do.run("ip", [ "link", "set", "down", "dev", name ]).success?
raise "(ip) Cannot set down #{name}"
end
end

def self.description(name : String, description : String) def self.description(name : String, description : String)
puts "TODO: (ip) setup description '#{description}' to interface #{name}" puts "TODO: (ip) setup description '#{description}' to interface #{name}"
# unless Do.run("ip", [ "link", "set", "description", description, "dev", name ]).success? # unless Do.run("ip", [ "link", "set", "description", description, "dev", name ]).success?
@ -244,9 +266,9 @@ class NetworkCommands
def self.dhcp(name : String) def self.dhcp(name : String)
cmd = @@cmd_dhcp_client cmd = @@cmd_dhcp_client
case cmd case cmd
when NotSetup when NotSetup.class
puts "no dhcp client: cannot perform dhcp on #{name}" puts "no dhcp client: cannot perform dhcp on #{name}"
when UDHCPCCommand else
cmd.run name cmd.run name
end end
end end
@ -263,12 +285,20 @@ class NetworkCommands
@@cmd_network_configuration.description name, description @@cmd_network_configuration.description name, description
end end


def self.flush(name : String)
@@cmd_network_configuration.flush name
end

def self.down(name : String)
@@cmd_network_configuration.down name
end

def self.wireless_list_ssid(ifname : String) def self.wireless_list_ssid(ifname : String)
cmd = @@cmd_wireless_configuration cmd = @@cmd_wireless_configuration
case cmd case cmd
when NotSetup when NotSetup.class
puts "no wireless configuration program: cannot list ssid" puts "no wireless configuration program: cannot list ssid"
when IWCommand when IWCommand.class
cmd.list_ssid ifname cmd.list_ssid ifname
end end
end end
@ -276,9 +306,9 @@ class NetworkCommands
def self.wireless_connect_wpa_psk(ifname : String, ssid : String, passwd : String) def self.wireless_connect_wpa_psk(ifname : String, ssid : String, passwd : String)
cmd = @@cmd_wireless_configuration cmd = @@cmd_wireless_configuration
case cmd case cmd
when NotSetup when NotSetup.class
puts "no wireless configuration program: cannot connect to ssid #{ssid}" puts "no wireless configuration program: cannot connect to ssid #{ssid}"
when IWCommand when IWCommand.class
cmd.list_ssid ifname cmd.list_ssid ifname
end end
end end
@ -489,7 +519,7 @@ class InterfaceConfiguration
when DHCP when DHCP
NetworkCommands.dhcp name NetworkCommands.dhcp name
when NotSetup when NotSetup
puts "no ipv4" # do nothing
else else
raise "ipv4 configuration: neither static nor dynamic" raise "ipv4 configuration: neither static nor dynamic"
end end
@ -514,7 +544,7 @@ class InterfaceConfiguration
#when DHCP #when DHCP
# NetworkCommands.dhcp6 name # NetworkCommands.dhcp6 name
when NotSetup when NotSetup
puts "no ipv6" # do nothing
else else
raise "ipv4 configuration: neither static nor dynamic" raise "ipv4 configuration: neither static nor dynamic"
end end
@ -527,6 +557,15 @@ class InterfaceConfiguration
end end
end end
end end

def down
unless NetworkCommands.interface_exists(@name)
raise "The interface #{@name} doesn't exists or is already down."
end

NetworkCommands.flush name
NetworkCommands.down name
end
end end


class NetworkConfigurationParser class NetworkConfigurationParser
@ -807,8 +846,12 @@ case command
when "list" when "list"
# TODO: why having to force "not_nil!" ? Seems like a compiler bug # TODO: why having to force "not_nil!" ? Seems like a compiler bug
puts NetworkConfigurationParser.parse_file(file.not_nil!) puts NetworkConfigurationParser.parse_file(file.not_nil!)
when "connect" when "up"
# TODO: why having to force "not_nil!" ? Seems like a compiler bug # TODO: why having to force "not_nil!" ? Seems like a compiler bug
network_configuration = NetworkConfigurationParser.parse_file(file.not_nil!) network_configuration = NetworkConfigurationParser.parse_file(file.not_nil!)
network_configuration.execute network_configuration.execute
when "down"
# TODO: why having to force "not_nil!" ? Seems like a compiler bug
network_configuration = NetworkConfigurationParser.parse_file(file.not_nil!)
network_configuration.down
end end