How to get a MAC address through MATLAB? - windows

I am going to design a firewall using MATLAB and I need to get Ethernet data like IP, Port and MAC address to write my filter rules.
How do I obtain those?

Since you originally had the macos tag on your question (before the question was edited), here is a more general solution with error checking that should work on Windows, macOS, and on other UNIX/Linux systems:
if ispc
[status,result] = dos('getmac');
if status == 0
mac = result(160:176);
else
error('Unable to obtain MAC address.\n%s',result)
end
elseif isunix
[status,result] = system('ifconfig en0 | awk ''/ether/ {print $2}''');
if status == 0
mac = result(1:end-1); %remove trailing carriage return
else
error('Unable to obtain MAC address.\n%s',result);
end
else
error('Platform not recognized. Unable to obtain MAC address.');
end
For UNIX-based systems (including macOS), the system function is used to call the ifconfig and awk commands in the terminal.
Note that for both Windows and UNIX this solution returns the MAC address for the primary Ethernet interface on en0. It's possible that on some systems one may need to change this to en1 or another interface ID depending on what ifconfig returns. You can use ifconfig via system to also obtain IP addresses, port number, etc. on UNIX systems.

The MathWorks Staff has answered this on MATLAB answers:
Using the output of this command, you can parse the resulting string
output to get MAC address. For instance:
[status,result] = dos('getmac');
mac = result(160:176);

Related

Changing MacOS Location based on SSID - can't get script to run automatically

I'm trying to get this GitHub project up and running on my Mac, for the purposes of automatically changing my network location when I switch physical location (based on the SSID I'm connected to). I have two problems I'm so far unable to resolve.
I've followed the instructions to update the appropriate location names, and associated SSID's, and it largely all works as it should. However, there's one SSID that has a space in the name, and when I connect to that SSID, it falls back to "Automatic" location as it did not find a matching SSID in the list. I have updated the script as per the suggestions below to put quote marks in all the places they are missing, but the issue appears to be in getting the SSID in the first place. The log file echoes the new SSID name, and if my SSID were my wifi it just echoes New SSID Found: my. So I likely just need to slightly change the line of code that gets the SSID (below), but I'm not sure how.
SSID=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | awk '/ SSID:/ {print $2}')
I've completed the automated installation (executed ./install.sh on the appropriate directory), and it has placed all the files in the relevant places. However, it does not automatically run whenever I switch SSID's. I can double click on the executable at any point and it will run and select the correct location, but it doesn't execute automatically, which is the whole point of the exercise. Have I missed a step somewhere?
I'm extremely new to scripting on mac and GitHub in general; my previous experience is all on Windows. So I'm probably overlooking something really simple, but I just don't have the skills yet to work out where it is.
Running macOS Catalina 10.15.2
Nice sleuthing! It should work if you change the awk to use a colon and a space as the field separator:
SSID=$(AIRPORTTHING| awk -F ': ' '/ SSID:/ {print $2}')
Failing that, this should work too:
SSID=$(AIRPORTTHING| sed -n 's/^ *SSID: //p')

How to combine ifconfig with openssl to change MAC address

I am trying to make a one-line MAC Address spoofer. However, I cannot combine ifconfig with OpenSSL.
My method of doing this is to change the MAC Address to a randomly generated MAC Address using OpenSSL. However, combining the code to change the MAC Address with the random MAC Address creator doesn't seem to work.
openssl rand -hex6 | sed 's/\(..\)/\1:/g;s?.$//
This code creates a random MAC Address
sudo ifconfig en0 ether xyz
This code sets the MAC Address to xyz
Combining these two lines did not change the MAC Address as expected.
sudo ifconfig en0 ether openssl rand -hex6 | sed 's/\(..\)/\1:/g;s?.$//
This code returns a "Invalid Argument"
I expect this to be a formatting issue, but I could not find proper formatting guides online and I am new to bash.
There are a bunch of problems in this code. I think the main thing you are missing is that you need $( ) to capture the output of one command so you can use it as an argument to another command (and you generally want double-quotes around that, to avoid unexpected parsing weirdness). The way you've written it, openssl rand -hex6 isn't treated as a command, just as three more arguments to sudo ifconfig (and it's the output from sudo ifconfig that's piped to sed).
You also need a space in -hex 6, and the end of the sed command is using inconsistent delimiters and is missing a close quote. Also, rather than using a capture group in the first sed command, you can just use & to get the entire matched string (i.e. s/../&:/g).
Here's a corrected version:
sudo ifconfig en0 ether "$(openssl rand -hex 6 | sed 's/../&:/g;s/:$//')"
EDIT: as #Cyrus pointed out in a comment, MAC address space is divided up into globally- vs. locally administered, and uncast vs. multicast (see Wikipedia). Using a multicast address might cause trouble, so it'd really be better to force the second digit to be even. This is a bit tricky, so I'm going to duck the question of how to do it...

mac terminal computer name is garbled

