Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
1,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. #!/bin/bash
  2. #title :importnordvpn.sh
  3. #description :This script batch import ovpn files .
  4. #author :dzaczek consolechars.wordpress.com
  5. #date :20170227
  6. #version :0.4.2a
  7. #usage :./bash mkscript.sh -u [username] -p [password] -d [directory with ovpn configs] || -g
  8. #notes :Install NetworkManager.x86_64 NetworkManager-openvpn.x86_64 NetworkManager-openvpn-gnome.x86_64 awk
  9. #notes : Script reqquired time, for add 1583 vpn config needed 3h 2m
  10. #==============================================================================
  11. sessionname="$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c 6;echo;)"
  12. target="/tmp/$sessionname/nordvpn.zip"
  13. target_1=/tmp/$sessionname/
  14. nmclisysttemconnections="/etc/NetworkManager/system-connections"
  15. nmclibuffer="/tmp/$sessionname/bufer"
  16. nmclitmpfs="/etc/NetworkManager/tmpfs"
  17. bck=$PWD
  18. wnump=0
  19. #!/bin/bash
  20. ttt=$(ps ax | grep $$ | grep -v grep | awk '{print $2}')
  21. terminal="/dev/$ttt"
  22. #rows=$(stty -a <"$terminal" | grep -Po '(?<=rows )\d+')
  23. start=`date +%s`
  24.  
  25.  
  26.  
  27. runtime=$((end-start))
  28. nice_output(){
  29. clear
  30. columns=$(stty -a <"$terminal" | grep -Po '(?<=columns )\d+')
  31. #echo "Progress BARR"
  32. precenteage=$(echo "(($1*100/$2))/1" | bc )
  33. in="$precenteage/100%"
  34. sizbar=$(($columns-${#in}-7))
  35. p1=$(echo "(($precenteage*$sizbar)/100)/1" | bc )
  36. arr=$5
  37. precenteage1=$p1
  38. precenteage2=$((sizbar-p1))
  39. echo "Session name: $sessionname"
  40. echo -n -e "\n\n\n\n\n \t\t\tImporting Files.$1/$2\t $6 \n\n\n"
  41. end=`date +%s`
  42. echo -n -e "\t\t\t Script Working `date -d@$((end-$3)) -u +%H:%M:%S` seconds \n \t\t\t ETA : `date -d@$(echo "($2-$1)*$4" |bc -l) -u +%H:%M:%S`\n ${arr[@]}\n"
  43.  
  44.  
  45. #___________Progress___BAR______________________________
  46. echo -n "$in"
  47. echo -n -e "["
  48. #echo -n -e "\n"
  49. for ((i=0;i<=precenteage1;i++)); do
  50. echo -n -e "\e[44m#\e[0m"
  51. done
  52.  
  53. for ((i=0;i<precenteage2;i++)); do
  54. echo -n -e "\e[100m-\e[0m"
  55. done
  56.  
  57. echo -n -e "]"
  58. echo -n -e "100% \n"
  59. #______________________________________________
  60. }
  61.  
  62.  
  63. remove_all_vpn(){
  64. #remove all vpn utill any vpn conncetion is on a list
  65.  
  66. echo "Connection VPN removed"
  67. }
  68.  
  69. get_ovpn_files(){
  70. #get form network vpn-config files
  71.  
  72. echo "I cant download ovpn files check internet connection"
  73.  
  74. }
  75.  
  76. backupnmcliconnections() {
  77. #create backup ncli connections
  78. sudo tar -cvf ~/backupNMCLI-$sessionname.tar $nmclisysttemconnections
  79. if [ $? -eq 0 ]; then
  80. echo "Backuped $nmclisysttemconnections in home directory file : backupNMCLI-$sessionname.tar"
  81. if hash xz 2>/dev/null;then
  82. xz -9 ~/backupNMCLI-$sessionname.tar && echo "Compressed backup" &
  83. else
  84. echo "Nooooo xz consuela say nononono nono no no packing "
  85. fi
  86. else
  87. echo "Na backuped "
  88. fi
  89. }
  90.  
  91.  
  92.  
  93. moveconfigsfromramdisk() {
  94. echo "removed"
  95.  
  96. }
  97.  
  98. import_files_to_nmcli(){
  99. dxa=6
  100. dxb=0
  101. dbl=( )
  102. flags=30
  103. echo "Added :"
  104. printf '%s\n' "$a" | while IFS= read -r line
  105. do
  106. if [ "x" != "x$arr" ]; then
  107. if [ $flags -eq 0 ];then
  108.  
  109. flags=11
  110. fi
  111. fi
  112. flags=$(($flags-1))
  113. start_loop1=`date +%s.%N`
  114. wnump=$(($wnump+1))
  115. # dxb=$(($dxb+1))
  116. #prepare short name for connection
  117. conname=`echo $line | awk -F "." '{print $1"-"$4}' `
  118. # add/import connection to nmcli and grap uuid by regex in awk
  119. uuidcon=$(nmcli connection import $temp8 type openvpn file $line | awk 'match($0, /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/) {print substr($0, RSTART, RLENGTH)}')
  120. #reneme conenction and add username and password
  121. nmcli con mod $temp8 uuid $uuidcon connection.id $conname +vpn.data "username=$USERNAMEFORVPN" vpn.secrets password="$PASSWFORVPN"
  122. if [ $dxb -eq $dxa ];then echo -n -e "\n";dxb=0;else dbl[dxb]="$(echo "scale=3;$(date +%s.%N)-$start_loop1"| bc -l)";dxb=$(($dxb+1)); fi
  123. average_nmcli_loop=$(echo "scale=2;($(echo ${dbl[*]}| tr ' ' '+'))/${#dbl[*]}" | bc -l )
  124. #echo "$average_nmcli_loop"
  125. #echo ${dbl[*]}
  126.  
  127. nice_output $wnump $numfiles $start $average_nmcli_loop "${dbl[*]}" $conname
  128. #echo -n -e "\e[$((31+$dxb))m$conname\e[0m\t" ; if [ $dxb -eq $dxa ];then echo -n -e "\n";dxb=0; fi
  129. # echo -e "$wnump. Added $conname:\t $uuidcon" #verbose
  130.  
  131.  
  132. done
  133. echo "Loops $wnump"
  134. }
  135.  
  136.  
  137.  
  138.  
  139. while getopts ":u:p:d:c h g t r" opt; do
  140. case $opt in
  141. u) au=$OPTARG ;;
  142. p) ap=$OPTARG ;;
  143. c) ac=1 ;;
  144. d) ad=$OPTARG ;;
  145. h) ah=1 ;;
  146. t) att=1 ;;
  147. r) arr=1 ;;
  148. g) ag=1 ;;
  149. \?) echo "Invalid option: -$OPTARG\n Please use parameter -h for help" >&2
  150. exit 1 ;;
  151.  
  152. esac
  153. done
  154.  
  155.  
  156. if [ "$#" == 0 ]; then
  157.  
  158. echo "Parameter do not found please use -h for help" ; exit 1;
  159. exit 1
  160. fi
  161. backupnmcliconnections
  162. #check if -h print help end exit
  163. if [ "x" != "x$ah" ]; then
  164. cat << EOF
  165. script batch adding openvpn nordvpn configs to nmcli
  166. #time adding 1583 VPN'S 3:02:17.37 total
  167. #time adin 2197 7h
  168. #time adding 2197 wuth -r parameter form 30 minutes to 1h 20 minutes
  169. usage:
  170. ./importnordvpn.sh [-u <"username"> -p <"password">][-h][-d <"directory"> || -g][-c]
  171. -u username (is mail ) it must be in qoutes " "
  172. -p password it must be in qoutes " "
  173. -d patch to direcotory ovpn files arguments not required
  174. you can run script in direcotry white space in patch
  175. not working.
  176. -g Get configs from network.
  177. -c clean DANGER, roemove all connection type vpn from nmcli
  178. -t Temporary use this for test, added configuration
  179. disaper after restart NetworkManager (nmcli)
  180. -h it is this information
  181. -r Test function for fast add servers , all operations
  182. works on ram disk and NetwormManager is restarted every 30 new added configs
  183. examples:
  184. ./importnordvpn -u "myemail@exampl.com" -p "P44SSwoRd"
  185. or
  186. ./importnordvpn -u "myemail@exampl.com" -p "P44SSwoRd" -d Download/configs/
  187. Get configuration from nordvpn.com
  188. ./importnordvpn -u "myemail@exampl.com" -p "P44SSwoRd" -d
  189. if you want clean configuration
  190. ./importnordvpn -c
  191. clean configuration (remove all vpns from nmcli ). and load new
  192. ./importnordvpn -c -u "myemail@exampl.com" -p "P44SSwoRd" -d Download/configs/
  193. __________________________________________________________
  194. Report bugs to:dzaczek[animaleatingyellow fruit]sysop.cat
  195. up home page:https://consolechars.wordpress.com/
  196. ______________)____________________________________________
  197. EOF
  198.  
  199. exit 1
  200. fi
  201. #check if -c if exist remove all vpn
  202. if [ "x" != "x$ac" ]; then
  203.  
  204. #check if username and password id declarated if not exit
  205. if [ "x" == "x$au" ] && [ "x" == "x$ap" ]; then
  206. exit 1
  207. fi
  208. fi
  209. #checked if username declarated
  210. if [ "x" == "x$au" ]; then
  211. echo "-u [username] is required"
  212. exit 1
  213. fi
  214. #checked if password is declarated
  215. if [ "x" == "x$ap" ]; then
  216. echo "-p [password] is required"
  217. exit 1
  218. fi
  219. if [ "x" != "x$att" ]; then
  220. temp8="--temporary"
  221. echo "ok";
  222. else
  223. temp8=""
  224. fi
  225. #checked id direcotry is delcarated
  226. if [ "x" != "x$ad" ] ; then
  227. cd $ad 2>/dev/null
  228. #chek if -d patch is able to cd if not exit
  229. if [ $? -eq 1 ]; then
  230. echo "-d $ad wrong patch to directory"
  231. exit 1
  232. fi
  233. fi
  234. #check if parameter -g
  235. if [ "x" != "x$ag" ] ; then
  236. #get ovpn config files
  237. mkdir $target_1
  238. get_ovpn_files
  239. #go to diretory with ovpn config files
  240. cd $target_1 2>/dev/null
  241. echo $PWD
  242. #chek if -g patch is able to cd if not exit
  243. if [ $? -eq 1 ]; then
  244. echo "-d $target_1 wrong patch to directory"
  245. exit 1
  246. fi
  247. fi
  248.  
  249.  
  250. #assign varaibles
  251. USERNAMEFORVPN=$au
  252. PASSWFORVPN=$ap
  253. #ssign to vataible a all files *.vpn in directory
  254.  
  255. a=$(ls *.ovpn)
  256. numfiles=$(echo $a |wc | awk '{print $2}')
  257. #check if not len a eq 0
  258. if [[ "$numfiles" -eq 0 ]]; then
  259. echo "Ovpn filne in $PWD do not found "
  260. exit 1
  261. fi
  262.  
  263. mkdir $target_1
  264.  
  265.  
  266.  
  267. import_files_to_nmcli
  268. if [ "x" != "x$arr" ]; then
  269. moveconfigsfromramdisk
  270. fi
  271.  
  272.  
  273. # Program strat here iterating a line by line and adding
  274. if [ "x" != "x$ag" ] ; then
  275. cd $bck
  276. rm -fr $target_1 2>/dev/null
  277. #chek if -g patch is able to rm if not exit
  278. if [ $? -eq 1 ]; then
  279. echo "-d $target_1 wrong patch to directory i can remove directory"
  280. exit 1
  281. fi
  282. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement