dhcp and static IP configuration working
This commit is contained in:
parent
ec6b9f4c29
commit
a4649261f0
89
src/main.cr
89
src/main.cr
@ -61,8 +61,10 @@ OptionParser.parse! do |parser|
|
||||
case command
|
||||
when /^(list)/
|
||||
STDERR.puts "TODO: list" unless arg.empty?
|
||||
when /^(connect)/
|
||||
STDERR.puts "TODO: connect" unless arg.empty?
|
||||
when /^(up)/
|
||||
STDERR.puts "TODO: up" unless arg.empty?
|
||||
when /^(down)/
|
||||
STDERR.puts "TODO: down" unless arg.empty?
|
||||
else
|
||||
STDERR.puts "Command #{command} not understood"
|
||||
end
|
||||
@ -112,7 +114,7 @@ class NetworkCommands
|
||||
|
||||
class IWCommand
|
||||
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}"
|
||||
end
|
||||
end
|
||||
@ -120,20 +122,16 @@ class NetworkCommands
|
||||
|
||||
class UDHCPCCommand
|
||||
def self.run(ifname : String)
|
||||
# TODO: verify which dhcp client is installed on the system
|
||||
cmd = "udhcpc"
|
||||
unless Do.run(cmd, [ name ]).success?
|
||||
raise "(#{cmd}) dhcp failed on #{ifname}"
|
||||
unless Do.run("udhcpc", [ ifname ]).success?
|
||||
raise "(udhcpc) dhcp failed on #{ifname}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class DHClientCommand
|
||||
def self.run(ifname : String)
|
||||
# TODO: verify which dhcp client is installed on the system
|
||||
cmd = "udhcpc"
|
||||
unless Do.run(cmd, [ name ]).success?
|
||||
raise "(#{cmd}) dhcp failed on #{ifname}"
|
||||
unless Do.run("dhclient", [ ifname ]).success?
|
||||
raise "(dhclient) dhcp failed on #{ifname}"
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -156,7 +154,7 @@ class NetworkCommands
|
||||
end
|
||||
|
||||
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}"
|
||||
end
|
||||
end
|
||||
@ -164,8 +162,8 @@ class NetworkCommands
|
||||
# currently, aliasses with ifconfig: ifconfig add ip/mask
|
||||
# same command as the ip setup
|
||||
def self.set_alias(name : String, ip : IPAddress)
|
||||
unless Do.run("ifconfig", [ name, "add", ip.to_string ]).success?
|
||||
raise "(ifconfig) Cannot set ip address alias #{ip.to_string} for #{name}"
|
||||
unless Do.run("ifconfig", [ name, "add", ip.to_s ]).success?
|
||||
raise "(ifconfig) Cannot set ip address alias #{ip.to_s} for #{name}"
|
||||
end
|
||||
end
|
||||
|
||||
@ -180,6 +178,18 @@ class NetworkCommands
|
||||
raise "(ifconfig) Cannot set description #{description} for #{name}"
|
||||
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
|
||||
|
||||
class IPCommand
|
||||
@ -217,6 +227,18 @@ class NetworkCommands
|
||||
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)
|
||||
puts "TODO: (ip) setup description '#{description}' to interface #{name}"
|
||||
# unless Do.run("ip", [ "link", "set", "description", description, "dev", name ]).success?
|
||||
@ -244,9 +266,9 @@ class NetworkCommands
|
||||
def self.dhcp(name : String)
|
||||
cmd = @@cmd_dhcp_client
|
||||
case cmd
|
||||
when NotSetup
|
||||
when NotSetup.class
|
||||
puts "no dhcp client: cannot perform dhcp on #{name}"
|
||||
when UDHCPCCommand
|
||||
else
|
||||
cmd.run name
|
||||
end
|
||||
end
|
||||
@ -263,12 +285,20 @@ class NetworkCommands
|
||||
@@cmd_network_configuration.description name, description
|
||||
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)
|
||||
cmd = @@cmd_wireless_configuration
|
||||
case cmd
|
||||
when NotSetup
|
||||
when NotSetup.class
|
||||
puts "no wireless configuration program: cannot list ssid"
|
||||
when IWCommand
|
||||
when IWCommand.class
|
||||
cmd.list_ssid ifname
|
||||
end
|
||||
end
|
||||
@ -276,9 +306,9 @@ class NetworkCommands
|
||||
def self.wireless_connect_wpa_psk(ifname : String, ssid : String, passwd : String)
|
||||
cmd = @@cmd_wireless_configuration
|
||||
case cmd
|
||||
when NotSetup
|
||||
when NotSetup.class
|
||||
puts "no wireless configuration program: cannot connect to ssid #{ssid}"
|
||||
when IWCommand
|
||||
when IWCommand.class
|
||||
cmd.list_ssid ifname
|
||||
end
|
||||
end
|
||||
@ -489,7 +519,7 @@ class InterfaceConfiguration
|
||||
when DHCP
|
||||
NetworkCommands.dhcp name
|
||||
when NotSetup
|
||||
puts "no ipv4"
|
||||
# do nothing
|
||||
else
|
||||
raise "ipv4 configuration: neither static nor dynamic"
|
||||
end
|
||||
@ -514,7 +544,7 @@ class InterfaceConfiguration
|
||||
#when DHCP
|
||||
# NetworkCommands.dhcp6 name
|
||||
when NotSetup
|
||||
puts "no ipv6"
|
||||
# do nothing
|
||||
else
|
||||
raise "ipv4 configuration: neither static nor dynamic"
|
||||
end
|
||||
@ -527,6 +557,15 @@ class InterfaceConfiguration
|
||||
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
|
||||
|
||||
class NetworkConfigurationParser
|
||||
@ -807,8 +846,12 @@ case command
|
||||
when "list"
|
||||
# TODO: why having to force "not_nil!" ? Seems like a compiler bug
|
||||
puts NetworkConfigurationParser.parse_file(file.not_nil!)
|
||||
when "connect"
|
||||
when "up"
|
||||
# TODO: why having to force "not_nil!" ? Seems like a compiler bug
|
||||
network_configuration = NetworkConfigurationParser.parse_file(file.not_nil!)
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user