When I use terminal enter zsh, my computer name is garbled, n3-85-8 instead MacBook-Pro. Sometime so as the bash. Do anyone know why? And how to fix it.
There are two effects happening here:
Bash only reads the hostname (as displayed in the prompt) once at shell startup, which means you only see the change when you start a new shell, not when your hostname changes.
macOS by default changes its own hostname based on the network configuration
You can configure your computer not to change its hostname (see for example this question). Or, you can configure bash to use the computer's persistent LocalHostName in the prompt. This value does not change when you connect to a different network.
You can edit your ~/.bashrc (or related file) to have a line like:
PS1=$(scutil --get LocalHostName)':\W \u\$'
Extending Grisha's excellent answer, there are several different host names. Often the same, but may vary to accommodate different naming constraints. The whatami function (below) can help you choose which one you want in your PS1 prompt.
Here's a Bash function to help assess the different names.
function whatami {
local cn=$(scutil --get ComputerName)
local lhn=$(scutil --get LocalHostName)
local hn=$(scutil --get HostName)
local nbn=$(/usr/libexec/PlistBuddy -c "Print :NetBIOSName" /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist)
printf '\e[1mComputerName\e[0m: '"$cn"'\n'
printf '\e[1mLocalHostName\e[0m (Bonjour): '"$lhn"'\n'
printf '\e[1mHostName\e[0m: '"$hn"'\n'
printf '\e[1mNetBIOSName\e[0m (SMB): '"$nbn"'\n'
printf '\e[1mIP Address\e[0m: '
for x in $(ifconfig -l); do ipconfig getifaddr $x; done
}

Write data to Arduino from terminal (OSX)

I'm attempting to send data to my Arduino over my USB cable from the OSX terminal. My understanding was that I should be able just do something like echo b > /dev/tty.usbmodem1411 or echo b > /dev/cu.usbmodem1411 but neither of those are working. I also tried an explicit newline by doing echo "b\r\n" > /dev/tty.usbmodem1411 and echo "b\r\n" > /dev/cu.usbmodem1411 but neither worked. I don't really have any idea what's going on, what the difference between the cu and tty is (when I write data with the Serial Monitor in the Arduino IDE I use the cu version, and that works)
It is not as easy as echoing :)
Here is great tutorial with everything you need to know about Serial Communication -- Serial Terminal Basics
I'd be happy to answer more of your questions if needed.
EDIT:
for the shell script something like that:
#!/bin/bsh
screen /dev/tty.usbmodem* 9600
# and everything you need to do
I found this blog post: http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/
And this code: https://github.com/todbot/arduino-serial
Which solve the problem.

Extend ps output to more than 80 characters in Solaris 8

How do you extend the output of ps -fe in Solaris so that it displays more than 80 characters? My process has several arguments and the process name could not be displayed anymore.
You can't display them with the default ps (/usr/bin/ps) which is a SVR4 regular one.
To get the full argument line, use the BSD ps (UCB = University of California at Berkeley):
/usr/ucb/ps -alxwww
We have finally fixed this in Solaris; as of Solaris 11.3 SRU 5, all original argument vectors as well as the environment variables can be retrieved from /proc. ps will now print all of the command line.
Fixed in Solaris 11.3 SRU 5
The simple answer is that there is no way to reliably acquire the full arguments to processes on Solaris for processes owned by other users. If you have root or other privileged access you can use /usr/ucb/ps on older versions, and 'pargs' or similar tools on newer versions (there is no tool which works across all versions).
Essentially Solaris stores the original args at process start time, while most other platforms allow ps to access, via some means, the contents of argv at runtime for the process. This stored-copy is in a special kernel data structure with limited (80 byte) size. This also means that it's not possible for a program to modify the args post-start as displayed by ps for useful or nefarious means.
Thus, if you need to access the command line for portable purposes, such as pid checking, you will need to choose between enforcing a short command line via hacks like launching programs controlled execp paths without absolute paths, or you will need to forgo that portable functionality on Solaris.
you can use pargs PID
it will give you more information than ps
Try ps -efl. If that doesn't work (I don't have a Solaris box handy) you can also try ps -efl | cat (as some programs check whether they're outputting to a terminal to decide on their output width).
There are two sets of options available for ps. Others will chime in with the correct names ( ( maybe BSD and SRVn)?)
With the non-options-preceded-with-a-hyphen-version, you can do
ps auxww(w?) | grep ${PID} to extend the length of the command detail that is printed (again, notice NO leading '-' option indicator).
Note that in some cases you will see a lot of environment variable assignments before the actually command, i.e. myPath=... cfgFile=... /path/to/command ... args ...
I think that 'www' in some systems will print everything, regardless how long the command is.
Finally, in my experience using ps to do a lot of crazy things, I would ocassionally have a PID and the output would display the first 6? columns, but the space reserved for the command was empty or had some sort of place holder value. I eventually found out why that was true, by searching comp.unix.shell, but it's too long ago now to be sure and I don't have access to Solaris systems right now.
I hope this helps.

Resources