This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
средства_программирования_shell [2020/06/15 13:55] val [Web сервер на shell] |
средства_программирования_shell [2023/01/07 18:16] val [Web сервер на shell] |
||
---|---|---|---|
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.]] | ||
+ | |||
+ | ===== Проверка синтаксиса ===== | ||
+ | <code> | ||
+ | # apt install shellcheck | ||
+ | |||
+ | $ shellcheck webd/webd | ||
+ | </code> | ||
===== Переменные окружения ===== | ===== Переменные окружения ===== | ||
Line 81: | Line 91: | ||
done | done | ||
</code><code> | </code><code> | ||
- | $ cat test_arp_ping.sh | + | $ cat arp_ping.sh |
</code><code> | </code><code> | ||
#!/bin/sh | #!/bin/sh | ||
Line 90: | Line 100: | ||
while : | while : | ||
do | do | ||
- | ping -c 1 -W 1 $1.$i > /dev/null 2>&1 | + | ip=$1.$i |
- | arp -an | grep "($1.$i)" | grep -q ':..:..:' && echo + $1.$i || echo - $1.$i | + | |
+ | ping -c 1 -W 1 $ip >/dev/null | ||
+ | ip n | grep "$ip " | grep -q ':..:..:' && echo $ip | ||
i=$(($i + 1)) | i=$(($i + 1)) | ||
test $i -eq 254 && break | test $i -eq 254 && break | ||
Line 358: | Line 371: | ||
==== 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 369: | Line 386: | ||
#!/bin/bash | #!/bin/bash | ||
base=/var/www | base=/var/www | ||
+ | #log=/var/log/webd.log | ||
read request | read request | ||
+ | #echo $request >> $log | ||
+ | |||
+ | 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 | ||
[ "$header" == $'\r' ] && break; | [ "$header" == $'\r' ] && break; | ||
done | done | ||
- | url="${request#GET }" | + | if [ -e "$filename" ] |
- | url="${url% HTTP/*}" | + | then |
- | filename="$base$url" | + | # echo `date` OK $filename >> $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" |
- | + | else | |
- | /bin/cat "$filename" | + | # echo "$(date)" ERR $filename >> $log |
+ | echo -e "HTTP/1.1 404 Not Found\r" | ||
+ | echo -e "Content-Type: text/html;\r" | ||
+ | echo -e "\r" | ||
+ | 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 | ||
</code> | </code> | ||
- | === Debian/Ubuntu === | ||
<code> | <code> | ||
# chmod +x /usr/local/sbin/webd | # chmod +x /usr/local/sbin/webd | ||
Line 408: | Line 441: | ||
</code> | </code> | ||
- | === Debian/Ubuntu === | ||
<code> | <code> | ||
# wget -O /var/www/img/logo.gif http://val.bmstu.ru/unix/Media/logo.gif | # wget -O /var/www/img/logo.gif http://val.bmstu.ru/unix/Media/logo.gif | ||
</code> | </code> | ||
- | === FreeBSD === | + | * [[Сервис INETD]] |
+ | ==== Проверка ==== | ||
<code> | <code> | ||
- | # fetch -o /var/www/img/logo.gif http://val.bmstu.ru/unix/Media/logo.gif | + | http://server.corpX.un/ |
</code> | </code> | ||
- | |||
==== Asterisk AGI ==== | ==== Asterisk AGI ==== | ||
- | * [[Сервис Asterisk#Тарификация звонков]] | + | * [[Сервис Asterisk#Asterisk AGI]] |