четверг, 29 декабря 2011 г.

nmap sh

#!/bin/sh
echo "\tВведи IP адрес, который желаешь проверить на открытые порты:"
read IP
INFO=$(nmap -A -e eth0 -D 83.137.145.102 143.121.84.12 132.154.156.6 -vv -O -PN -sS -sV $IP)
for i in $IP
do
echo "\n\tIP:\n\t $IP \n\tINFO:\n\t $INFO \n"
echo "$INFO\t$IP\n" > /home/tmp/log/nmap/nmap-$IP.txt
done

понедельник, 26 декабря 2011 г.

dns>ip >> ip.txt

После 3-х часов курения ман'ов вывел такой скрипт для удобного получения адресов ип при вводе url:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



#!/bin/sh
echo "URL:"
read URL
IP=$(nslookup $URL | awk '/Address:/ { print $2 }' | grep -v \#)
for i in $URL
do
echo "\n\t$IP:\n\t$URL\n\tDNS прочитан, ip записан в файл ip-ban.txt\t[OK]\n"
echo "# $URL\n$IP" > /home/tmp/ban-ip/ip-ban.txt
done




<><><><><><><><><><><><><><><><><><><><><><><><><>

В консоли видим следующее:

:~$ /home/tmp/sh/nslookup.sh
URL:
google.com

74.125.232.50
74.125.232.51
74.125.232.52
74.125.232.48
74.125.232.49:
google.com
DNS прочитан, ip записан в файл ip-ban.txt [OK]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
В файле:

# google.ru
74.125.232.50
74.125.232.51
74.125.232.52
74.125.232.48
74.125.232.49
# google.com
74.125.232.50
74.125.232.51
74.125.232.52
74.125.232.48
74.125.232.49

воскресенье, 25 декабря 2011 г.

практичный способ банить ип

ip адреа принадлежащие доменному имени сложим результат в файл:

nslookup brightcity.ru | awk '/Address:/ { print $2 }' | grep -v \# > /etc/ip-ban.txt

Создадим скрипт и разрешим выполнение файла:

/etc/iptables.sh
*********************************************

#!/bin/bash
BLOCKDB="/etc/ip-ban.txt"
IPS=$(grep -Ev '^#' $BLOCKDB)
for i in $IPS
do
iptables -A INPUT -s $i -j DROP
iptables -A OUTPUT -o $i -j DROP
done

*********************************************
 Собственно сам список ip:

/etc/ip-ban.txt
#############################################

# brightcity.ru
90.156.201.15
90.156.201.25
90.156.201.44
90.156.201.14
#############################################

пятница, 23 декабря 2011 г.

Лог http:// на email за сутки

Нам понадобятся следующие команды:
# tcpflow -c -i eth0 host 111.111.111.111 and port 80 | grep "Referer" >> /home/tmp/log/tcp/tcpflow/log.txt
$ sort /home/tmp/log/tcp/tcpflow/log.txt | uniq > /home/tmp/log/tcp/tcpflow/loga.txt
mail -s "Письмо из файла" darvin44in@yandex.ru < /home/tmp/log/tcp/tcpflow/loga.txt


А теперь пояснения и сунем все это в cron:


*****************************************************
59 23 22 12 * tcpflow -c -i eth0 host 111.111.111.111 and port 80 | grep "Referer" >> /home/tmp/log/tcp/tcpflow/log.txt
59 23 * * * sort /home/tmp/log/tcp/tcpflow/log.txt | uniq > /home/tmp/log/tcp/tcpflow/loga.txt && mail -s "Log http:// в течении суток" user@mail.ru < /home/tmp/log/tcp/tcpflow/loga.txt && rm /home/tmp/log/tcp/tcpflow/log.txt


*****************************************************

скачиваем видео с youtube.com

http://userscripts.org/scripts/show/62634

четверг, 22 декабря 2011 г.

ban ip (продолжение)

Итак, есть список адресов серверов, но ведь мы можем и забыть, какие именно сайты заблокировали. Исправим это:

Соберем информацию об адресах:

nslookup < ipa.txt | cat > ipb.txt

Отфильтруем, чтобы присутствовали только строки с ip:

egrep -iRnH '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' ipb.txt > ipc.txt
  Файл стал заметно меньше.

Для лучшего понимания того, что нам мешает и не нужно сортируем:


sort ipc.txt > ipf.txt


Удалим лишнее:

sed -i '/Server/d' ipf.txt
sed -i '/#53/d' ipf.txt
sed -i '/datacenter.com/d' ipf.txt
sed -i '/server/d' ipf.txt


получаем:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ipb.txt:107:120.227.172.184.in-addr.arpa name = 184.172.227.120-static.reverse.softlayer.com.
ipb.txt:123:ptr1.intergenia.de internet address = 217.172.191.251
ipb.txt:124:ptr2.intergenia.de internet address = 62.75.134.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Мой список ban ip

Вынем из уже созданных правил ip:

egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' iptables.rules > ip.txt

Теперь отсортируем и удалим повторяющиеся строки:

sort ip.txt | uniq >ipa.txt

%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%;%

113.57.157.10
121.10.143.226
122.225.11.177
173.244.196.64
173.244.196.66
173.244.196.67
173.244.196.68
173.244.196.69
173.244.196.70
174.37.109.121
178.218.210.194
178.248.234.2
184.172.227.120
188.138.113.162
188.138.90.154
188.138.90.193
188.138.92.242
188.138.95.145
188.187.250.19
188.64.170.50
188.64.170.9
188.64.171.2
188.64.171.7
188.93.18.82
188.93.18.84
208.101.49.98
213.174.154.100
213.174.154.217
213.186.117.133
213.248.36.100
217.69.135.1
222.177.23.112
222.75.152.67
32.64.5.74
46.182.28.101
46.182.28.102
46.182.28.98
46.182.28.99
46.38.184.68
50.30.33.90
64.76.235.3
69.162.117.202
74.125.232.48
74.125.232.49
74.125.232.50
74.125.232.51
74.125.232.52
74.125.79.100
74.125.79.101
74.125.79.102
74.125.79.113
74.125.79.138
74.125.79.139
81.192.11.100
81.19.70.19
81.19.88.103
81.19.88.108
81.19.88.80
81.19.88.81
83.149.2.253
85.195.93.66
85.25.137.18
85.85.181.180
87.242.77.54
87.242.88.80
87.242.88.94
88.208.32.151
88.208.32.20
88.208.32.47
88.208.57.102
88.208.57.32
88.208.58.166
88.208.58.194
88.212.196.101
88.212.196.102
88.212.196.103
88.212.196.104
88.212.196.105
88.212.196.66
88.212.196.69
88.212.196.77
88.85.93.100
88.85.93.101
88.85.93.34
88.85.93.35
90.156.219.61
90.156.219.62
91.199.92.39
91.202.63.162
91.205.189.15
91.205.189.27
95.211.127.92
95.211.33.37

продолжение темы

понедельник, 19 декабря 2011 г.

воскресенье, 18 декабря 2011 г.

log to email logwatch

 gedit /etc/logwatch/conf/logwatch.conf
************************
Detail = High
mailer = "/usr/sbin/sendmail -t"
TmpDir = /tmp
MailTo = user@yandex.ru
MailFrom = user-pc
Arcives = Yes
Renage = Today
Print = No
Service = All
******************
############
 /etc/cron.daily/00logwatch
############

aptitude install fortunes-ru

crontab -e
****
*/5 * * * * /usr/games/fortune | sendxmpp -s umor -r umor -i user@jabber.ru
****

пятница, 16 декабря 2011 г.

Мониторим сервер с помощью Jabber. Мои наработки.


gedit /etc/rsyslog.d/50-default.conf
***

:msg, contains, "Accepted password for" /var/log/ssh/Accepted_auth.log
:msg, contains, "Failed password for" /var/log/ssh/Failed_auth.log

***
/etc/init.d/rsyslog restart

//

gedit /home/$user/.sendxmpprc
***
user1@jabber.ru:5222 PaSsWoRd
***

tail -f /var/log/ssh/Failed_auth.log | sendxmpp -s 'SSH Login' -r 'F_SSH' -i user@jabber.ru
tail -f /var/log/ssh/Accepted_auth.log | sendxmpp -s 'SSH Login' -r 'SSH' -i user@jabber.ru

воскресенье, 4 декабря 2011 г.

Вывод в консоль и запись в файл

/home/tmp/echo.sh && /home/tmp/echo.sh > /home/tmp/echo.log


#!/bin/bash
#/home/tmp/echo.sh
# используйте обратные кавычки " ` ` " для выполнения команды оболочки (Клавиша "тильда" - "~")
echo `nmap -sV -PN 85.195.93.66`

суббота, 26 ноября 2011 г.

crontab - будильник

crontab -e


35 05 * * 1-5 mpg321 --gain 90 http://alf.rga.ru:8008
40 05 * * 1-5 kill -s 9 $(ps -e|grep mpg321 |awk '{print $1}')
#через 5 минут процесс будет убит))

00 20 1 * * DISPLAY=:0 gdialog --msgbox "Оплати интернет!" 25 20 > /dev/null
#Сообщение на экране появиться в 00 минут, 20 часов 1-го числа каждого месяца.

воскресенье, 20 ноября 2011 г.

Восстановление данных

sudo apt-get install scalpel
Для работы в файле нужно убрать комментарии, где указаны расширения.
gedit /etc/scalpel/scalpel.conf

scalpel /dev/sda1 -o /home/output 


Дополнительные утилиты:
ddrescuedd_rescuetestdisk, Photorec

четверг, 27 октября 2011 г.

key ssh

ssh-keygen -t rsa
cat /home/user1/.ssh/id_rsa.pub > /home/user1/.ssh/authorized_keys
cd ~/.ssh
ssh-copy-id -i ~/.ssh/id_rsa user2@192.168.1.111

scp ~/.ssh/id_rsa user2@192.168.1.1:/home/user2/.ssh/

порт должен быть 22!!!
после манипуляций, можно поменять.

ВНИМАНИЕ!!! Без этих манипуляций работать не будет!!!
(Authentication refused: bad ownership or modes for directory)

Получили удаленный шелл - все настроено правильно, нет - проверяем сделанные шаги или смотрим ниже.

SSH параноидально относится к безопасности - по понятным причинам. В частности, права на директории и файлы должны быть следующие:
/home/user1 - 755
/home/user1/~.ssh -700
/home/user1/authorized_keys - 600

ssh-add ~/.ssh/id_rsa

понедельник, 8 августа 2011 г.

Нетбук (ноутбук) в роли IP камеры

Для этого мне сгодились:
 Устройства - Toshiba ac100-117, Dell Inspition 1525.
 Программы - Zoneminder & Motion.

 Итак, на Тошибу я поставил мотион и он запускается вместе с убунтой, подключается к ви-фи сети и готов транслировать видео в сеть))
 На Делл запустил зонемидер, настроил как получение картинки как от вебки и... Вуаля - все что происходит перед нетбуком пишется на винт моего ноутбука))

