edit interfaces file under /etc/network in Raspber

2019-08-27 15:32发布

问题:

I'm currently working with RaspberryPi-3 with Rasbian installed. I want to auto-reconnect to a network to a specific network. Going through the internet, I found out a way to do it i.e. by editing the interfaces file under /etc/network. I want to edit this file using some script (preferably Python-3). I just need to add these lines to the interfaces file:

auto wlan0
iface wlan0 inet dhcp
    wpa-ssid <my-SSID>
    wpa-psk <my-PassKey>

Please help me regarding this issue.

回答1:

If you have no specific reason to do that in Python I'd suggest a simple shell script like:

MYSSID=WiFi1
WIFIPW=Zekrett1

cat >> /etc/network/interfaces << EoNet
auto wlan0
iface wlan0 inet dhcp
    wpa-ssid $MYSSID
    wpa-psk  $WIFIPW
EoNet

the same in Python 2/3:

ssid='WiFi1'
wifipw='Zekrett1'

with open('/etc/network/interfaces', 'a') as netcfg:
    netcfg.write('auto wlan0\n'
                 'iface wlan0 inet dhcp\n'
                 '    wpa-ssid {}\n'
                 '    wpa-psk  {}\n'.format(ssid, wifipw))