📜 ⬆️ ⬇️

Automate the creation of VPN users in PFSense

image

I really like PFSense 2.0. Especially good is the OpenVPN server function. The server itself is configured in a few clicks ( www.youtube.com/watch?v=odjviG-KDq8 ). After that, it remains only to create users and send them an archive with the settings. But when I faced the task of transferring 70 users from ClearOS to PFSense, I was frankly depressed. Perspective drawn next. Enter “User Manager”, click the add user button, enter login, enter password, enter password again, enter last name and first name, check the box “Click to create a user certificate”, enter last name and first name, click “Save” button , go to the "OpenVPN Server" tab "Client Export", save the archive with the settings, create a letter, insert the email there, write the login, password, attach the archive with the settings, write the accompanying text and send. And so 70 times !!! Of course, if you set a goal and turn off the brain, all this can be done in a day, or even less. But I either fall asleep from stupid work or go berserk. Therefore, it was decided to automate this process. And here the despondency, now turning into despair, overtook me a second time. Because PFSense responded to my attempts to rewrite its offal with the Read-only file system, and knowing what it was doing through config.xml did not allow me to know its structure (and I suspect that it is impossible with its means). That is, on the horizon loomed gloomily, either nervous falling asleep, or biting near humanoid-type individuals. But the solution was found. If you do not want to do the work yourself, you need to write a script that will click on the buttons for you. I already see a forest of Windows users who impatiently shout out: “AutoIt, AutoIt!”. Forgive generously, I did not describe the full picture. I work from under Ubuntu. Yeah. The forest of hands has greatly thinned. There is only a couple, three. Come on, here you are, a boy in a sweater with a frantic beard making his way. How how? Right. Sit down, five. Xdotools, friends. Here is the man in blue tights with the letter S on his chest, which saved my desperate situation.


Working conditions.
The screen must be unlocked.
Must be installed Google Chrome browser. In others, it was not tested.
Admin PFSense need to open at least once. The script will not enter the login name and password, although at first this idea was.
Ubuntu 11.04 Desktop System. On others it was not checked.
The xdotools and sendemail packages must be installed.
The format of the user list should be as follows:
login first_name second_name
For example:
S_Astapov Sergey Astapov
Login must match email address.
')
Below is the code (I beg you, do not shoot me with stools. I am not a programmer. So the code does not pretend to elegance and adherence to programmer dogmas and rules).

 #!/bin/bash #      bdpath=/opt/automa #    bdusers=(`cat $bdpath/users.txt`) #      .     . fs=(`echo ${bdusers[0]} | sed -e 's/^.[a-zA-Z]\{0\}//'`) #   a=0 b=`expr $a + 1` c=`expr $b + 1` #    . bdusers[0]=$fs #    tab="xdotool key --delay 300 "Tab"" shifttab="xdotool key --delay 300 --clearmodifiers "Shift+Tab"" #  while [ ${bdusers[$a]} ] do #         . #echo "All is right?" && read #    /usr/bin/google-chrome - https://pfsense.loc/system_usermanager.php?act=new sleep 3 #   name1="${bdusers[$c]}" name2="${bdusers[$b]}" login="${bdusers[$a]}" pass=`pwgen -1n` #   #   Username i=0 while [ "$i" != "9" ] do $tab i=`expr $i + 1` done #  xdotool type $login #   Password xdotool key --delay 200 "Tab" #  xdotool type $pass xdotool key --delay 200 "Tab" xdotool type $pass #   Full Name xdotool key --delay 200 "Tab" #  xdotool type $name1 #  xdotool key space #  xdotool type $name2 #   Click to create a user certificate. i=0 while [ "$i" != "7" ] do $tab i=`expr $i + 1` done #  Click to create a user certificate. xdotool key space #  Descriptive name xdotool key --delay 30 "Tab" #  xdotool type $name1 #  xdotool key space #  xdotool type $name2 #   Save i=0 while [ "$i" != "6" ] do $tab i=`expr $i + 1` done #  Save xdotool key "Return" #  xdotool key "Ctrl+w" #    /usr/bin/google-chrome — https://pfsense.loc/vpn_openvpn_export.php sleep 3 #      i=0 while [ "$i" != "6" ] do $shifttab i=`expr $i + 1` done #   xdotool key "Return" sleep 2 #  /home/user/temp/       xdotool key slash xdotool type "home" xdotool key slash xdotool type "user" xdotool key slash xdotool type "temp" xdotool key slash xdotool type "vpn-TCP-1194-config" # Enter xdotool key "Return" sleep 3 #  xdotool key "Ctrl+w" #    ,     sendemail -f admin@company.ru -t $login@company.ru -o message-charset=utf-8 -o message-content-type=html -u "NewVPN Settings" -m "<p> .<br>   VPN .          .</p><p>name - $login <br> pass - $pass"\ -s mail.company.ru -xu admin@company.ru -xp password -a instruction.doc /home/user/temp/vpn-TCP-1194-config.zip #   rm /home/user/temp/vpn-TCP-1194-config.zip a=`expr $a + 3` b=`expr $b + 3` c=`expr $c + 3` done 
