Advertisement
geofferey

[BASH] - getopts SSHD Config Example

Aug 30th, 2014
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.59 KB | None | 0 0
  1. #!/system/xbin/bash
  2. . /system/etc/sshd.conf
  3. clear
  4.  
  5. echo ""
  6. read -s -p "Enter password:" CPASS
  7. echo ""
  8. until [ "$CPASS" = "$PASS" ]; do
  9. read -s -p "Wrong password, try again:" CPASS
  10. echo ""
  11. done
  12.  
  13. mount -o remount,rw /system
  14.  
  15.  
  16. SSHD_CONFIG (){
  17. echo ""
  18. echo "---CONFIGURE-SSH-DAEMON---"
  19. read -s -p "Enter new password:" NEWPASS
  20. echo ""
  21. while [[ -z "$NEWPASS" ]]; do
  22. echo ""
  23. echo "Password CAN NOT be blank"
  24. echo ""
  25. read -s -p "Enter new password:" NEWPASS;
  26. echo ""
  27. done
  28.  
  29. sed -i -e"s/^PASS=.*/PASS=$NEWPASS/" /etc/sshd.conf
  30. if test $? -eq 0; then
  31. echo 'password written'
  32. else
  33. echo 'write attempt failed!'
  34. fi
  35.  
  36. echo ""
  37.  
  38. read -p "Enter new port: " NEWPORT
  39. if test "$NEWPORT" = ""; then
  40. echo "DEFAULT PORT #2222"; sed -i -e"s/^PORT=.*/PORT=2222/" /etc/sshd.conf
  41. else
  42. sed -i -e"s/^PORT=.*/PORT=$NEWPORT/" /etc/sshd.conf
  43. fi
  44.  
  45. if test $? -eq 0; then
  46. echo "port set"
  47. else
  48. echo "write attempt failed!"
  49. fi
  50.  
  51. echo ""
  52.  
  53. while true
  54.   do
  55.     read -r -p 'Enable SSH? ' choice
  56.   case "$choice" in
  57.     n|N|no|No|NO) echo "SSH disabled";
  58.   if [[ -e /system/etc/init.d/60dropbear ]]; then
  59.     rm /etc/init.d/60dropbear
  60.   else
  61.     break
  62.   fi;
  63.     break;;
  64.     y|Y|yes|Yes|YEs|YES|YeS|yeS|yES) echo "SSH enabled";
  65.   if [[ ! -e /system/etc/init.d/60dropbear ]]; then
  66.     touch /system/etc/init.d/60dropbear;
  67.     echo "#!/system/bin/sh" >> /system/etc/init.d/60dropbear;
  68.     echo "" >> /system/etc/init.d/60dropbear;
  69.     echo "if [[ ! -d /data/data/br.com.bott.droidsshd ]]; then
  70. ln -s /system/etc/dropbear/data/br.com.bott.droidsshd /data/data/br.com.bott.droidsshd
  71. fi" >> /system/etc/init.d/60dropbear;
  72.     echo "" >> /system/etc/init.d/60dropbear;
  73.     echo "sleep 1" >> /system/etc/init.d/60dropbear;
  74.     echo "" >> /system/etc/init.d/60dropbear;
  75.     echo "dropbear" >> /system/etc/init.d/60dropbear;
  76.     chmod 755 /etc/init.d/60dropbear;
  77.   else
  78.     break
  79.   fi; break;;
  80.     *) echo 'Nothing done';
  81.     break;;
  82.   esac
  83. done
  84.  
  85. }
  86.  
  87. REVERSE_SSH (){
  88. echo ""
  89.  
  90. echo "---CONFIGURE-REVERSE-SSH---"
  91.  
  92. echo ""
  93.  
  94. read -p "Enter reverse listen port: " NEWREVPORT
  95. if test "$NEWREVPORT" = ""; then
  96. echo "DEFAULT IS BLANK"; sed -i -e"s/^REVPORT=.*/REVPORT=/" /etc/sshd.conf
  97. else
  98. sed -i -e"s/^REVPORT=.*/REVPORT=$NEWREVPORT/" /etc/sshd.conf
  99. fi
  100.  
  101. if test $? -eq 0; then
  102. echo "port set"
  103. else
  104. echo "write attempt failed!"
  105. fi
  106.  
  107. echo ""
  108.  
  109. read -p "Enter remote server: " NEWREMSERV
  110. if test "$NEWREMSERV" = ""; then
  111. echo "DEFAULT IS BLANK"; sed -i -e"s/^REMSERV=.*/REMSERV=/" /etc/sshd.conf
  112. else
  113. sed -i -e"s/^REMSERV=.*/REMSERV=$NEWREMSERV/" /etc/sshd.conf
  114. fi
  115.  
  116. if test $? -eq 0; then
  117. echo "server set"
  118. else
  119. echo "write attempt failed!"
  120. fi
  121.  
  122. echo ""
  123.  
  124. read -p "Enter remote serv port: " NEWREMSERVPORT
  125. if test "$NEWREMSERVPORT" = ""; then
  126. echo "DEFAULT IS 22"; sed -i -e"s/^REMSERVPORT=.*/REMSERVPORT=22/" /etc/sshd.conf
  127. else
  128. sed -i -e"s/^REMSERVPORT=.*/REMSERVPORT=$NEWREMSERVPORT/" /etc/sshd.conf
  129. fi
  130.  
  131. if test $? -eq 0; then
  132. echo "port set"
  133. else
  134. echo "write attempt failed!"
  135. fi
  136.  
  137. echo ""
  138.  
  139. read -p "Enter remote username: " NEWREMUSER
  140. if test "$NEWREMUSER" = ""; then
  141. echo "DEFAULT IS BLANK"; sed -i -e"s/^REMUSER=.*/REMUSER=/" /etc/sshd.conf
  142. else
  143. sed -i -e"s/^REMUSER=.*/REMUSER=$NEWREMUSER/" /etc/sshd.conf
  144. fi
  145.  
  146. if test $? -eq 0; then
  147. echo "username set"
  148. else
  149. echo "write attempt failed!"
  150. fi
  151.  
  152. echo ""
  153.  
  154. while true
  155.   do
  156.     read -r -p 'Enable reverse ssh? ' choice
  157.     case "$choice" in
  158.     n|N|no|No|NO) echo "Reverse SSH disabled";
  159.   if [[ -e /system/etc/init.d/70sshtunnel ]]; then
  160.     rm /etc/init.d/70sshtunnel
  161.   else
  162.     break
  163.   fi;
  164.     break;;
  165.     y|Y|yes|Yes|YEs|YES|YeS|yeS|yES) echo "Reverse SSH enabled";
  166.   if [[ ! -e /system/etc/init.d/70sshtunnel ]]; then
  167.     touch /etc/init.d/70sshtunnel;
  168.     echo "#!/system/xbin/bash" >> /system/etc/init.d/70sshtunnel;
  169.     echo "" >> /system/etc/init.d/70sshtunnel;
  170.     echo "reverse_ssh >/dev/null" >> /system/etc/init.d/70sshtunnel;
  171.     chmod 755 /etc/init.d/70sshtunnel;
  172.   else
  173.     break
  174.   fi;
  175.     break;;
  176.     *) echo 'Nothing done';
  177.     break;;
  178.   esac
  179. done
  180. }
  181.  
  182. HTTP_PROXY (){
  183. echo ""
  184.  
  185. echo "---CONFIGURE-HTTP-PROXY---"
  186. echo ""
  187.  
  188. read -p "Enter socks listen port: " NEWSOCKPORT
  189. if test "$NEWSOCKPORT" = ""; then
  190. echo "DEFAULT IS BLANK"; sed -i -e"s/^SOCKSPORT=.*/SOCKSPORT=/" /etc/sshd.conf
  191. else
  192. sed -i -e"s/^SOCKSPORT=.*/SOCKSPORT=$NEWSOCKPORT/" /etc/sshd.conf
  193. fi
  194.  
  195. if test $? -eq 0; then
  196. echo "port set"
  197. else
  198. echo "write attempt failed!"
  199. fi
  200.  
  201. echo ""
  202.  
  203. while true
  204.   do
  205.     read -r -p 'Enable http tunnel? ' choice
  206.     case "$choice" in
  207.     n|N|no|No|NO) echo "HTTP tunnel disabled";
  208.    if [[ -e /system/etc/init.d/80httptunnel ]]; then
  209.      rm /etc/init.d/80httptunnel
  210.    else
  211.     break
  212.    fi; break;;
  213.     y|Y|yes|Yes|YEs|YES|YeS|yeS|yES) echo "HTTP tunnel enabled";
  214.    if [[ ! -e /system/etc/init.d/80httptunnel ]]; then
  215.     touch /system/etc/init.d/80httptunnel;
  216.     echo "#!/system/xbin/bash" >> /system/etc/init.d/80httptunnel;
  217.     echo "" >> /system/etc/init.d/80httptunnel;
  218.     echo "http_tunnel >/dev/null" >> /system/etc/init.d/80httptunnel;
  219.     chmod 755 /etc/init.d/80httptunnel;
  220.   else
  221.     break
  222.   fi; break;;
  223.     *) echo 'Nothing done';
  224.     break;;
  225.   esac
  226.   done
  227. }
  228.  
  229. DDNS (){
  230. echo ""
  231.  
  232. echo "--CONFIGURE-DDNS--"
  233. echo ""
  234.  
  235. read -p "Enter username: " DDNSUSER
  236. if test "DDNSUSER" = ""; then
  237. echo "DEFAULT UNCONFIGURED"; sed -i -e"s/^--username .*/--username/" /etc/inadyn.conf
  238. else
  239. sed -i -e"s/^--username .*/--username $DDNSUSER/" /etc/inadyn.conf
  240. fi
  241.  
  242. if test $? -eq 0; then
  243. echo "user set"
  244. else
  245. echo "write attempt failed!"
  246. fi
  247.  
  248. echo ""
  249.  
  250. read -p "Enter password: " DDNSPASS
  251. if test "DDNSPASS" = ""; then
  252. echo "DEFAULT UNCONFIGURED"; sed -i -e"s/^--password .*/--password/" /etc/inadyn.conf
  253. else
  254. sed -i -e"s/^--password .*/--password $DDNSPASS/" /etc/inadyn.conf
  255. fi
  256.  
  257. if test $? -eq 0; then
  258. echo "pass set"
  259. else
  260. echo "write attempt failed!"
  261. fi
  262.  
  263. echo ""
  264.  
  265. read -p "Enter alias: " DDNSALIAS
  266. if test "DDNSALIAS" = ""; then
  267. echo "DEFAULT UNCONFIGURED"; sed -i -e"s/^--alias .*/--alias/" /etc/inadyn.conf
  268. else
  269. sed -i -e"s/^--alias .*/--alias $DDNSALIAS/" /etc/inadyn.conf
  270. fi
  271.  
  272. if test $? -eq 0; then
  273. echo "alias set"
  274. else
  275. echo "write attempt failed!"
  276. fi
  277.  
  278. echo ""
  279.  
  280. read -p "Enter provider: " DDNSSYSTEM
  281. if test "DDNSSYSTEM" = ""; then
  282. echo "DEFAULT UNCONFIGURED"; sed -i -e"s/^--dyndns_system .*/--dyndns_system/" /etc/inadyn.conf
  283. else
  284. sed -i -e"s/^--dyndns_system .*/--dyndns_system $DDNSSYSTEM/" /etc/inadyn.conf
  285. fi
  286.  
  287. if test $? -eq 0; then
  288. echo "provider set"
  289. else
  290. echo "write attempt failed!"
  291. fi
  292.  
  293. echo ""
  294.  
  295. while true
  296.   do
  297.     read -r -p 'Enable DDNS? ' choice
  298.     case "$choice" in
  299.     n|N|no|No|NO) echo "DDNS disabled";
  300.   if [[ -e /system/etc/init.d/50inadyn ]]; then
  301.     rm /etc/init.d/50inadyn
  302.   else
  303.     break
  304.   fi;
  305.     break;;
  306.     y|Y|yes|Yes|YEs|YES|YeS|yeS|yES) echo "DDNS enabled";
  307.   if [[ ! -e /system/etc/init.d/50inadyn ]]; then
  308.     touch /etc/init.d/50inadyn;
  309.     echo "#!/system/xbin/bash" >> /system/etc/init.d/50inadyn;
  310.     echo "" >> /system/etc/init.d/50inadyn;
  311.     echo "inadyn >/dev/null" >> /system/etc/init.d/50inadyn;
  312.     chmod 755 /etc/init.d/50inadyn;
  313.   else
  314.     break
  315.   fi; break;;
  316.     *) echo 'Nothing done';
  317.     break;;
  318.   esac
  319. done
  320. }
  321.  
  322. KEYS () {
  323. echo ""
  324. echo "---REMOVE-OLD-KEYS---"
  325. echo ""
  326.  
  327. while true
  328.   do
  329.     read -r -p 'Remove old key(s)? ' choice
  330.     case "$choice" in
  331.     n|N|no|No|NO) echo "Keeping old key(s)";
  332.     break;;
  333.     y|Y|yes|Yes|YEs|YES|YeS|yeS|yES) echo "Removing old key(s)";
  334.     rm /etc/dropbear/data/br.com.bott.droidsshd/files/etc/dropbear_*_host_key;
  335.     break;;
  336.     *) echo 'Keeping old key(s)';
  337.     break;;
  338.   esac
  339.   done
  340.  
  341. echo ""
  342.  
  343. echo "---GENERATING-RSA-KEY---"; dropbearkey -t rsa -f /etc/dropbear/data/br.com.bott.droidsshd/files/etc/dropbear_rsa_host_key; chmod 400 /etc/dropbear/data/br.com.bott.droidsshd/files/etc/dropbear_rsa_host_key
  344.  
  345. echo ""
  346.  
  347. echo "---GENERATING-DSS-KEY---"; dropbearkey -t dss -f /etc/dropbear/data/br.com.bott.droidsshd/files/etc/dropbear_dss_host_key; chmod 400 /etc/dropbear/data/br.com.bott.droidsshd/files/etc/dropbear_dss_host_key
  348.  
  349. echo ""
  350.  
  351. read -p "Enter path to private key: "  PRIKEYLOC
  352. echo ""
  353. echo "---CONVERTING-PRIVATE-KEY---"; mkdir /system/etc/.ssh; mkdir /sdcard/.ssh; rm /system/etc/.ssh/reverse_ssh_key; dropbearconvert openssh dropbear "$PRIKEYLOC" /system/etc/.ssh/reverse_ssh_key; chmod 400 /system/etc/.ssh/reverse_ssh_key; cp "$PRIKEYLOC" /system/etc/.ssh/openssh_key; chmod 400 /system/etc/.ssh/openssh_key
  354. }
  355.  
  356.  
  357. HELP () {
  358. echo "Configuration Help"
  359. echo ""
  360. echo "Available options:"
  361. echo " -s configures SSH"
  362. echo " -r configures REVERSE SSH"
  363. echo " -p configures HTTP SOCKS PROXY"
  364. echo " -d configures DDNS"
  365. echo " -k configures SSH KEYS"
  366. echo " -h displays this HELP dialog"
  367. }
  368.  
  369.  
  370. if [[ $@ ]]; then
  371.     while getopts "srpdkh" opt; do
  372.         case $opt in
  373.             s)
  374.                 SSHD_CONFIG
  375.                 ;;
  376.             r)
  377.                 REVERSE_SSH
  378.                 ;;
  379.             p)
  380.                 HTTP_PROXY
  381.                 ;;
  382.             d)
  383.                 DDNS
  384.                 ;;
  385.             k)
  386.                 KEYS
  387.                 ;;
  388.             h)
  389.                 HELP
  390.                 ;;
  391.             \?)
  392.                 HELP
  393.                 ;;
  394.         esac
  395.     done
  396. else
  397.     SSHD_CONFIG;
  398.     REVERSE_SSH;
  399.     HTTP_PROXY;
  400.     DDNS;
  401.     KEYS;
  402. fi
  403.  
  404. echo ""; echo "Restarting dropbear with new settings..."
  405.  
  406. killall dropbear
  407.  
  408. echo ""
  409.  
  410. dropbear
  411.  
  412. echo ""
  413.  
  414. echo ""; echo "Press RETURN to continue"; read return; clear
  415.  
  416. mount -o remount,ro /system
  417.  
  418. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement