How to combine ifconfig with openssl to change MAC address - bash

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...

Related

How to get a MAC address through MATLAB?

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);

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')

Osx Sierra argument list too long

I'm facing this kind of error:
zsh: argument list too long: [command]
I need to launch a command via terminal that needs as argument a long string (it's about 1.4mb).
So instead of copy-paste the string into the terminal I actually save it on a file and then I take the content with cat file.txt.
So the command I launch in the end is:
bitcoin-cli -rpcconnect=127.0.0.1 -rpcport=16593 -rpcuser=root -rpcpassword=root submitblock 'cat test.txt'
(where ' symbols before and after cat test.txt are `)
This, as I said before, leads me to the error zsh: argument list too long: bitcoin-cli.
I've already tried to follow this blog post:
Link
Even if it changed the setting returned by ulimit -a, it didn't solve my issue.
Does anyone knows a solution?
Thanks everyone!
The maximum length of a command is defined by the kern.argmax sysctl setting, which is not changeable. On my system (El Capitan) it's 256kB:
$ sysctl kern.argmax
kern.argmax: 262144
So I don't think you'll be able to specify your block on the command line. Maybe there's the possibility to load it from a file or from standard input?

Script awk v. sed for modifying ipsec.conf

I am creating a script that I can run and it will simply ask me the common location name...i.e SEC-DF1 and it will fetch the ip of that site from within script. My problem is taking that IP and replacing
right=IP_ADDRESS
with
right=NEW_IP_ADDRESS
I need this so I can call the script as I will be changing the value of right so often for testing.
I have been messing with sed until someone mentioned awk...this stuff has such horrid documentation I keep getting all types errors or weird results on the test file I am messing with.
Since this is a straight forward substitution, I would just use sed:
sed -e 's/^right=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/right=192.168.1.92/' filename
This will match right= at the beginning of a line followed by an IP address and replace it with the IP of your choosing.
This command will modify your script:
NEW_IP_ADDRESS=101.102.103.104 sed -i "s/^(right=).*$/\\1$NEW_IP_ADDRESS/" script

Mac ssh terminal

So I am on an old (fully aluminum) macbook pro running snow leopard and using terminal to ssh into remote hosts on my work network. I am noticing a strange thing when I copy and paste things in the terminal.
For example I will grep for something like this in a file:
samtools view sorted-616.bam | grep 'SOLEXA9:1:1:30:3316:10211' | head -n 1
and it gives
SOLEXA9:1:1:30:3316:10211 69 k26_179705 159 0 * = 159 0 TATGCCGCCAAACGCTTCCGCAAAGCTCTGTGTTTGACTATGTAGCGACTA CBCCCCCC#CCCCCCCCC?#CC?CC########################## RG:Z:1
But now when I select it, hit command+c to copy, and then command+v to paste, it comes out like this:
SOLEXA9:1:1:30:3316:1021169k26_1797051590*=1590TATGCCGCCAAACGCTTCCGCAAAGCTCTGTGTTTGACTATGTAGCGACTACBCCCCCC#CCCCCCCCC?#CC?CC##########################RG:Z:1
Notice how there are no spaces in between fields now. Is there a special method to copy and paste things exactly as they are?? Why is terminal behaving this way?
What happens when you use pbcopy?
samtools view sorted-616.bam | grep 'SOLEXA9:1:1:30:3316:10211' | head -n 1 | pbcopy
This should do the same thing that the copy command does, but without having to select the output you want.
Have you tried another terminal emulator? I use iTerm2 because it will copy to the clipboard on selection without having to hit Command-c.
EDIT: You may have to install Apple's developer tools to get the pbcopy/pbpaste tools.
No idea why the spaces are missing from the pasted text, but I'd try to write the output to a file, open up the file in an editor and try to see if it's something other than the standard space/newline characters. You seem to know your way with piping, but anyway:
samtools view sorted-616.bam | grep 'SOLEXA9:1:1:30:3316:10211' | head -n 1 > file.txt
It might depend on which system the host is running. I have had some issues while on ssh connections to linux/unix hosts, while Mac-to-Mac usually works just fine.

Resources