P.S. Стоит добавить, что на ac100 изначально стоял Андроид, но он меня не устроил совершенно. Да и как видите, классическая ОС ЗНАЧИТЕЛЬНО выигрывает, перед урезанной мобильной платформой, хоть и на нормальном ядре сделано...

суббота, 30 июля 2011 г.

Фейк Вконтакте.ру

Пришло сообщение в вконтакте, с прикрепленным файлом *.rtf. его содержимым было приглашение пользователя узнать, кто же посещает его страницу. Зайдя по ссылке http://vklok.in увидел аналогичный дизайн что и у оригинального сайта, однако введя заведомо неверный адрес электронной почты и вымышленный пароль. Бинго - вошел)) Естественно показали красивый дизайн, но в конце страницы понимаешь, что это очередной способ обмана доверчивых пользователей социалок - предложение отправить смс на платный номер...

Дорогие друзья, будьте бдительны!!! Мошенники придумывают новые, как им кажется, способы облапошить интернет публику. Однако будучи внимательным к деталям, не сложно разглядеть истинные цели тех или иных ресурсов сети))

пятница, 22 июля 2011 г.

convert jpg

$ touch /home/"user_name"/script/convert_jpg.sh
# chmod +x /home/"user_name"/script/convert_jpg.sh
# nano /home/"user_name"/script/convert_jpg.sh


