Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am writing a python3 script to automate installation of radius and daloradius. in the middle, am getting some error. my code is :

import subprocess
import os
import sys

# Updating Resources
os.system("sudo apt-get update -y")


#subprocess.call(["apt-get", "install", "freeradius"])

#Installing Radius & Dependencies
print("\n Installing Free Radius ... \n")
os.system("sudo apt-get install freeradius freeradius-mysql -y")
print("\nInstalling  LAMP  ... \n")
os.system("sudo apt-get install mysql-server mysql-client apache2 php5 libapache2-mod-php5 php5-mysql php5-common php5-gd php-pear php-db php-mail -y")
#os.system("sudo apt-get install python-dev libmysqlclient-dev python3-pip")
#os.system("sudo pip3 install mysqlclient")

#Creating A Database for radius server
os.system("mysql -h localhost -uroot -p123 -e 'CREATE DATABASE foo';")
os.system("mysql -h localhost -uroot -p123 -e 'CREATE USER 'radius2'@'localhost' IDENTIFIED BY 'radpass'';")
os.system("mysql -h localhost -uroot -p123 -e 'GRANT ALL PRIVILEGES ON `radius` . * TO 'radius2'@'localhost'';")

I am getting an error at

os.system("mysql -h localhost -uroot -p123 -e 'CREATE USER 'radius2'@'localhost' IDENTIFIED BY 'radpass'';")

The error is :

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'radpass' at line 1
share|improve this question
1  
Try os.system ("mysql -h localhost -uroot -p123 -e \"CREATE USER 'radius2'@'localhost' IDENTIFIED BY 'radpass'\"") . You'll notice I use escaped double quotes for the -e part of the command. –  Michael Petch Jun 25 at 5:11
    
Yes go with this. It was the double wrapping sunk me. Or was it triple –  Drew Jun 25 at 5:15

1 Answer 1

There are two many single quotemarks in the SQL statement, use back slash to escape them:

os.system("mysql -hlocalhost -uroot -p123 -e 'CREATE USER \'radius2\'@\'localhost\' IDENTIFIED BY \'radpass\';'")

You can use just one statement to create user and grant privilege to it:

GRANT ALL PRIVILEGES ON radius.* TO 'radius2'@'localhost' IDENTIFIED BY 'radpass';
share|improve this answer
1  

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.