Jump to content

Recommended Posts

Hi, Everyone is there any possible way to change the IP address of tinker board using python script. I tried to change it using script, changes reflecting in network file properly (/etc/NetworkManager/system-connections/Wired Ethernet1.nmconnection). But It's creating new connection eth0  and taking auto IP. Not taking my IP, anyone have any idea about this my guess is because of edit maybe format change will happens because of that its not changing I think.  File also attached . 

Eg.

[connection]
id=Ethernet connection 1
uuid=940351fd-af24-4694-ab43-65f8560e76d7
type=ethernet
permissions=

[ethernet]
mac-address-blacklist=

[ipv4]
address1= 192.168.1.220/24,192.168.1.10
dns8.8.8.8 4.2.2.2
dns-search=
method=manual

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
ip6-privacy=0
method=auto

Wired1.nmconnection

Share this post


Link to post
Share on other sites

Well, I search about it and found this code that may help you to do what you are looking for.

Quote

 

import netifaces
from configobj import ConfigObj

def change_ip_address(interface_name, new_ip, new_subnet, new_gateway):
    iface = netifaces.ifaddresses(interface_name)
    current_ip = iface[netifaces.AF_INET][0]['addr']
    current_subnet = iface[netifaces.AF_INET][0]['netmask']

    config = ConfigObj('/etc/NetworkManager/system-connections/Wired Ethernet1.nmconnection', encoding='utf8')
    config['ipv4']['address1'] = f"{new_ip}/{new_subnet}"
    config['ipv4']['gateway'] = new_gateway
    config.write()

    print(f"IP address for {interface_name} changed from {current_ip}/{current_subnet} to {new_ip}/{new_subnet}")

interface_name = 'eth0'
new_ip = '192.168.1.220'
new_subnet = '24'
new_gateway = '192.168.1.1'

change_ip_address(interface_name, new_ip, new_subnet, new_gateway)

 

Quote

 


 

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...