chosts.c

来自「linux/unix下c/s形式的资源监视,客户端负责搜集机器,再传送到服务端.」· C语言 代码 · 共 79 行

C
79
字号
/*  * List all the hosts in this cluster with the help of  * /proc/cluster directory. * * Copyright (c) 2004, by:      Jian Shen *    All rights reserved.      Peking University, China *                             <shenjian@net.pku.edu.cn> * * This file may be used subject to the terms and conditions of the * GNU Library General Public License Version 2, or any later version * at your option, as published by the Free Software Foundation. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Library General Public License for more details. * */#include <sys/types.h>#include <dirent.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <netdb.h>void print_host(char* host_ip){    long hostname;    struct hostent *hinfo;                       	hostname = inet_addr(host_ip);	hinfo = gethostbyaddr((char *)&hostname, sizeof(long), AF_INET);	if(hinfo != NULL)		printf("%s\n", hinfo->h_name);	else		printf("%s\n", host_ip);}void help(){	printf("\nCluster load monitor like top.\n");	printf("no arguments is needed.\n");	printf("report bugs to <shenjian@net.pku.edu.cn>.\n");	printf("\n");}int main(int argc, char* argv[]){	DIR *dp;	struct dirent *dirp;	if(argc != 1) {		help();		exit(1);	}	if((dp = opendir("/proc/cluster")) == NULL) {		fprintf(stderr, "Failed to open /proc/cluster!\n");		exit(1);	}	printf("current hosts: \n");	while((dirp = readdir(dp)) != NULL) {		if(strcmp(dirp->d_name, ".") == 0)			continue;		if(strcmp(dirp->d_name, "..") == 0)			continue;		print_host(dirp->d_name);	}	closedir(dp);		return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?