This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
средства_программирования_shell [2023/01/22 19:07] val [Использование диалоговых окон] |
средства_программирования_shell [2025/03/10 10:06] (current) val [Передача сообщений в syslog] |
||
---|---|---|---|
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://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators|What are the shell's control and redirection operators?]] | ||
* [[https://youtu.be/GxVmukxVUo0|Видео урок]] | * [[https://youtu.be/GxVmukxVUo0|Видео урок]] | ||
Line 79: | Line 80: | ||
==== Целочисленный цикл (поиск хостов в подсети) ==== | ==== Целочисленный цикл (поиск хостов в подсети) ==== | ||
+ | |||
+ | * [[Утилита nmap#Ping диапазона адресов с verbose и debug]] | ||
+ | |||
<code> | <code> | ||
$ cat test_ping.sh | $ cat test_ping.sh | ||
Line 84: | Line 88: | ||
#!/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 90: | Line 95: | ||
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 187: | Line 192: | ||
<code> | <code> | ||
- | $ cat log_gen.sh | + | bash -c 'cat <<END >log_gen.sh |
- | </code><code> | + | |
while : | while : | ||
do | do | ||
Line 194: | Line 198: | ||
logger -t cisco -p local0.info "Message 2" | logger -t cisco -p local0.info "Message 2" | ||
done | done | ||
+ | END' | ||
</code><code> | </code><code> | ||
- | $ sh cisco_log_gen.sh | + | $ sh log_gen.sh |
</code> | </code> | ||
===== Примеры использования скриптов sh в системах загрузки ===== | ===== Примеры использования скриптов sh в системах загрузки ===== | ||
Line 392: | Line 397: | ||
read request | read request | ||
- | ##echo "$request" >> $log # not work with keepalived | + | ##echo "$request" >> $log # for educational demonstration |
filename="${request#GET }" | filename="${request#GET }" | ||
Line 404: | Line 409: | ||
do | do | ||
read -r header | read -r header | ||
- | ## echo "$header" >> $log # not work with keepalived | + | ## echo "$header" >> $log # for educational demonstration |
[ "$header" == $'\r' ] && break; | [ "$header" == $'\r' ] && break; | ||
+ | ## [ "$header" == $'' ] && break; # for STDIN/STDOUT educational demonstration | ||
done | done | ||
Line 412: | Line 418: | ||
# echo `date` OK $filename on `hostname` >> $log | # 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 "$(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" | ||
Line 432: | Line 438: | ||
==== Ресурсы Web сервера на shell ==== | ==== Ресурсы Web сервера на shell ==== | ||
<code> | <code> | ||
- | # mkdir /var/www | + | # mkdir -p /var/www |
# cat /var/www/index.html | # cat /var/www/index.html | ||
Line 441: | Line 447: | ||
</html> | </html> | ||
</code><code> | </code><code> | ||
- | # mkdir /var/www/img | + | # mkdir -p /var/www/img |
</code> | </code> | ||
Line 458: | Line 464: | ||
* [[Сервис Asterisk#Asterisk AGI]] | * [[Сервис Asterisk#Asterisk AGI]] | ||
+ | |||
+ | ==== Отправка email с вложениями ==== | ||
+ | |||
+ | * [[https://serverfault.com/questions/38919/send-an-email-with-an-attached-file-using-telnet-or-netcat|Send an email with an attached file using telnet or netcat]] | ||
+ | |||
+ | <code> | ||
+ | $ cat mail.sh | ||
+ | </code><code> | ||
+ | #!/bin/sh | ||
+ | |||
+ | [ -f "$1" ] || { echo Example: ./mail.sh attach_file_name; exit 1; } | ||
+ | |||
+ | EMAIL="val@bmstu.ru" | ||
+ | MAILSERVER="mailhub.bmstu.ru" | ||
+ | FILENAME=$1 | ||
+ | |||
+ | { | ||
+ | sleep 2; | ||
+ | echo "EHLO $MAILSERVER"; sleep 1; | ||
+ | echo "MAIL FROM: <$EMAIL>"; sleep 1; | ||
+ | echo "RCPT TO: <$EMAIL>"; sleep 1; | ||
+ | echo "DATA"; sleep 1; | ||
+ | echo "To: $EMAIL" | ||
+ | echo "Subject: $FILENAME" | ||
+ | echo "MIME-Version: 1.0 (mime-construct 1.9)" | ||
+ | echo "Content-Type: $(/usr/bin/file -bi $FILENAME); name=\"$FILENAME\"" | ||
+ | echo "Content-Transfer-Encoding: base64"; | ||
+ | echo | ||
+ | cat $FILENAME | openssl base64; | ||
+ | echo '.'; | ||
+ | echo "QUIT"; | ||
+ | echo | ||
+ | } | nc $MAILSERVER 25 | ||
+ | </code> | ||
==== Использование диалоговых окон ==== | ==== Использование диалоговых окон ==== | ||
Line 488: | Line 528: | ||
PROG_THBIRD: "Mail client Thunderbird" "$(on_off PROG_THBIRD "$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_ZAB_AG: "Zabbix Agent" "$(on_off ROLE_ZAB_AG "$GP_OP_FILE")" \ | ||
- | ROLE_OVPN_CL: "OpenVPN Client" "$(on_off ROLE_OVPN_CL "$GP_OP_FILE")" \ | + | ROLE_OVPN1_CL: "OpenVPN Client" "$(on_off ROLE_OVPN1_CL "$GP_OP_FILE")" \ |
2>"$TEMP_FILE" | 2>"$TEMP_FILE" | ||