Skip to content

Instantly share code, notes, and snippets.

@juzam
Forked from jpmens/hare.c
Created March 26, 2018 17:35
Show Gist options
  • Save juzam/1a092365463567153df1b55f8b0927b5 to your computer and use it in GitHub Desktop.
Save juzam/1a092365463567153df1b55f8b0927b5 to your computer and use it in GitHub Desktop.
hare / hared
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <memory.h>
#include <sys/socket.h>
/*
* hare.c (C)2018 by Jan-Piet Mens <[email protected]>
*/
#define PORTNO 8035
#define env(K) ( getenv(K) ? getenv(K) : "<unknown>" )
int main(int argc, char **argv)
{
struct sockaddr_in servaddr;
unsigned long addr;
int sockfd;
char *host = argv[1], buf[BUFSIZ], myhostname[BUFSIZ];
char *pamtype = env("PAM_TYPE"); /* Linux */
char *pamsm = env("PAM_SM_FUNC"); /* FreeBSD https://www.freebsd.org/cgi/man.cgi?query=pam_exec */
if (strcmp(pamtype, "open_session") != 0 && strcmp(pamsm, "pam_sm_open_session") != 0) {
fprintf(stderr, "Neither PAM open_session nor pam_sm_open_session detected\n");
return 0;
}
if (argc != 2) {
fprintf(stderr, "Usage: %s address\n", *argv);
exit(2);
}
if (gethostname(myhostname, sizeof(myhostname)) != 0)
strcpy(myhostname, "?");
snprintf(buf, sizeof(buf), "%s login to %s from %s via %s",
env("PAM_USER"),
myhostname,
env("PAM_RHOST"),
env("PAM_SERVICE"));
addr = inet_addr(host);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORTNO);
memcpy((void *)&servaddr.sin_addr, (void *)&addr, sizeof(addr));
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sendto(sockfd, (void *)buf, strlen(buf), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
perror("sendto");
}
return (0);
}
package main
import (
"fmt"
"net"
"encoding/json"
"github.com/eclipse/paho.mqtt.golang"
)
type Data struct {
Host string `json:"host"`
Msg string `json:"msg"`
}
func main() {
ServerAddr, _ := net.ResolveUDPAddr("udp",":8035")
ServerConn, _ := net.ListenUDP("udp", ServerAddr)
defer ServerConn.Close()
buf := make([]byte, 1024)
for {
n,addr,_ := ServerConn.ReadFromUDP(buf)
message := string(buf[0:n]);
address := addr.IP.String();
data := Data{Host: address, Msg: message}
js, _ := json.Marshal(data)
fmt.Println(string(js))
opts := mqtt.NewClientOptions().AddBroker("tcp://192.168.1.130:1883")
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
}
if token := c.Publish("logging/hare", 0, false, js); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
}
}
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import paho.mqtt.publish as mqtt
import socket
import json
__author__ = 'Jan-Piet Mens <jp()mens.de>'
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('', 8035))
while True:
message, address = server_socket.recvfrom(1024)
host, port = address
data = {
'host' : host,
'msg' : message,
}
js = json.dumps(data)
print js
mqtt.single("logging/hare", js, hostname='192.168.1.130')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment