edit interfaces file under /etc/network in Raspberry-Pi using Python

 ̄綄美尐妖づ 提交于 2020-01-25 07:27:27

问题


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))


来源:https://stackoverflow.com/questions/54312879/edit-interfaces-file-under-etc-network-in-raspberry-pi-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!