#include #include #include #include #include #include #include int main(){ // get ip address // get last byte of the ip address // assemble a hostname like mlh310-(last byte of the ip address) struct ifaddrs *ifa_start; getifaddrs(&ifa_start); uint32_t address; for(struct ifaddrs *i = ifa_start; i; i = i->ifa_next){ printf("Interface: %s\n", i->ifa_name); if(i->ifa_addr->sa_family == AF_INET){ printf("IPv4 address detected!\n"); struct sockaddr_in *a = (struct sockaddr_in*)i->ifa_addr; printf("%s\n", inet_ntoa(a->sin_addr)); address = a->sin_addr.s_addr; } } unsigned char last_byte; char new_hostname[128]; char *address_as_array = (char*)&address; sprintf(new_hostname, "mlh310-%d", address_as_array[3]); printf("New name: %s\n", new_hostname); sethostname(new_hostname, strlen(new_hostname)); return 0; }