linux修改ip,mac命令和qt中编程修改

linux修改Mac地址

有三种方法:
方法一:
1.关闭网卡设备
ifconfig eth0 down
2.修改MAC地址
ifconfig eth0 hw ether MAC地址
3.重启网卡
ifconfig eth0 up

方法二:
以上方法一修改后linux重启后MAC又恢复为原来的,为了下次启动时修改后的MAC仍有效,我们可以修改文件file:/etc/rc.d/rc.sysinit(RedFlag Linux为这个文件,其他版本的linux应该不同)的内容,在该文件末尾加以下内容:
ifconfig eth0 down
ifconfig eth0 hw ether MAC地址
ifconfig eth0 up

方法三:
很简单的,只是在./etc/sysconfig/network-scripts/ifcfg-eth0中加入下面一句话: 
MACADDR=00:AA:BB:CC:DD:EE
 

linux修改IP地址

(1)Ifconfig命令 

第一种使用ifconfig命令配置网卡的ip地址。此命令通常用来零时的测试用,计算机启动后

ip地址的配置将自动失效。具体用法如下。ifconfig  eth0   ipadd    netmask      x.x.x.x。

子网掩码:ifconfig eth0 netmask x.x.x.x

其中ethx中的x代表第几快以太网卡,默认第一块为0.ipadd代表ip地址。x.x.x..x为子网掩码。例如给网卡eth0配置的ip地址为192.168.1.1 子网掩码为 255.255.255.0 。

(2)vi  /etc/sysconfig/network-scripts/ifcfg-eth0

配置完以后重启动服务.,p地址就配置好了。其实前面3个的配置方法最终还是改变了/etc/sysconfig/network-scripts/ifcfg-eth0下的配置文件罢了。

linux修改网关地址


 # route add default gw xxx.xxx.xxx.xxx(网关的IP)

  如果用户想更改网关,编辑/etc/sysconfig/network-scripts/ifcfg-eth0文件,将对应的网关设置修改之后,执行下面的命令即可:

  #/etc/init.d/network restart

Qt中如何获取主机名、IP、端口、MAC等信息?
主要使用到的类有:
  • QHostInfo
  • QHostAddress
  • QNetworkInterface
  • QNetworkAddressEntry
1.获取登录用户

QString name = qgetenv("USERNAME");
qDebug() << "User Name:" << name;
输出:
User Name:"wangl-d"

2.获取主机名、IP地址

//QHostInfo类作用,获取主机名,也可以通过主机名来查找IP地址,或者通过IP地址来反向查找主机名。
QString localHostName = QHostInfo::localHostName();
qDebug() << "LocalHostName:" << localHostName;

//获取IP地址
QHostInfo info = QHostInfo::fromName(localHostName);
qDebug() << "IP Address:" << info.addresses();

foreach(QHostAddress address, info.addresses())
{
    if (address.protocol() == QAbstractSocket::IPv4Protocol)
        qDebug() << "IPv4 Address:" << address.toString();
}

foreach (QHostAddress address, QNetworkInterface::allAddresses())
{
    qDebug() << "Address:" << address;
}
输出:

LocalHostName:"wangl-d-pc"

IPAddress: (QHostAddress("fe80::396e:183d:aac6:f7f2") , QHostAddress("192.168.118.55" ) )

IPv4Address: "192.168.118.55"

Address:QHostAddress( "fe80::396e:183d:aac6:f7f2" )

Address:QHostAddress( "192.168.118.55" )

Address:QHostAddress( "::1" )

Address:QHostAddress( "127.0.0.1" )

Address:QHostAddress( "fe80::5efe:c0a8:7637" )

Address:QHostAddress( "fe80::e0:0:0:0" )  


3.获取设备名、MAC地址、子网掩码、广播地址等

//获取所有网络接口的列表
foreach (QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
    //设备名
    qDebug() << "Device:" << netInterface.name();

    //MAC地址
    qDebug() << "HardwareAddress:" << netInterface.hardwareAddress();

    QList entryList = netInterface.addressEntries();

    //遍历每一个IP地址(每个包含一个IP地址,一个子网掩码和一个广播地址)
    foreach(QNetworkAddressEntry entry, entryList)
    {
        //IP地址
        qDebug() << "IP Address:" << entry.ip().toString();

        //子网掩码
        qDebug() << "Netmask:" << entry.netmask().toString();

        //广播地址
        qDebug() << "Broadcast:" << entry.broadcast().toString();
    }
}
输出:  

Device:"{4C5C2613-C18F-416A-A21F-47B707C3B935}"

HardwareAddress:"74:D4:35:1B:A6:2F"

IPAddress: "fe80::396e:183d:aac6:f7f2"

Netmask:"ffff:ffff:ffff:ffff::"

Broadcast:""

IPAddress: "192.168.118.55"

Netmask:"255.255.255.0"

Broadcast:"192.168.118.255"

Device:"{846EE342-7039-11DE-9D20-806E6F6E6963}"

HardwareAddress:""

IPAddress: "::1"

Netmask:"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"

Broadcast:""

IPAddress: "127.0.0.1"

Netmask:""

Broadcast:""

Device:"{539F3167-F67C-432D-8904-34129F3F5521}"

HardwareAddress:"00:00:00:00:00:00:00:E0"

IPAddress: "fe80::5efe:c0a8:7637"

Netmask:"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"

Broadcast:""

Device:"{5DECB4C5-20AC-4BD6-A685-9FE608ED6EF5}"

HardwareAddress:"00:00:00:00:00:00:00:E0"

IPAddress: "fe80::e0:0:0:0"

Netmask:"ffff:ffff:ffff:ffff::"

Broadcast:"" 



你可能感兴趣的:(QT,ip,mac,gw,netmask)