简体   繁体   English

通过 Python 更改 Raspberry PI ip 地址

[英]Changing Raspberry PI ip address Via Python

I am making a user application that we will deploy on machines.我正在制作一个我们将部署在机器上的用户应用程序。 The end user will have little linux experience so I wanted in our GUI to give them an option to set the IP.最终用户几乎没有 linux 经验,所以我想在我们的 GUI 中为他们提供设置 IP 的选项。 It seems to take the IP but loses it over reboot.它似乎占用了 IP 但在重新启动时丢失了它。 I am using netifaces to read the IP and system commands to set it.我正在使用 netifaces 读取 IP 和系统命令来设置它。 Inside python or the linux cmd line I am seeing the same result.在 python 或 linux cmd 行内,我看到了相同的结果。 After an change ifconfig shows the change.更改后 ifconfig 显示更改。 After a reboot it reverts back.重新启动后它会恢复。 Do I need to modify the eth config file?我需要修改 eth 配置文件吗?

import netifaces as ni
from os import system

def getIPs():
    #Grab Current IP Address
    eth0 = ni.ifaddresses('eth0')[2][0]['addr']
    wlan0 = ni.ifaddresses('wlan0')[2][0]['addr']
    return eth0, wlan0

def setEth0(ipAddress):
    if ipAddress != "":
        system('sudo ifconfig eth0 down')
        system(f'sudo ifconfig eth0 {ipAddress}')
        system('sudo ifconfig eth0 up')

def setWlan0(ipAddress):
    if ipAddress != "":
        system('sudo ifconfig wlan0 down')
        system(f'sudo ifconfig wlan0 {ipAddress}')
        system('sudo ifconfig wlan0 up')

I changed the process to actually modify the following file instead of sending the system commands.我将过程更改为实际修改以下文件,而不是发送系统命令。

/etc/network/interfaces.d/eth0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM