使用Python在Raspberry-Pi中的/etc/network下编辑interfaces文件 [英] edit interfaces file under /etc/network in Raspberry-Pi using Python
问题描述
我目前正在使用已安装Rasbian的RaspberryPi-3.我想自动重新连接到特定网络的网络.通过互联网,我找到了一种实现方法,即通过编辑/etc/network下的interfaces文件.我想使用一些脚本(最好是Python-3)来编辑此文件.我只需要将这些行添加到interfaces文件中即可:
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.
推荐答案
如果您没有在Python中执行此操作的特定原因,建议您使用以下简单的shell脚本:
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
与Python 2/3相同:
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))
这篇关于使用Python在Raspberry-Pi中的/etc/network下编辑interfaces文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!