Getting Mac Address and Hostname with Cygwin - cygwin

I need to write a script that would get hostname and all physical addresses on my local machine. It will be run on the several machines.
After some research I've found out that I can get hostname from the $HOSTNAME variable and I also found getmac command.
The problem with getmac is that it returns all mac addresses (loopback, logical, etc.) without any clue about interface it uses. Here is my example output:
2C-27-D7-40-62-7A \Device\Tcpip_{A04C5C9D-CD41-48D1-8EC8-54128C70835C}
32-9D-DE-C7-3F-6D \Device\Tcpip_{EB38E492-9BEF-4F8F-98E6-C08716920461}
02-90-4C-4F-4F-50 \Device\Tcpip_{42DB7C91-BB9E-4BF4-B374-289F1BBA7AC6}
It gives me 3 mac addresses, from which only one is physical, but I don't see the way to decide which one. Could you please help me?

since you mentioned cygwin, 'awk' would work over it... for general windows execution even a separate download of GNU-Win32 utils is available with AWK.
when you do "$ getmac -V -FO LIST" it would display the Network Devices with their name and then you could fetch the required MAC Address as following:
[Ethernet Card]
for "Local Area Connection" text-marked card ~
$ getmac -V -FO CSV | awk -F ',' '{if(match($1,"Local Area Connection"))print $3;}'
[Wireless Card]
for "Wireless Network Connection" text-marked card ~
$ getmac -V -FO CSV | awk -F ',' '{if(match($1,"Wireless Network Connection"))print $3;}'
if you have more than one Ethernet/Wireless cards, name might change a bit... you can check for actual name using "$getmac -V" and match it as required.

Just parse the output from ipconfig /all
I presume you are only interested in the "real" adapter connected to the outside..
Based on the IP-address/default gateway info that is also in there it should be relatively straightforward to determine which is which.
Be warned that the ipconfig output varies slightly from Windows to Windows version.
You are fine if you split the lines on the ":". Don't assume that the : always appears in the same column.

You can also parse the output of ipconfig /all which is a windows utility that will give you interface names as well as their MAC addresses. I'm not 100% sure which Windows platforms support this utility, but it does exist on systems from NT through XP.
It does execute from a Cygwin bash shell.

Related

How do I use I/O redirection in combination with the nmcli help option?

Consider the following command, which I am trying to execute on Red Hat Enterprise Linux 8 system.
nmcli con add help | less
Some of the help pages for specific COMMANDs are quite lengthy and piping to less is not working as I would expect. Normally a command like 'man nmcli | less' provides the first page and you have to use navigation to get to the bottom. In this case it shows me the last page, and I can't page up. Attempting to do so results in a page full of blank lines. I'd like to see all of the help documentation for that particular combination of the object and add command. For some reason the help option for the nmcli command is not working well with the less utility. If I try to use '>' to redirect to a file I end up with a blank file.
It looks nmcli help is writing on stderr instead of stdout.
The following works on CentOS 7:
$ nmcli con add help 2>&1 | less

how to enable statistics using awk in linux

when I type the command stats.awk size.cvs, it says command not found on my virtual machine linux. Can someone help me get through sice Im new to linux and linux coding
It looks like you are not trying to run any standard Linux tool, but a script written by someone else called "stats.awk"... and the file you want to operate on is called "size.cvs" (it is more likely called size.csv -- as this is likely a "Comma Separated Value" file). Most Linux implementations do not allow you to just run a script unless the script is in your PATH and has the right permissions.
You could try "fully qualifying" the path of your awk script, but probably easiest is to just feed it into awk. Try:
awk -f stats.awk size.cvs
To find out more information about your shell run:
man $SHELL
Or just Google for a tutorial on your shell (which is likely bash). To find more about awk, again, try Googling for a tutorial on awk.

Search linux log files using mac terminal or script

I want to search my access logs for traffic going to a directory on a Linux. I have a mac which I know I can do this with my terminal but I can't find an example how to do this.
Do you have the log locally on your Mac? Or are you asking how you would use a Mac to administer a Linux OS?
Open terminal, either change directory to the dir that contains the log or adjust the command to suit. Depending on what you want to search for, you'd use grep.
grep -i "dir name" logfile.log
It's a bit of a wide empty question so I don't know what you want to search for.
If it's remote, you'd open terminal and ssh to the server which has the log and do something similar to the above. Or, if you're asking how you would do it in one command, you could use ssh too.
ssh user#linux "grep something /var/log/apache/access.log"

