JurgenRoman
Posts: 33
Joined: Mon Aug 24, 2015 7:43 am

changing static ip through python

Tue Nov 03, 2015 4:09 pm

I want to have some files like:
/etc/network/interfaces
/etc/dnsmasq.conf

to be updated with the right ip-address when i hook up my pi to another network, where it'll get a different ip-address

i tried the following method:

Code: Select all

from subprocess import *

def run_cmd(cmd)
    p = Popen(cmd, shell=True, stdout = PIPE)
    output = p.communicate()[0]
    return output

ipaddrCODEDbytes = run_cmd("hostname -I")
ipaddrDouble = ipaddrCODEDbytes.decode('utf-8')
ipaddrSpaceIND = ipaddrDouble.index(' ')
ipaddr = ipaddrDouble[:ipaddrSpaceIND] #I know it can be done in like 1 line, but i want to keep it easy to read
with open("IPaddr.txt", "w+") as file:
    file.write(str(ipaddr))

#this writes the ip address to that file just fine
now that i have the ip address written to a file, i try to use configparser to change my value in /etc/network/interfaces
(value currently is:
address 10.0.1.15
)

Code: Select all

import configparser
Config = configparser.ConfigParser()

networkInterfaces = open("/etc/network/interfaces", "r+")
ipaddr = open("IPaddr.txt", "r").read()
Config.set("address", str(ipaddr)) #this line gives the error
Config.write(networkInterfaces)
networkInterfaces.close()
i get the following error message:
TypeError: option values must be strings
(error is in the indicated line 6 above)


User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: changing static ip through python

Wed Nov 04, 2015 1:07 pm

Have you looked at (e.g. printed out) the value of 'ipaddress' after reading it in? If that looks sensible, check type(ipaddress), str(ipaddress), and type(str(ipaddress)).

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: changing static ip through python

Wed Nov 04, 2015 4:58 pm

JurgenRoman wrote:networkInterfaces = open("/etc/network/interfaces", "r+")
ipaddr = open("IPaddr.txt", "r").read()
Config.set("address", str(ipaddr)) #this line gives the error

i get the following error message:
TypeError: option values must be strings
(error is in the indicated line 6 above)
Jump into the REPL and try this out. It works with an ordinary file but when you try the interfaces one, try to open it with r+, it fails because you don't have root privileges. Then when you get further down, oh the wonder of dynamically typed languages!!!, it gives you an error that it's not a string var. Of course it's not, it failed and didn't return what it was supposed to.

If you'd just used a statically typed language like Grandma Eecs recommended back when she used to set you on her knee and discuss computer science, maybe it wouldn't be caught at compile time because it wouldn't be known that the file read would fail but at least the type error and location could make more sense. But no, like most of us, you thought she was all about the delicious home baked sugar cookies and farm fresh milk, now we are older and know dynamic typing will all end in tears.

gkreidl
Posts: 6139
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: changing static ip through python

Wed Nov 04, 2015 5:18 pm

stderr wrote:...
If you'd just used a statically typed language like Grandma Eecs recommended back when she used to set you on her knee and discuss computer science, maybe it wouldn't be caught at compile time because it wouldn't be known that the file read would fail but at least the type error and location could make more sense. But no, like most of us, you thought she was all about the delicious home baked sugar cookies and farm fresh milk, now we are older and know dynamic typing will all end in tears.
Oh yeah, back to the stone age, just because some people don't know what they are doing.
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: changing static ip through python

Wed Nov 04, 2015 7:07 pm

gkreidl wrote:.Oh yeah, back to the stone age,
The Stone Age? Obsidian can be made razor sharp and it doesn't cause global warming, which is certainly frowned upon today in the coal dependant iron making industries. Stone Age might be good.

But I'm not so sure that static typing is a step backward, many current languages support type inference, certainly for anything simple and obvious that you are probably not wanting to take the time to go on about.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: changing static ip through python

Thu Nov 05, 2015 5:12 pm

With updated versions of Wheezy and the new Jessie the static address stuff moved to /etc/dhcpcd.conf

Code: Select all

interface eth0 
static ip_address=192.168.1.9/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8
which is probably easier to handle with configparser.

If you want find your IP address in python try this

Code: Select all

#!/usr/bin/python3
import socket
import os
testIP = "8.8.8.8" # Or your home router eg. testIP = "192.168.1.1"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((testIP, 0))
ipaddr = s.getsockname()[0]
host = socket.gethostname()
print ("IP:", ipaddr, " Host:", host)
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Troubleshooting”