#!/bin/bash # bdpath=/opt/automa # bdusers=(`cat $bdpath/users.txt`) # . . fs=(`echo ${bdusers[0]} | sed -e 's/^.[a-zA-Z]\{0\}//'`) # a=0 b=`expr $a + 1` c=`expr $b + 1` # . bdusers[0]=$fs # tab="xdotool key --delay 300 "Tab"" shifttab="xdotool key --delay 300 --clearmodifiers "Shift+Tab"" # while [ ${bdusers[$a]} ] do # . #echo "All is right?" && read # /usr/bin/google-chrome - https://pfsense.loc/system_usermanager.php?act=new sleep 3 # name1="${bdusers[$c]}" name2="${bdusers[$b]}" login="${bdusers[$a]}" pass=`pwgen -1n` # # Username i=0 while [ "$i" != "9" ] do $tab i=`expr $i + 1` done # xdotool type $login # Password xdotool key --delay 200 "Tab" # xdotool type $pass xdotool key --delay 200 "Tab" xdotool type $pass # Full Name xdotool key --delay 200 "Tab" # xdotool type $name1 # xdotool key space # xdotool type $name2 # Click to create a user certificate. i=0 while [ "$i" != "7" ] do $tab i=`expr $i + 1` done # Click to create a user certificate. xdotool key space # Descriptive name xdotool key --delay 30 "Tab" # xdotool type $name1 # xdotool key space # xdotool type $name2 # Save i=0 while [ "$i" != "6" ] do $tab i=`expr $i + 1` done # Save xdotool key "Return" # xdotool key "Ctrl+w" # /usr/bin/google-chrome — https://pfsense.loc/vpn_openvpn_export.php sleep 3 # i=0 while [ "$i" != "6" ] do $shifttab i=`expr $i + 1` done # xdotool key "Return" sleep 2 # /home/user/temp/ xdotool key slash xdotool type "home" xdotool key slash xdotool type "user" xdotool key slash xdotool type "temp" xdotool key slash xdotool type "vpn-TCP-1194-config" # Enter xdotool key "Return" sleep 3 # xdotool key "Ctrl+w" # , sendemail -f admin@company.ru -t $login@company.ru -o message-charset=utf-8 -o message-content-type=html -u "NewVPN Settings" -m "<p> .<br> VPN . .</p><p>name - $login <br> pass - $pass"\ -s mail.company.ru -xu admin@company.ru -xp password -a instruction.doc /home/user/temp/vpn-TCP-1194-config.zip # rm /home/user/temp/vpn-TCP-1194-config.zip a=`expr $a + 3` b=`expr $b + 3` c=`expr $c + 3` done

Sophisticated readers may ask: “Why doesn't the extension fit in the file name?” And I will answer - because when you save the archive (at least for me), the selection is automatically set only to the file name. And when prescribing the path and name, the extension remains unchanged.


I strongly recommend to uncomment the line echo "All is right?" && read or make pauses longer, seconds 10-15.
Well that's all. I sincerely hope that the script will help those in my situation. And finally, the video. As it is live.

Source: https://habr.com/ru/post/132106/


All Articles