Linux获取本机IP、MAC示例程序
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
void peek_interfaces(int fd);
void print_hw_addr(int fd, const char* if_name);
int main() {
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == fd) {
perror("Failed create socket.");
return -1;
}
peek_interfaces(fd);
close(fd);
return 0;
}
void peek_interfaces(int fd) {
ifreq ifs[16] = {0};
ifconf conf = {sizeof(ifs)};
conf.ifc_req = ifs;
if(-1 == ioctl(fd, SIOCGIFCONF, &conf)) {
perror("Failed IOCTL SIOCGIFCONF.");
return;
}
if(conf.ifc_len >= sizeof(ifs)) {
perror("Buffer too small for IOCTL SIOCGIFCONF.");
return;
}
int num = conf.ifc_len / sizeof(ifreq);
cout << num << " interface entry retrieved." << endl;
for(int i = 0; i < num; ++i) {
cout << "[ " << ifs[i].ifr_name << " ]" << endl;
sockaddr_in* sai = (sockaddr_in*)&ifs[i].ifr_addr;
cout << "Addr: " << inet_ntoa(sai->sin_addr) << endl;
print_hw_addr(fd, ifs[i].ifr_name);
cout << endl;
}
}
void print_hw_addr(int fd, const char* if_name) {
ifreq req = {0};
strcpy(req.ifr_name, if_name);
if(-1 == ioctl(fd, SIOCGIFFLAGS, &req)) {
perror("Failed IOCTL SIOCGIFFLAGS.");
return;
}
if(req.ifr_flags & IFF_LOOPBACK) {
cout << "Is LOOPBACK." << endl;
return;
}
if(-1 == ioctl(fd, SIOCGIFHWADDR, &req)) {
perror("Failed IOCTL SIOCGIFHWADDR.");
return;
}
unsigned char* puc = (unsigned char*)req.ifr_hwaddr.sa_data;
printf("HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
puc[0], puc[1], puc[2], puc[3], puc[4], puc[5]);
}
相关文档:
在 linux 下配置 ITK
1. 下载 CMake:http://www.cmake.org/cmake/resources/software.html
为方便安装,这里下载二进制文件,选择下载: cmake-2.6.4-Linux-i386.sh
2. 安装 CMake, 到 cmake-2.6.4-Linux-i386.sh 存放的目录,输入,可用 TAB 键方便补齐命令
#./ cmake-2. ......
今天fedora挂了,为了备份数据用ubuntu的livecd来进行数据备份,从网上看到了一篇帖子,
http://www.linux-sxs.org/storage/fedora2ubuntu.html
解决了问题
内容如下:
Boot Ubuntu.
Install lvm2:
$ sudo apt-get install lvm2
Load the necessary module(s):
$ sudo modprobe dm-mod
Scan your system for LVM v ......
NFS网络文件系统是通过文件系统实现资源共享的一种最重要的方式。
c/s 客户机 服务器
B/S 浏览器 服务器
NFS服务端配置:
编辑/etc/exports文件以配置开放路径。/home/share 192.168.0.123(ro)/(rw).
/etc/init.d/portmap restart
/etc/init.d/nfs&nb ......
ls
ls 命令可以说是linux下最常用的命令之一。它有众多的选项,其中有很多是很有用的,你是否熟悉呢?下面列出了 ls 命令的绝大多数选项。
-a 列出目录下的所有文件,包括以 . 开头的隐含文件。
-b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出。
-c 输出文件的 i 节 ......
NO
分类
PS1
命令名
用法及参数
功能注解
1
显示目录信息
#
ls
ls -a
列出当前目录下的所有文件,包括以.头的隐含文件
#
ls
ls -l或ll
列出当前目录下文件的详细信息
#
ls
ls -a
显示所有文件,包含隐藏。
#
ls
ls -al
显示所有文件的详细信息。
2
查看路径
#
pwd
pwd
......