How to get netmask? - ruby-on-rails

I know how to get from ifconfig. (linux)
But is there another way? Can found it in socket.

You need to use IO#ioctl. This is totally non-portable. On my linux box this code words:
require 'socket'
sock = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM,0)
buf = ["eth0",""].pack('a16h16')
sock.ioctl(0x891b, buf)
netmask = "#{buf[20]}.#{buf[21]}.#{buf[22]}.#{buf[23]}" #=> "255.255.255.240"
Ioctl differs considerably between systems and I had to look through a few system header files to get the right sizes for the [].pack, the location of the address in buf and the numeric value for SIOCGIFBRDADDR (the first argument to ioctl).
If these values don't work for you I can give you more information on how to find them.

Related

Can't switch from staic IP to DHCP in nodemcu lua

wifi.sta.setip({ ip = "192.168.0.111", netmask = "255.255.255.0",gateway = "192.168.0.1"})
with above we can set static IP,
but by mistake if someone enters the netmask/gateway/ip a digit or to wrong (eg:netmask = "255.255.2.0" ) we have no way of detecting. only way is to
by re entering correctly
rebooting
(How to clear static IP configuration and start DHCP)
so it would be nice if we can detect the status somehow like with wifi.sta.status() do.
How to clear static IP configuration and start DHCP
wifi.sta.clearconfig()
Clears the currently saved WiFi station configuration, erasing it from
the flash. May be useful for certain factory-reset scenarios
Not sure if this also deletes the ip but you can probably just set the ip to "0.0.0.0" befor you reconnect.

HAProxy Lua how to change server port?

Server.set_addr (sv, addr)
I can not understand what the method takes as input.
It would seem that Server.get_addr(sv) returns ip:port - so you also need to pass ip:port, but no. This does not work.
The documentation reads:
See the documentation for the control socket for more information on the string format.
Well, I saw that you need to transfer the ip port: port (example: 127.0.0.1 port 80). But that doesn't work either.
The only thing that works is pass only ip (example: 127.0.0.1).
The question is how to change the port?
Code sample: (if not work - no have any entry in log)
-- work
-- log entry: changed its IP from 1.1.1.1 to 2.2.2.2 by Lua script.
local newAdr = backendServer.ip
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. ":" .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " " .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " port " .. backendServer.port
server:set_addr(newAdr)
Update: As of HAProxy 2.2dev7, this functionality has now been merged: set_addr takes an additional optional parameter to specify a new port.
It's not currently possible to do that via Lua. A look through HAProxy's source code reveals that set_addr calls hlua_server_set_addr, which calls server_parse_addr_change_request, which calls update_server_addr, which only updates the address and not the port. Contrast this with the management socket's set server, which calls cli_parse_set_server, which calls update_server_addr_port, which does update the port too.
However, there are two pieces of good news:
Since the management socket can do it, you can use it as a workaround.
It would be very simple to modify HAProxy's Lua interface to support updating the port, since all the logic is already there. I sent a patch that does so to their mailing list.

How to obtain bluetooth port direction with pyserial?

I'm trying to connect to an RN42, module through python. When the RN42 pairs with W10 it creates two virtual COM ports(outgoing and incoming). I need to connect to the outgoing port.
I'm trying to do this automatically. I've tried:
import serial
import serial.tools.list_ports as port_lst
ports = list(port_lst.comports())
bluetooth_ports = []
for p in ports:
if 'Bluetooth' in p.description:
bluetooth_ports += [p.device]
bluetooth_com = serial.Serial(bluetooth_ports[0],115200)
I thought that the first port was usually the outgoing one, but I've paired the module to another computer, and this didn't apply (the second port was the outgoing one). Is there a way to find out the direction of the COM ports?
Thanks!!!
Although this is an antique question, I have been searching for the answer to this for some time myself and since I finally figured it out I wanted others to be able to find the answer. With help from a blog entry at in the hand and its accompanying gist:
The trick is to acquire the hwid using pySerial, then parse the address. The incoming port in a pair has an address of zero and the outgoing port has a nonzero address. Here is some ugly Python code that decodes it:
import serial.tools.list_ports
cp=serial.tools.list_ports.comports()
for p in cp:
if "BTHENUM" in p.hwid:
start_of_address=p.hwid.rfind("&")
end_of_address=p.hwid.rfind("_")
address=p.hwid[start_of_address+1:end_of_address]
if int(address,16)==0:
port_type="incoming"
else:
port_type="outgoing"
print(p.hwid)
print(p.name, address, port_type)
And the output:
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&CC47540&0&000000000000_000000A8
COM4 000000000000 incoming
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\7&CC47540&0&209BA5420081_C00000000
COM5 209BA5420081 outgoing

