Skip to content

Instantly share code, notes, and snippets.

@goliatone
Last active July 5, 2021 21:01
Show Gist options
  • Save goliatone/94e0f89bfe92b18615497990dce89cac to your computer and use it in GitHub Desktop.
Save goliatone/94e0f89bfe92b18615497990dce89cac to your computer and use it in GitHub Desktop.
[RPi Utilities] #rpi

RPi Utilities

Get mac addresses:

ifconfig -a | grep HWaddr | tr -s " " | cut -d' ' -f5

Output:

b8:27:eb:34:53:7e //eth0
b8:27:eb:61:06:2b //wlan0

Get serial number:

awk '/Serial/{print $3}' /proc/cpuinfo

Output:

000000002b34537e

Notice that the last 6 characters of the serial number and the last 6 characters of the eth0 mac address are the same.

Python

def getserial():
    # Extract serial from cpuinfo file
    cpuserial = "0000000000000000"
    try:
        f = open('/proc/cpuinfo','r')
        for line in f:
            if line[0:6] == 'Serial':
                cpuserial = line[10:26]
        f.close()
    except:
        cpuserial = "ERROR000000000"

    return cpuserial
def getMAC(interface):
  # Return the MAC address of interface
  try:
    str = open('/sys/class/net/' + interface + '/address').read()
  except:
    str = "00:00:00:00:00:00"
  return str[0:17]
* * * * * /home/pi/CODE/cudp >> /home/pi/CODE/cudp.txt
package main
import (
"fmt"
"net"
"time"
"log"
"strconv"
)
//CheckError ...
func CheckError(err error) {
if err != nil {
fmt.Println("Error: " , err)
}
}
func main() {
ServerAddr,err := net.ResolveUDPAddr("udp","224.0.0.1:1234")
LocalAddr, err := net.ResolveUDPAddr("udp", ":0")
CheckError(err)
Conn, err := net.ListenUDP("udp", LocalAddr)
CheckError(err)
defer Conn.Close()
i := 0
for {
msg := strconv.Itoa(i)
i++
buf := []byte("request;23;/alive;" + msg)
_,err := Conn.WriteTo(buf, ServerAddr)
if err != nil {
fmt.Println(msg, err)
}
time.Sleep(time.Second * 1)
buf = make([]byte, 1024)
n,addr,err := Conn.ReadFrom(buf)
log.Println("Received ",string(buf[0:n]), " from ",addr)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment