User Tools

Site Tools


средства_программирования_shell

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
средства_программирования_shell [2021/05/20 14:57]
admin [Средства программирования shell]
средства_программирования_shell [2024/03/13 08:30] (current)
val [Целочисленный цикл (поиск хостов в подсети)]
Line 4: Line 4:
   * [[http://​mywiki.wooledge.org/​BashFAQ|Часто задаваемые вопросы про bash (eng)]]   * [[http://​mywiki.wooledge.org/​BashFAQ|Часто задаваемые вопросы про bash (eng)]]
   * [[https://​habrahabr.ru/​post/​335960/​|Играючи BASH'​им]]   * [[https://​habrahabr.ru/​post/​335960/​|Играючи BASH'​им]]
 +
   * [[https://​youtu.be/​GxVmukxVUo0|Видео урок]]   * [[https://​youtu.be/​GxVmukxVUo0|Видео урок]]
 +
   * [[https://​www.tutorialspoint.com/​execute_bash_online.php|Execute Bash Shell Online]]   * [[https://​www.tutorialspoint.com/​execute_bash_online.php|Execute Bash Shell Online]]
 +  * [[https://​www.shellcheck.net/​|ShellCheck finds bugs in your shell scripts.]]
  
 ===== Проверка синтаксиса ===== ===== Проверка синтаксиса =====
 +
 +  * [[https://​www.shellcheck.net/​wiki/​]]
 +
 <​code>​ <​code>​
 # apt install shellcheck # apt install shellcheck
  
-$ shellcheck ​script.sh+$ shellcheck ​webd/webd
 </​code>​ </​code>​
 ===== Переменные окружения ===== ===== Переменные окружения =====
Line 73: Line 79:
  
 ==== Целочисленный цикл (поиск хостов в подсети) ==== ==== Целочисленный цикл (поиск хостов в подсети) ====
 +
 +  * [[Утилита nmap#Ping диапазона адресов с verbose и debug]]
 +
 <​code>​ <​code>​
 $ cat test_ping.sh $ cat test_ping.sh
Line 78: Line 87:
 #!/bin/sh #!/bin/sh
  
-test -z $1 && exit 1+#test -z $1 && exit 1 
 +#[ "​$1"​ ] || { echo Example: ./​test_ping.sh 10.5.11; exit 1; }
  
 i=1 i=1
Line 84: Line 94:
 do do
   test $i = 50 && continue   test $i = 50 && continue
-  ping -c 1 -W 1 $1.$i > /dev/null 2>&1 && echo $1.$i+  ping -c 1 -W 1 $1.$i > /dev/null 2>&1 && echo $1.$i || echo No $i
   i=$(($i + 1))   i=$(($i + 1))
 done done
Line 368: Line 378:
 ==== CGI на shell ==== ==== CGI на shell ====
  
-  * [[http://www.team2053.org/docs/bashcgi/gettingstarted.html|Creating CGI Programs with Bash: Getting Started]]+  * [[http://underpop.online.fr/c/cgi/docs/creating-cgi-programs-with-bash/|Creating CGI Programs with Bash: Introduction]] 
 +  * [[https://​debian-administration.org/​article/​371/​A_web_server_in_a_shell_script|A web server in a shell script]] 
 +  * [[https://​stackoverflow.com/​questions/​16640054/​minimal-web-server-using-netcat|Minimal web server using netcat]] 
 +  * [[https://​funprojects.blog/​2021/​04/​11/​a-web-server-in-1-line-of-bash/​|A Web Server in 1 Line of Bash Code]]
  
 ==== Web сервер на shell ==== ==== Web сервер на shell ====
  
-  * [[https://​debian-administration.org/​article/​371/​A_web_server_in_a_shell_script|A web server in a shell script]]+  * [[Сервис HTTP]] 
 +  * [[Переменные окружения#​Чтение значений переменных окружения]]
  
 <​code>​ <​code>​
Line 379: Line 393:
 #!/bin/bash #!/bin/bash
 base=/​var/​www base=/​var/​www
 +#​log=/​var/​log/​webd.log
  
 read request read request
 +##echo "​$request"​ >> $log       # for educational demonstration
 +
 +filename="​${request#​GET }"
 +filename="​${filename% HTTP/​*}"​
 +
 +test $filename = "/"​ && filename="/​index.html"​
 +
 +filename="​$base$filename"​
  
 while : while :
 do do
-  read header+  read -r header 
 +##  echo "​$header"​ >> $log       # for educational demonstration
   [ "​$header"​ == $'​\r'​ ] && break;   [ "​$header"​ == $'​\r'​ ] && break;
 +##  [ "​$header"​ == $''​ ] && break; ​   # for STDIN/​STDOUT educational demonstration
 done done
- 
-url="​${request#​GET }" 
-url="​${url% HTTP/​*}"​ 
- 
-test $url = "/"​ && url="/​index.html"​ 
- 
-filename="​$base$url"​ 
  
 if [ -e "​$filename"​ ] if [ -e "​$filename"​ ]
 then then
 +#  echo `date` OK $filename on `hostname` >> $log
   echo -e "​HTTP/​1.1 200 OK\r"   echo -e "​HTTP/​1.1 200 OK\r"
-  echo -e "​Content-Type: ​`/​usr/​bin/​file -bi \"​$filename\"​`\r"+  echo -e "​Content-Type: ​$(/​usr/​bin/​file -bi \"​$filename\"​)\r"
   echo -e "​\r"​   echo -e "​\r"​
   /bin/cat "​$filename"​   /bin/cat "​$filename"​
 else else
 +#  echo "​$(date)"​ ERR $filename on "​$(hostname)"​ >> "​$log"​
   echo -e "​HTTP/​1.1 404 Not Found\r"​   echo -e "​HTTP/​1.1 404 Not Found\r"​
   echo -e "​Content-Type:​ text/​html;​\r"​   echo -e "​Content-Type:​ text/​html;​\r"​
   echo -e "​\r"​   echo -e "​\r"​
-  echo -e "<​h1>​Not Found</​h1>"​+  echo -e "<​h1>​File $filename ​Not Found</​h1>​
 +#  ip=$(awk '/32 host/ { print f } {f=$2}'​ /​proc/​net/​fib_trie | sort -u | grep -v 127.0.0.1) 
 +#  echo -e "Host: $(hostname),​ IP: $ip, ver 1.1"
 fi fi
- 
 </​code>​ </​code>​
  
Line 432: Line 453:
 </​code>​ </​code>​
  
 +  * [[Сервис INETD]]
 ==== Проверка ==== ==== Проверка ====
  
Line 443: Line 465:
  
 ==== Использование диалоговых окон ==== ==== Использование диалоговых окон ====
 +
 +<​code>​
 +ansible-pull-gpo#​ cat start.sh
 +</​code><​code>​
 +#!/bin/bash
 +
 +on_off() {
 +        grep -q "​$1"​ "​$2"​ && echo ON || echo OFF
 +}
 +
 +if [ -z ${BR+x} ]; then echo -e "​Variable BR (branch) is not set, specify, for example\nexport BR=master";​ exit 1; fi
 +
 +apt update || exit 0
 +
 +apt install dialog ansible git -y
 +
 +GP_OP_FILE=/​usr/​local/​etc/​gpo_options.yml
 +GP_EX_FILE=/​usr/​local/​bin/​ansible-pull-gpo.sh
 +
 +TEMP_FILE=$(mktemp -t '​XXXX'​)
 +
 +touch "​$GP_OP_FILE"​
 +
 +dialog ​ --title "​Configurations,​ Roles and Programs" ​ --clear --nocancel\
 +        --checklist "​Choose Options"​ 19 56 15 \
 +        CONF_RUS_INT:​ "​Russian Interface"​ "​$(on_off CONF_RUS_INT "​$GP_OP_FILE"​)"​ \
 +        PROG_THBIRD:​ "Mail client Thunderbird"​ "​$(on_off PROG_THBIRD "​$GP_OP_FILE"​)"​ \
 +        ROLE_ZAB_AG:​ "​Zabbix Agent" "​$(on_off ROLE_ZAB_AG "​$GP_OP_FILE"​)"​ \
 +        ROLE_OVPN1_CL:​ "​OpenVPN Client"​ "​$(on_off ROLE_OVPN1_CL "​$GP_OP_FILE"​)"​ \
 +2>"​$TEMP_FILE"​
 +
 +< "​$TEMP_FILE"​ tr " " "​\n"​ > "​$GP_OP_FILE"​
 +
 +rm "​$TEMP_FILE"​
 +
 +echo -e "​\nEND:"​ >> "​$GP_OP_FILE"​
 +
 +echo "/​usr/​bin/​ansible-pull -U http://​server.corpX.un/​student/​ansible-pull-gpo.git -C $BR -e @$GP_OP_FILE"​ > "​$GP_EX_FILE"​
 +
 +chmod +x "​$GP_EX_FILE"​
 +
 +##exit 0
 +
 +echo -e "0 */2 * * * sleep \${RANDOM:​0:​2}m;​ $GP_EX_FILE\n@reboot sleep 3m; $GP_EX_FILE"​ | crontab -
 +
 +"​$GP_EX_FILE"​
 +
 +#apt upgrade -y
 +
 +#reboot
 +</​code>​
  
 === Использование программы whiptail (Linux) === === Использование программы whiptail (Linux) ===
средства_программирования_shell.1621511876.txt.gz · Last modified: 2021/05/20 14:57 by admin