How do I detect the OS Name in Perl to a particular version?

I have tried to look for an answer on how this may be done but I cannot seem to find one.
I am aware that to find the OS name in perl you can use
$^O
However in Linux and Solaris, no matter what OS version you are on it will just return either Solaris or Linux. I would like it to show up as Solaris10 or Solaris11. Is there a way to get this? I know if you enter
uname -r
you can get the version number (5.10 in Solaris for example).
Any thoughts on how this could be done?
Thank you =)
The POSIX module in perl provides the uname call, so:
use POSIX qw(uname);
my #uname = uname();
print $uname[0] . " " . $uname[2];
This provides the information from the running system, not from the system that performed the build (which is where the Config results come from).
In general, though, this information is the kernel release, and not the marketing version of the product, so for example, this wouldn't tell you if it's a Linux Mint system, just that it's a Linux system running the 3.2.foo kernel.
For lsb based Linux systems, the lsb_release command can give this information e.g. on a linux system I have:
natsu ~> lsb_release -r
Release: 12.04
natsu ~> lsb_release -i
Distributor ID: Ubuntu
This information is in the /etc/lsb-release file.
For Solaris, the release information is in the /etc/release file. The first line contains the name of the operating system e.g. Solaris 10 9/10. There are multiple lines in this file, and it's more of a free-form text field.
use Config;
print "$_\n" for #Config{qw(myuname osname osvers)};
From perldoc Config,
The Config module contains all the information that was available to the Configure program at Perl build time.
myuname
The output of uname -a if available, otherwise the hostname. The whole thing is then lower-cased and slashes and single quotes are removed.
osname
This variable contains the operating system name (e.g. sunos, solaris, hpux, etc.). It can be useful later on for setting defaults. Any spaces are replaced with underscores. It is set to a null string if we can't figure it out.
osvers
This variable contains the operating system version (e.g. 4.1.3, 5.2, etc.). It is primarily used for helping select an appropriate hints file, but might be useful elsewhere for setting defaults. It is set to '' if we can't figure it out. We try to be flexible about how much of the version number to keep, e.g. if 4.1.1, 4.1.2, and 4.1.3 are essentially the same for this package, hints files might just be os_4.0 or os_4.1, etc., not keeping separate files for each little release.

Using Linux commands on files across multiple servers

I am new to Linux as a whole and so far I have not found a solution to this that isnt clumsy at best. I have a Windows background and so I am accustomed to running commands on one server that access text files on multiple systems in the same domain.
Example of what is processed in Windows:
find "Some text" \\ServerName01\c$\inetpub\*.log
find "Some text" \\ServerName02\c$\inetpub\*.log
find "Some text" \\ServerName03\c$\inetpub\*.log
Example of what I would LIKE to do in Linux:
sed 's/SomeText/OtherText/p //ServerName01/var/opt/somefolder/*.log
sed 's/SomeText/OtherText/p //ServerName02/var/opt/somefolder/*.log
sed 's/SomeText/OtherText/p //ServerName03/var/opt/somefolder/*.log
What is the best way to do the above in Linux, or is it even possible?
Thanks!
See the pssh and pscp suite, you can run commands on a bunch of remote servers : http://www.theether.org/pssh/
pssh or cssh would work
pssh provides a number of commands for executing against a group of
computers, using SSH. It’s most useful for operating on clusters of
homogenously-configured hosts.
http://www.ubuntugeek.com/execute-commands-simultaneously-on-multiple-servers-using-psshcluster-sshmultixterm.html
there is a lot of way for doing it :
Via NFS/Fuse Mount, mount the logs directory on one system and you could do the same thing as windows (which automatically mount remote filesystem with the "\\")
use ssh,(that would be my prefered solution)
cat serverlist | xargs -i ssh {} " grep \"some text\" yourfilepaths"
which helps if you use ssh keys pairs

Resources