Get the operating system in maxima

Is it possible to get the operating system in maxima? I have some code that needs the unix / or windows \ for path names. How can I find out which operating system the code is running in?
To give some context, I have the following code:
windows: false$
divider: "/"$
if (windows) then divider: "\\"$
initfile: concat(maxima_userdir, divider, "maxima-init.mac");
load(operatingsystem)$
dir: getcurrentdirectory();
if (substring(dir, slength(dir)) # divider) then dir: concat(dir, divider)$
repo: concat(dir, "$$$.mac")$
live: concat(dir, "live_packages", divider, "$$$.mac")$
with_stdout(initfile, printf(true, ""))$
with_stdout(initfile, printf(true, concat("file_search_maxima: append (file_search_maxima, [
~s,
~s
]);"), repo, live))$
Take a look at the output of build_info, specifically the field host (i.e. foo#host where foo : build_info()). See ? build_info for more information.
On my (Linux) system I get: x86_64-unknown-linux-gnu I think on MS Windows you'll get a string containing windows or at least win or maybe win32.
There may be other ways to figure out the system type so let me know if that doesn't work for you. Also it is possible that there is a global variable floating around which tells the path separator; I would have to look for that.
If you're not adverse to writing a little bit of Lisp code, another approach is to use the file and directory functions in Common Lisp, which are more extensive than in Maxima. See the section on filenames in the Common Lisp Hyperspec. I think maybe MERGE-PATHNAMES and/or MAKE-PATHNAME might be relevant.

How to remove data link layer from pcap file?

I'm making a script that is inspecting packets, but headers giving me a headache. I have a DSL connection/Wireless at home, and the data link layer is appearing in Wireshark capture, either PPP or WLAN depending on which one I am currently using.
I've been searching for a thsark, editcap or tcpdump or whatever tutorial but I couldn't find any.
Basically all I need: < program_name_which_nicely_removes_transport_layer > input.pcap < output.pcap_only_containing_ethernet_header+ip+tcp+data > or something similar.
I have found a program named bittwiste, but it's operating with fixed sizes as I realized, but I need something 'universal', where I don't have to determine the used link type + size.
Any help is appreciated!
Thank you in advance.
With Perl and the libraries Net::Pcap and Net::PcapWriter you could do the following to remove the PPPoE layer. This works at least for my router (fritz.box). This needs to be adapted to other encapsulations:
#!/usr/bin/perl
# usage: pcap_remove_pppoe.pl infile.pcap outfile.pcap
use strict;
use warnings;
use Net::Pcap qw(pcap_open_offline pcap_loop pcap_datalink :datalink);
use Net::PcapWriter;
my $infile = shift or die "no input file";
my $outfile = shift; # use stdout if not given
my $err;
my $pcap = pcap_open_offline($infile,\$err) or
die "failed to open $infile: $err";
my $ll = pcap_datalink($pcap);
die "expected DLT_EN10MB, got $ll" if $ll != DLT_EN10MB;
my $offset = 14; # DLT_EN10MB
# open and initialize output
my $pw = Net::PcapWriter->new($outfile);
# process packets
pcap_loop($pcap, -1, sub {
my (undef,$hdr,$data) = #_;
my $dl = substr($data,0,$offset,''); # remove data link layer
my $etype = unpack("n",substr($dl,-2,2)); # get ethernet type
$etype == 0x8864 or return; # ignore any type except PPPoE Session
substr($data,0,8,''); # remove 8 byte PPPoE layer
substr($data,-2,2,pack("n",0x0800)); # set ethernet type to IPv4
# output: data link layer with IP type + IP layer (PPPoE removed)
$pw->packet($data, [ $hdr->{tv_sec},$hdr->{tv_usec} ]);
},undef);

Resources