Page 1 of 1

bash for creating users

PostPosted: Sat Mar 03, 2012 3:26 pm
by gisclace
Hello everyone, I installed subsonic on a server, everything went off without worries ^ _ ^
I would like to know if I can create users with the terminal, my goal is to centralize the creation of my users all the tools in one bash.

Thank you in advance, and sorry for my English came from France.

Re: bash for creating users

PostPosted: Mon Mar 05, 2012 10:47 pm
by paulodell
gisclace wrote:Hello everyone, I installed subsonic on a server, everything went off without worries ^ _ ^
I would like to know if I can create users with the terminal, my goal is to centralize the creation of my users all the tools in one bash.

Thank you in advance, and sorry for my English came from France.

I am not familiar with bash but if it can run a url you could create a user by utilising the Subsonic API - http://www.subsonic.org/pages/api.jsp
Look at createUser section...http://your-server/rest/createUser.view

Paul

Re: bash for creating users

PostPosted: Fri May 11, 2012 5:11 pm
by crazybigdan
Bonjour, permettez moi essaier mon français (je suis un américain qui est étudier en France pour l'instant) et après je utilserai anglais, ma première langue.
Je viens d'écrire ça ici:
Code: Select all
#!/bin/bash

server=""
port=""
admin_username=""
admin_password=""
username=""
password=""
email=""
nc_flag=0
server_default="localhost"
port_default=4040

rest_version="1.1.0"
client_str="bash_script_v0.1"

for ((i=1;i<=$#;i++)); do
   case "${!i}" in
      -u)
         let i++
         admin_username=${!i}
      ;;
      -pw)
         let i++
         admin_password=${!i}
      ;;
      -nu)
         let i++
         username=${!i}
      ;;
      -np)
         let i++
         password=${!i}
      ;;
      -e)
         let i++
         email=${!i}
      ;;
      -[hs])
         let i++
         server=${!i}
      ;;
      -p)
         let i++
         port=${!i}
      ;;
      -n)
         nc_flag=1
      ;;
   esac
done

if [ ! "$server" ]; then
   echo -n "Enter server [localhost]: "
   read server
   server=${server:-$server_default}
fi
if [ ! "$port" ]; then
   echo -n "Enter port [80]: "
   read port
   port=${port:-$port_defalt}
fi
if [ ! "$admin_username" ]; then
   echo -n "Enter an admin username: "
   read admin_username
fi
if [ ! "$admin_password" ]; then
   echo -n "Enter password of $admin_username: "
   stty -echo
   read admin_password
   stty echo
   echo ""
fi
if [ ! "$username" ]; then
   echo -n "Enter new user: "
   read username
fi
if [ ! "$password" ]; then
   echo -n "Enter new password for $username: "
   stty -echo
   read password
   stty echo
   echo ""
fi
if [ ! "$email" ]; then
   echo -n "Enter $username's email: "
   read email
fi

rest_call() {
   echo -ne "GET /rest/createUser.view?u=$admin_username&p=$admin_password&v=$rest_version&c=$client_str&username=$username&password=$password&email=$email HTTP/1.0\r\n\r\n"
}

response=""
if (($nc_flag)); then
   response=$(rest_call | nc $server $port)
   if (($?)); then
      echo "Error connecting to $server:$port"
      exit
   fi
else
   exec 2> /dev/null
   exec 9<> /dev/tcp/$server/$port
   e=$?
   exec 2>&1
   if (($e)); then
      echo "Error connecting to $server:$port"
      exit
   else
      rest_call >&9
      #cat <&9
      while read -e -u 9 line; do
         response=$response$line
      done
      exec 9>&-
      exec 9<&-
   fi
fi

e=$(echo $response | sed -n -e '/error/{s/^.*code="\([[:digit:]]*\)".*$/\1/;p}')
if (($e)); then
   echo -n "Subsonic returned an error code indicating "
   case $e in
      0)
         echo "a generic error"
      ;;
      10)
         echo "a required parameter is missing."
      ;;
      20)
         echo "an incompatible Subsonic REST protocol version. Client must upgrade."
      ;;
      30)
         echo "an incompatible Subsonic REST protocol version. Server must upgrade."
      ;;
      40)
         echo "wrong username or password."
      ;;
      50)
         echo "that $admin_username is not authorized for the given operation."
      ;;
      60)
         echo "that the trial period for the Subsonic server is over."
      ;;
      70)
         echo "that the requested data was not found."
      ;;
   esac
else
   echo 'Success! :)'
fi

Vous pouvez le mettre dans un fichier script et faites comme ça:
Code: Select all
chmod +x fichier_script.sh

et le démarrer comme ça:
Code: Select all
./fichier_script.sh

ou, si vous preferez, vous pouvez le mettre au milieu de votre fichier ".bashrc" dans votre dossier principal, entre "sub_add() {" et "}":
Code: Select all
# trucs de .bashrc
sub_add() {
# le code
}
# plus trucs


vous pouvez le démarrer avec des paramètres (ou pas):
Code: Select all
sub_add -s serveur -p 80 -u admin -pw mot_de_passe -nu nouveaux_utilisateur -np nouveaux_mot_de_passe -e addresse@email.com


le script n'utilise que bash, et sed. Il est possible qu'il ne va pas marcher parce qu'il utilse bash sous des effets sophistiques, mais sinon, le démarrez avec la parametre "-n", qui choisit netcat, en lieu de bash sophistique.
------------------------------------------------------------------------------------------

Yeah... I think that explains it all... :)

Let me know how it works out for you.

Re: bash for creating users

PostPosted: Fri May 11, 2012 5:54 pm
by BKKKPewsey
Yep thank you for posting but next time could you post in your native language as this forum is english only!
But we do allow Americans in :roll:

:mrgreen:

Re: bash for creating users

PostPosted: Fri May 11, 2012 11:14 pm
by crazybigdan
BKKKPewsey wrote:But we do allow Americans in

That exorcised a hearty chuckle from me. Well done.

The note-worthy information on the script:
That script was slapped together over the course of today and so it carries the generic "use at your own risk" disclaimer. It is all bash except for a single call to sed, to half-parse the xml returned from the REST subsonic api call. It uses special bash filehandles to handle the network connection, but since I am not sure how cross compatible that is (I tested it on gentoo and debian), you can pass the script the '-n' parameter to make it use netcat as the backend, which may or may not be on whatever *nix based system you are using, but will work if the file descriptors do not.

From there, you can run the script one of three ways (or any mixture of them):
1. Just as is. The script is interactive and you can fill the information as needed, when prompted for it.

2. Via the command parameters:
-u - username that will carry out the createUser
-pw - password of said user
-s or -h - hostname of server as domain name or ip
-p - port of that server
-nu - desired username of user to be created
-np - the password of that user
-e - email address of that user
-n - use netcat as the network backend instead of bash file descriptors

3. You can set the variables in the beginning of the script file to whatever you want, and they will stay that way and you won't need to specify them. This could be useful if you are always authenticating under the same username and password all the time, for instance.

note 1: I tried to be nice with the error handling, so hopefully the script will be descriptive enough when (err... I mean *if*) it fails.
note 2: Apologies for the foreign language in the previous post.
note 3: The OP posted two months ago, and has not signed on since then, casting doubt on whither this script will get any use...