|
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
# Project: Wicd -> Network Manager
|
|
# Module: wicdnm.py
|
|
# Purpose: AConvert wicd configuration files to NM system configurations
|
|
# Language: Python 2.5
|
|
# Date: 24-May-2011.
|
|
# File Version: 24-May-2011
|
|
# Author: José L. Redrejo Rodríguez
|
|
# Copyright: 2011 - José L. Redrejo Rodríguez <jredrejo @nospam@ debian.org>
|
|
#
|
|
|
|
# This file is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
# wicd2nm.py is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this file. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
# Important: WHEN EDITING THIS FILE, USE TABS TO INDENT - NOT SPACES!
|
|
#
|
|
# More important: This script has been done with the public schools laptops
|
|
# of Extremadura in mind. It WILL fail in some cases that are not possible
|
|
# in these laptops, so don't use it as a general wicd to nm configuration
|
|
# conversion script, because there are not contempled case uses.
|
|
##############################################################################
|
|
|
|
import ConfigParser
|
|
import os.path
|
|
from os import makedirs,chmod
|
|
from uuid import uuid1
|
|
|
|
wicd_config='/etc/wicd/wireless-settings.conf'
|
|
nm_config_dir='/etc/NetworkManager/system-connections'
|
|
|
|
|
|
def parse_wicd():
|
|
Connections=[]
|
|
my_parser = ConfigParser.ConfigParser()
|
|
archivo_config = my_parser.read(wicd_config)
|
|
redes=my_parser.sections()
|
|
opciones=('passphrase','bssid','essid','enctype','automatic','key')
|
|
for red in redes:
|
|
try:
|
|
red_info=my_parser.items(red)
|
|
datos_red={}
|
|
for i in red_info:
|
|
if i[0] in opciones:
|
|
datos_red[i[0]]=i[1]
|
|
|
|
Connections.append(datos_red)
|
|
except:
|
|
pass #red mal construida
|
|
|
|
return Connections
|
|
|
|
def string2ascii(cadena):
|
|
ascii=''
|
|
for car in cadena:
|
|
ascii +=str(ord(car)) + ";"
|
|
return ascii
|
|
|
|
|
|
def crea_nm_configs(conexiones):
|
|
if not os.path.exists(nm_config_dir): makedirs(nm_config_dir)
|
|
for con in conexiones:
|
|
my_parser=ConfigParser.RawConfigParser()
|
|
my_parser.add_section('connection')
|
|
my_parser.add_section('802-11-wireless-security')
|
|
my_parser.add_section('ipv4')
|
|
my_parser.add_section('ipv6')
|
|
my_parser.add_section('802-11-wireless')
|
|
my_parser.set('connection','uuid',uuid1())
|
|
my_parser.set('connection','timestamp','1305878385')
|
|
my_parser.set('connection','type','802-11-wireless')
|
|
my_parser.set('802-11-wireless','mode','infrastructure')
|
|
my_parser.set('802-11-wireless','security','802-11-wireless-security')
|
|
my_parser.set('ipv4','method','auto')
|
|
my_parser.set('ipv6','method','ignore')
|
|
|
|
try:
|
|
id='Auto ' + con['essid']
|
|
my_parser.set('connection','id',id)
|
|
my_parser.set('802-11-wireless','seen-bssids',con['bssid'] + ';')
|
|
my_parser.set('802-11-wireless','ssid',string2ascii(con['essid']))
|
|
if 'passphrase' in con.keys():
|
|
contras=con['passphrase']
|
|
else:
|
|
contras=con['key']
|
|
|
|
if con['enctype'] == 'wep-passphrase' or con['enctype'] == 'wep-hex':
|
|
my_parser.set('802-11-wireless-security','key-mgmt','none')
|
|
my_parser.set('802-11-wireless-security','auth-alg','open')
|
|
my_parser.set('802-11-wireless-security','wep-key0',contras)
|
|
if con['enctype'] == 'wep-passphrase':
|
|
tipo=2
|
|
else:
|
|
tipo=1
|
|
my_parser.set('802-11-wireless-security','wep-key-type',tipo)
|
|
|
|
elif con['enctype'] == 'wpa-psk' or con['enctype'] == 'wpa':
|
|
my_parser.set('802-11-wireless-security','key-mgmt','wpa-psk')
|
|
my_parser.set('802-11-wireless-security','psk',contras)
|
|
|
|
else:
|
|
continue #caso no contemplado
|
|
|
|
configfile=open(os.path.join(nm_config_dir,id), 'wb')
|
|
my_parser.write(configfile)
|
|
configfile.close()
|
|
chmod(os.path.join(nm_config_dir,id),0600)
|
|
|
|
|
|
except:
|
|
#import pdb
|
|
#pdb.set_trace()
|
|
continue #hay algún dato mal en la configuración, así que tomamos la siguiente
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
conn=parse_wicd()
|
|
crea_nm_configs(conn)
|
|
#creamos archivo vacío de testigo de haber ejecutado el script
|
|
if not os.path.exists('/var/wicd2nm'): open('/var/wicd2nm', 'w').close()
|