(текстовый редактор может отличаться от указанного "nano")


for i in *.jpg; do convert -resize 800 -quality 100 $i resized_$i; done


Поместить скрипт в папку, в которой планируем находятся исходные файлы, запустить.

понедельник, 20 июня 2011 г.

продам на запчасти ноутбук. спб

продам на запчасти ноутбук dell inspirion 1525. в отличном состоянии: мать, проц, память (2 планки 512 ddr2), монитор 15.4". корпус в хлам, хард оставлю себе т.к. у него много битых секторов, не работает клава (шлейф убит). пишите.

пятница, 27 мая 2011 г.

ac100 ubuntu

Установил убунту на это "чудо")) Оригинальную инструкцию читаем: http://salaliitto.com/~gildean/ac100/wiki/phh/
Там берем нужные пакеты.
Должен стоять андроид. (версия не важна). создаем загрузочную sd карточку. Загружаемся с нее, фарматируем, монтируем раздел размером около 5.5 гигабайт. Распаковываем архив, на тот раздел. Запускаем устройство. Происходит установка ОС)))

воскресенье, 22 мая 2011 г.

15 апреля 2011 купил Toshiba ac100 android 2.1

Долго мучился с установкой убунту. Не получилось у меня. Прошил андроид 2.2. Были проблемы с видео - звук отсутсвовал - QQPlayer решил проблему. Все что нужно программно есть. И даже больше - голосовой поиск гугл меня порадовал! А теперь о грустном - в 117 модели нет модема, а воткнув усб модем, система лишь матом на меня ругнулась(( Так же по синему зубу\усб с кпк расшарил инет, но на смартбуке нуль эмоций...

воскресенье, 8 мая 2011 г.

samba share print$

Расшариваем в smb.conf:
[print$]
        comment = Printer Drivers
        path = /var/samba-drivers
        guest ok = no
        read only = yes
        write list = @"MYDOMAIN\Domain Admins"
Так же вносим глобальные настройки:
load printers = yes
printing = cups
Делаем Самбе smbcontrol smbd reload-config . Благодаря load printers все принтера из CUPS должны расшариться автоматически под теми же именами. Специально прописывать их в smb.conf не требуется. Хотя при необходимости можно сказать load printers = no и каждый расшариваемый принтер прописать явно, как ресурс printable = yes. А нерасшариваемый — проигнорировать.
Далее нужно установить на сервер драйвера. Начинается всё с заполнения /usr/share/cusp/drivers нужными файлами драйверов. Опишу два способа. Сначала тот, которым действовал при установке на певом сервере, второй — более простой и удобный для последующих установок метод передирания.
Итак, при первоначальном варианте ставится пакет cups-windows, который можно взять на сайте CUPS. После этого у нас уже лежат в /usr/share/cups/drivers некоторые файлы, необходимые для работы. Но не все. Это только то, что самбовцы могут поставлять сами, по лицензионным соображениям. Остальное нужно добавить из стандартного комплекта Windows. К сожалению, Винда обычно не понимает, что они у неё есть и пытается перескачать с сервера, когда ставится принтер. Итак, находим любую машину с Windows XP (или какая у вас там основная платформа) и у неё в system32 находим следующие файлы: ps5ui.dll, pscript.hlp, pscript.ntf, pscript5.dll. Кладём их в тот же каталог. (Я поступил так: скопировал по сети в print$, а затем внутри сервера перенёс на нужное место.) взято с сайта http://dep346.narod.ru/articles/samba-cups-print/
Мой конфиг самбы: 
########## Printing ##########

# If you want to automatically load your printer list rather
# than setting them up individually then you'll need this
   load printers = yes

# lpr(ng) printing. You may wish to override the location of the
# printcap file
#   printing = bsd
#   printcap name = /etc/printcap
# than setting them up individually then you'll need this
   load printers = yes

# lpr(ng) printing. You may wish to override the location of the
# printcap file
#   printing = bsd
#   printcap name = /etc/printcap

# CUPS printing.  See also the cupsaddsmb(8) manpage in the
# cupsys-client package.
   printing = cups
   printcap name = cups

#############
[printers]
   comment = All Printers
   browseable = yes
   path = /var/spool/samba
   printable = yes
   guest ok = yes
   read only = yes
   create mask = 0700

# Windows clients look for this share name as a source of downloadable
# printer drivers
[print$]
   comment = Printer Drivers
   path = /var/lib/samba/printers
   browseable = yes
   read only = yes
   guest ok = yes
# Uncomment to allow remote administration of Windows print drivers.
# You may need to replace 'lpadmin' with the name of the group your
# admin users are members of.
# Please note that you also need to set appropriate Unix permissions
# to the drivers directory for these users to have write rights in it
   write list = root, @lpadmin



cups - server printer

# Sample configuration file for the CUPS scheduler.  See "man cupsd.conf" for a
# complete description of this file.
#

# Log general information in error_log - change "warn" to "debug"
# for troubleshooting...
LogLevel warn

# Deactivate CUPS' internal logrotating, as we provide a better one, especially
# LogLevel debug2 gets usable now
MaxLogSize 0

# Administrator user group...
SystemGroup lpadmin


# Only listen for connections from the local machine.
Listen *:631
Listen /var/run/cups/cups.sock

# Show shared printers on the local network.
Browsing On
BrowseOrder allow,deny
BrowseAllow all
BrowseLocalProtocols CUPS dnssd
BrowseAddress @LOCAL

# Default authentication type, when authentication is required...
DefaultAuthType Basic

# Restrict access to the server...

  Order allow,deny
  Allow all
  Allow localhost
  Allow @LOCAL


# Restrict access to the admin pages...

  Order allow,deny
  Allow 192.168.1.1
  Allow 192.168.1.2
  Allow localhoct


# Restrict access to configuration files...

#  AuthType Default
#  Require user @SYSTEM
#  Order allow,deny
  Allow 192.168.1.1
  Allow 192.168.1.2
  Allow localhost


# Set the default printer/job policies...

  # Job-related operations must be done by the owner or an administrator...
  
    Require user @OWNER @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  # All administration operations require an administrator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  # All printer operations require a printer operator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  # Only the owner or an administrator can cancel or authenticate a job...
  
    Require user @OWNER @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  
    Order allow,deny
    Allow 192.168.1.1
 


# Set the authenticated printer/job policies...

  # Job-related operations must be done by the owner or an administrator...
  
    AuthType Default
    Order allow,deny
    Allow 192.168.1.1
 

  
    AuthType Default
    Require user @OWNER @SYSTEM
    Order deny,allow
    Allow 192.168.1.1
 

  # All administration operations require an administrator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  # All printer operations require a printer operator to authenticate...
  
    AuthType Default
    Require user @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  # Only the owner or an administrator can cancel or authenticate a job...
  
    AuthType Default
    Require user @OWNER @SYSTEM
    Order allow,deny
    Allow 192.168.1.1
 

  
    Order allow,deny
    Allow 192.168.1.1
 

среда, 27 апреля 2011 г.

genius eye 312 и zoneminder

добился своей цели, запустил таки свою тупейшую, древнюю вебку в режиме видеорегистратора!

apt-get install v4l2ucp


Installation from a .deb

ZoneMinder is now in the Debian repositories. There are downloadable .deb files for Ubuntu for Edgy and Feisty. At this point it is in the repository for Gutsy. After you install, you will need to do some additional work, which is documented in the readme file included in the package. These steps have worked for several people as of the end of Summer 07.
Link Apache
sudo ln -s /etc/zm/apache.conf /etc/apache2/conf.d/zoneminder.conf
Restart Apache
sudo apache2ctl restart
suid zmfix
sudo chmod 4755 /usr/bin/zmfix
Run zmfix
zmfix -a
Fix export problem
sudo chown www-data.www-data /usr/share/zoneminder/temp
edit /etc/sysctl.conf and add the following lines (for 128meg shared mem)
kernel.shmall = 134217728
kernel.shmmax = 134217728
Download cambozola.jar and put it in /usr/share/zoneminder Enable in options->Images.
Set path to ffmpeg in options -> Images /usr/bin/ffmpeg
test with xawtv (-nodga may be needed)


nano /usr/bin/zzmdc.pl Строка 64 нас интересует:

#!/usr/bin/perl -wT
#
# ==========================================================================
#
# ZoneMinder Daemon Control Script, $Date: 2009-06-08 10:11:56 +0100 (Mon, 08 Jun 2009) $, $Revision: 2908 $
# Copyright (C) 2001-2008 Philip Coombes
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ==========================================================================
#
# This script is the gateway for controlling the various ZoneMinder
# daemons. All starting, stopping and restarting goes through here.
# On the first invocation it starts up a server which subsequently
# records what's running and what's not. Other invocations just 
# connect to the server and pass instructions to it.
#
use strict;
use bytes;


# ==========================================================================
#
# User config
#
# ==========================================================================


use constant DBG_ID => "zmdc"; # Tag that appears in debug to identify source
use constant DBG_LEVEL => 0; # 0 is errors, warnings and info only, > 0 for debug


use constant MAX_CONNECT_DELAY => 10;


# ==========================================================================
#
# Don't change anything from here on down
#
# ==========================================================================


# Include from system perl paths only
use ZoneMinder;
use POSIX;
use Socket;
use IO::Handle;
use Data::Dumper;


use constant SOCK_FILE => ZM_PATH_SOCKS.'/zmdc.sock';


$| = 1;


$ENV{PATH}  = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{LD_PRELOAD} = '/usr/lib/libv4l/v4l2convert.so';


my @daemons = (
'zmc',
'zma',
'zmf',
'zmfilter.pl',
'zmaudit.pl',
'zmtrigger.pl',
'zmx10.pl',
'zmwatch.pl',
'zmupdate.pl',
'zmtrack.pl'
);


sub Usage
{   
    print( "
Usage: zmdc.pl [daemon [options]]
Parameters are :-
          - One of 'startup|shutdown|status|check|logrot' or
                      'start|stop|restart|reload'.
[daemon [options]]  - Daemon name and options, required for second group of commands
");     
    exit( -1 );
}


my $command = shift @ARGV;
if( !$command )
{
    print( STDERR "No command given\n" );
    Usage();
}
my $needs_daemon = $command !~ /(?:startup|shutdown|status|check|logrot)/;
my $daemon = shift( @ARGV );
if( $needs_daemon && !$daemon )
{
    print( STDERR "No daemon given\n" );
    Usage();
}
my @args;


my $daemon_patt = '('.join( '|', @daemons ).')';
if ( $needs_daemon )
{
if ( $daemon =~ /^${daemon_patt}$/ )
{
$daemon = $1;
}
else
{
print( STDERR "Invalid daemon '$daemon' specified" );
        Usage();
}
}


foreach my $arg ( @ARGV )
{
# Detaint arguments, if they look ok
#if ( $arg =~ /^(-{0,2}[\w]+)/ )
if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ )
{
push( @args, $1 );
}
else
{
print( STDERR "Bogus argument '$arg' found" );
        exit( -1 );
}
}


socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );


my $saddr = sockaddr_un( SOCK_FILE );
my $server_up = connect( CLIENT, $saddr );
if ( !$server_up )
{
if ( $command eq "logrot" )
    {
        exit();
    }
if ( $command eq "check" )
{
print( "stopped\n" );
exit();
}
elsif ( $command ne "startup" )
{
print( "Unable to connect to server\n" );
exit( -1 );
}
# The server isn't there 
print( "Starting server\n" );
close( CLIENT );


if ( my $cpid = fork() )
{
zmDbgInit( DBG_ID, level=>DBG_LEVEL );


# Parent process just sleep and fall through
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
my $attempts = 0;
while (!connect( CLIENT, $saddr ))
{
$attempts++;
Fatal( "Can't connect: $!" ) if ($attempts > MAX_CONNECT_DELAY);
sleep(1);
}
}
elsif ( defined($cpid) )
{
        my $fd = 0;
        while( $fd < POSIX::sysconf( &POSIX::_SC_OPEN_MAX ) )
        {
            POSIX::close( $fd++ );
        }


setpgrp();


zmDbgInit( DBG_ID, level=>DBG_LEVEL );


dPrint( DBG_INFO, "Server starting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );


if ( open( PID, ">".ZM_PID ) )
{
print( PID $$ );
close( PID );
}


killAll( 1 );


socket( SERVER, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
unlink( SOCK_FILE );
bind( SERVER, $saddr ) or Fatal( "Can't bind: $!" );
listen( SERVER, SOMAXCONN ) or Fatal( "Can't listen: $!" );


$SIG{CHLD} = \&reaper;
$SIG{INT} = \&shutdownAll;
$SIG{TERM} = \&shutdownAll;
$SIG{ABRT} = \&shutdownAll;
$SIG{HUP} = \&logrot;


my %cmd_hash;
my %pid_hash;


my $rin = '';
vec( $rin, fileno(SERVER), 1 ) = 1;
my $win = $rin;
my $ein = $win;
my $timeout = 0.1;
while( 1 )
{
my $nfound = select( my $rout = $rin, undef, undef, $timeout );
if ( $nfound > 0 )
{
if ( vec( $rout, fileno(SERVER), 1 ) )
{
my $paddr = accept( CLIENT, SERVER );
my $message = ;


next if ( !$message );


my ( $command, $daemon, @args ) = split( ';', $message );


if ( $command eq 'start' )
{
start( $daemon, @args );
}
elsif ( $command eq 'stop' )
{
stop( $daemon, @args );
}
elsif ( $command eq 'restart' )
{
restart( $daemon, @args );
}
elsif ( $command eq 'reload' )
{
reload( $daemon, @args );
}
elsif ( $command eq 'startup' )
{
# Do nothing, this is all we're here for
dPrint( DBG_WARNING, "Already running, ignoring command '$command'\n" );
}
elsif ( $command eq 'shutdown' )
{
shutdownAll();
}
elsif ( $command eq 'check' )
{
check( $daemon, @args );
}
elsif ( $command eq 'status' )
{
if ( $daemon )
{
status( $daemon, @args );
}
else
{
status();
}
}
elsif ( $command eq 'logrot' )
{
logrot();
}
else
{
dPrint( DBG_ERROR, "Invalid command '$command'\n" );
}
close( CLIENT );
}
else
{
Fatal( "Bogus descriptor" );
}
}
elsif ( $nfound < 0 )
{
if ( $! == EINTR )
{
# Dead child, will be reaped
#print( "Probable dead child\n" );
# See if it needs to start up again
restartPending();
}
elsif ( $! == EPIPE )
{
Error( "Can't select: $!" );
}
else
{
Fatal( "Can't select: $!" );
}
}
else
{
#print( "Select timed out\n" );
restartPending();
}
}
dPrint( DBG_INFO, "Server exiting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
unlink( SOCK_FILE );
unlink( ZM_PID );
exit();


sub cPrint
{
if ( fileno(CLIENT) )
{
print CLIENT @_
}
}
sub dPrint
{
my $dbg_level = shift;
if ( fileno(CLIENT) )
{
print CLIENT @_
}
if ( $dbg_level == DBG_DEBUG )
{
Debug( @_ );
}
elsif ( $dbg_level == DBG_INFO )
{
Info( @_ );
}
elsif ( $dbg_level == DBG_WARNING )
{
Warning( @_ );
}
elsif ( $dbg_level == DBG_ERROR )
{
Error( @_ );
}
elsif ( $dbg_level == DBG_FATAL )
{
Fatal( @_ );
}
}
sub start
{
my $daemon = shift;
my @args = @_;


my $command = $daemon;
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
my $process = $cmd_hash{$command};


if ( !$process )
{
# It's not running, or at least it's not been started by us
$process = { daemon=>$daemon, args=>\@args, command=>$command, keepalive=>!undef };
}
elsif ( $process->{pid} && $pid_hash{$process->{pid}} )
{
dPrint( DBG_INFO, "'$process->{command}' already running at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}\n" );
return();
}


if ( my $cpid = fork() )
{
my $sigset = POSIX::SigSet->new;
my $blockset = POSIX::SigSet->new( SIGCHLD );
sigprocmask( SIG_BLOCK, $blockset, $sigset ) or Fatal( "Can't block SIGCHLD: $!" );
$process->{pid} = $cpid;
$process->{started} = time();
delete( $process->{pending} );


dPrint( DBG_INFO, "'$command' starting at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}\n" );


$cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process;
sigprocmask( SIG_SETMASK, $sigset ) or Fatal( "Can't restore SIGCHLD: $!" );
}
elsif ( defined($cpid ) )
{
                my $fd = 0;
                while( $fd < POSIX::sysconf( &POSIX::_SC_OPEN_MAX ) )
                {
                    POSIX::close( $fd++ );
                }


# Child process
$SIG{CHLD} = 'DEFAULT';
$SIG{INT} = 'DEFAULT';
$SIG{TERM} = 'DEFAULT';
$SIG{ABRT} = 'DEFAULT';
dPrint( DBG_INFO, "'".join( ' ', ( $daemon, @args ) )."' started at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );

if ( $daemon =~ /^${daemon_patt}$/ )
{
$daemon = ZM_PATH_BIN.'/'.$1;
}
else
{
Fatal( "Invalid daemon '$daemon' specified" );
}


my @good_args;
foreach my $arg ( @args )
{
# Detaint arguments, if they look ok
if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ )
{
push( @good_args, $1 );
}
else
{
Fatal( "Bogus argument '$arg' found" );
}
}


exec( $daemon, @good_args ) or Fatal( "Can't exec: $!" );
}
else
{
Fatal( "Can't fork: $!" );
}
}
sub _stop
{
my $final = shift;
my $daemon = shift;
my @args = @_;


my $command = $daemon;
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
my $process = $cmd_hash{$command};
if ( !$process )
{
dPrint( DBG_WARNING, "Can't find process with command of '$command'\n" );
return();
}
elsif ( $process->{pending} )
{
delete( $cmd_hash{$command} );
dPrint( DBG_INFO, "Command '$command' removed from pending list at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
return();
}


my $cpid = $process->{pid};
if ( !$pid_hash{$cpid} )
{
dPrint( DBG_ERROR, "No process with command of '$command' is running\n" );
return();
}


dPrint( DBG_INFO, "'$daemon ".join( ' ', @args )."' stopping at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
$process->{keepalive} = !$final;
kill( 'TERM', $cpid );
delete( $cmd_hash{$command} );


# Now check it has actually gone away, if not kill -9 it
my $count = 0;
while( $cpid && kill( 0, $cpid ) )
{
if ( $count++ > 5 )
{
kill( 'KILL', $cpid );
}
sleep( 1 );
}
}
sub stop
{
_stop( 1, @_ );
}
sub restart
{
my $daemon = shift;
my @args = @_;


my $command = $daemon;
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
my $process = $cmd_hash{$command};
if ( $process )
{
if ( $process->{pid} )
{
my $cpid = $process->{pid};
if ( defined($pid_hash{$cpid}) )
{
_stop( 0, $daemon, @args );
return;
}
}
}
start( $daemon, @args );
}
sub reload
{
my $daemon = shift;
my @args = @_;


my $command = $daemon;
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
my $process = $cmd_hash{$command};
if ( $process )
{
if ( $process->{pid} )
{
kill( 'HUP', $process->{pid} );
}
}
}
sub logrot
{
            zmDbgReinit();
foreach my $process ( values( %pid_hash ) )
{
                if ( $process->{pid} && $process->{command} =~ /^zm.*\.pl/ )
                {
kill( 'HUP', $process->{pid} );
                }
}
}
sub reaper
{
my $saved_status = $!;
while ( (my $cpid = waitpid( -1, WNOHANG )) > 0 )
{
my $status = $?;


my $process = $pid_hash{$cpid};
delete( $pid_hash{$cpid} );


if ( !$process )
{
dPrint( DBG_INFO, "Can't find child with pid of '$cpid'\n" );
next;
}


$process->{stopped} = time();
$process->{runtime} = ($process->{stopped}-$process->{started});
delete( $process->{pid} );


my $exit_status = $status>>8;
my $exit_signal = $status&0xfe;
my $core_dumped = $status&0x01;


my $out_str = "'$process->{daemon} ".join( ' ', @{$process->{args}} )."' ";
                if ( $exit_signal )
                {
                    if ( $exit_signal == 15 || $exit_signal == 14 ) # TERM or ALRM
                    {
       $out_str .= "exited";
                    }
                    else
                    {
       $out_str .= "crashed";
                    }
   $out_str .= ", signal $exit_signal";
                }
                else
                {
   $out_str .= "exited ";
                    if ( $exit_status )
                    {
       $out_str .= "abnormally, exit status $exit_status";
                    }
                    else
                    {
                        $out_str .= "normally";
                    }
                }
#print( ", core dumped" ) if ( $core_dumped );
$out_str .= "\n";


if ( $exit_status == 0 )
{
Info( $out_str );
}
else
{
Error( $out_str );
}


if ( $process->{keepalive} )
{
if ( !$process->{delay} || ($process->{runtime} > ZM_MAX_RESTART_DELAY) )
{
#start( $process->{daemon}, @{$process->{args}} );
# Schedule for immediate restart
$cmd_hash{$process->{command}} = $process;
$process->{pending} = $process->{stopped};
$process->{delay} = 5;
}
else
{
$cmd_hash{$process->{command}} = $process;
$process->{pending} = $process->{stopped}+$process->{delay};
$process->{delay} *= 2;
# Limit the start delay to 15 minutes max
if ( $process->{delay} > ZM_MAX_RESTART_DELAY )
{
$process->{delay} = ZM_MAX_RESTART_DELAY;
}
}
}
}
$SIG{CHLD} = \&reaper;
$! = $saved_status;
}
sub restartPending
{
# Restart any pending processes
foreach my $process ( values( %cmd_hash ) )
{
if ( $process->{pending} && $process->{pending} <= time() )
{
dPrint( DBG_INFO, "Starting pending process, $process->{command}\n" );
start( $process->{daemon}, @{$process->{args}} );
}
}
}
sub shutdownAll
{
foreach my $process ( values( %pid_hash ) )
{
stop( $process->{daemon}, @{$process->{args}} );
}
killAll( 5 );
dPrint( DBG_INFO, "Server shutdown at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
unlink( SOCK_FILE );
unlink( ZM_PID );
close( CLIENT );
close( SERVER );
exit();
}
sub check
{
my $daemon = shift;
my @args = @_;


my $command = $daemon;
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
my $process = $cmd_hash{$command};
if ( !$process )
{
cPrint( "unknown\n" );
}
elsif ( $process->{pending} )
{
cPrint( "pending\n" );
}
else
{
my $cpid = $process->{pid};
if ( !$pid_hash{$cpid} )
{
cPrint( "stopped\n" );
}
else
{
cPrint( "running\n" );
}
}
}
sub status
{
my $daemon = shift;
my @args = @_;


if ( defined($daemon) )
{
my $command = $daemon;
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
my $process = $cmd_hash{$command};
if ( !$process )
{
dPrint( DBG_DEBUG, "'$command' not running\n" );
return();
}


if ( $process->{pending} )
{
dPrint( DBG_DEBUG, "'$process->{command}' pending at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{pending}) )."\n" );
}
else
{
my $cpid = $process->{pid};
if ( !$pid_hash{$cpid} )
{
dPrint( DBG_DEBUG, "'$command' not running\n" );
return();
}
}
dPrint( DBG_DEBUG, "'$process->{command}' running since ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}" );
}
else
{
foreach my $process ( values(%pid_hash) )
{
my $out_str = "'$process->{command}' running since ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}";
$out_str .= ", valid" if ( kill( 0, $process->{pid} ) );
$out_str .= "\n";
dPrint( DBG_DEBUG, $out_str );
}
foreach my $process ( values( %cmd_hash ) )
{
if ( $process->{pending} )
{
dPrint( DBG_DEBUG, "'$process->{command}' pending at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{pending}) )."\n" );
}
}
}
}


}
else
{
Fatal( "Can't fork: $!" );
}
}
if ( $command eq "check" && !$daemon )
{
print( "running\n" );
exit();
}
elsif ( $command eq "startup" )
{
# Our work here is done
exit() if ( !$server_up );
}
# The server is there, connect to it
#print( "Writing commands\n" );
CLIENT->autoflush();
my $message = "$command";
$message .= ";$daemon" if ( $daemon );
$message .= ";".join( ';', @args ) if ( @args );
print( CLIENT $message );
shutdown( CLIENT, 1 );
while ( my $line = )
{
chomp( $line );
print( "$line\n" );
}
close( CLIENT );
#print( "Finished writing, bye\n" );


exit;


sub killAll
{
my $delay = shift;
sleep( $delay );
foreach my $daemon ( @daemons )
{
my $cmd = "killall --quiet --signal TERM $daemon";
Debug( $cmd );
qx( $cmd );
}
sleep( $delay );
foreach my $daemon ( @daemons )
{
my $cmd = "killall --quiet --signal KILL $daemon";
Debug( $cmd );
qx( $cmd );
}
}




h265.sh

#!/bin/bash file="../mp4" if [ -d $file ]; then         echo "  Директория существует :-) "         for i in *.mp4; do f...