Unix HOWTOs and Tips Short unix command line administration tips and scripts

11Jul/140

Execute identical commands in all running OpenVZ containers on a hardware host

Suppose, that you have to execute some identical commands on all your running OpenVZ containers. For example you may want to check their disk usage, or ban an IP on all of them, or force a restart of their webserver for some reason. Typing the same commands over and over again is tiresome, and error prone. Suppose also, that you do not have an automated system like Ansible already (which by the way, I highly recommend) ...

Do not worry, just follow these steps:

1) Save the following script as allvpses_do, somewhere on your PATH, and make it executable:

#!/bin/bash

THECMD=$*

for i in `vzlist -o veid -H|  xargs`; do
   echo "============================  VPS: $i: CMD: $THECMD ===================================";
   echo "$THECMD" | vzctl exec $i - | grep --line-buffered -v 'Executing command:' | sed -e "s@^@VPS $i: @gm"
   echo;
done

2) Run a test command like this:

allvpses_do uptime

... or, you can try:

allvpses_do 'iptables --list -nv|grep DROP'

3) Enjoy your puppet master powers :-)

4Oct/120

Как да изпращаме HTML писма на кирилица, чрез PHP

Ако желаете да изпращате писма съдържащи кирилица, и форматиращи тагове, от PHP скрипт, можете да използвате следната функция:

function email_html($target, $sender, $subject, $htmlbody){
  $headers  = "MIME-Version: 1.0\r\n";
  $headers .= "From: {$sender}\r\n";
  $headers .= "Reply-To: {$sender}\r\n";
  $headers .= "Return-Path: {$sender}\r\n";
  $headers .= "Content-Type: text/html; charset=UTF-8; format=flowed\r\n";
  $headers .= "Content-Transfer-Encoding: base64 \r\n";
  $headers .= "X-Mailer: PHP/" . phpversion();

  $html = '<'.'!DOCTYPE'.' HTML>'.'<'.'head><'.'meta http-equiv="Content-Type" content="text/html; charset=utf-8">'.$htmlbody.'<'.'/body'.'><'.'/html>';
  $message = chunk_split(base64_encode($html));
  $subject = '=?UTF-8?B?'.base64_encode($subject).'?=';

  return @mail($target, $subject, $message, $headers);
}

Употребата и е проста (приемаме, че ще изпращаме писма с UTF-8 кодировка, съответно php скрипта ни или данните ни от базата също трябва да са в нея ... ):

$target = "targetmail@example.com";
$sender = "spammer@spammerhouse.com";
$subject = "Важно : това заглавие съдържа кирилица, но ще се покаже коректно в gmail.com и abv.bg";
$htmlbody = "Начало на тялото на съобщението....

Това е заглавен ред

А това е наклонен текст."; email_html($target, $sender, $subject, $htmlbody);
30Oct/110

Simple PHP cyr2lat command line transliteration filter from bulgarian to latin

Sometimes you need to easily convert some Cyrillic Bulgarian text to its latin equivalent (a process known as "romanization", see Romanization of Bulgarian ).

A possible use case scenario is making slugs for urls, containing bulgarian.

Since it is a common task, in the best Unix tradition, it is very usefull to have a simple command line filter, into which you can pipe the cyrillic text, and producing the romanized version in its output.

Here is a simple version of the command line filter cyr2lat, written in php, that does just that:

#!/usr/bin/env php
<?php
$cyr  = array('а','б','в','г','д','е','ж','з','и','й','к','л','м','н','о','п','р',
              'с','т','у','ф','х','ц','ч','ш','щ','ъ','ь','ю','я',
              'А','Б','В','Г','Д','Е','Ж','З','И','Й','К','Л','М','Н','О','П','Р',
              'С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ь', 'Ю','Я' );
$lat = array( 'a','b','v','g','d','e','zh','z','i','y','k','l','m','n','o','p','r',
              's','t','u','f' ,'h' ,'ts' ,'ch','sh' ,'sht' ,'a' ,'y' ,'yu','ya',
              'A','B','V','G','D','E','Zh','Z','I','Y','K','L','M','N','O','P','R',
              'S','T','U','F' ,'H' ,'Ts' ,'Ch','Sh' ,'Sht' ,'A' ,'Y' ,'Yu' ,'Ya' );

$in = fopen ("php://stdin","r");
while($line = fgets($in)){
    echo str_replace($cyr, $lat, $line);
}

Source of cyr2lat.php

To use it, just save it to a file named cyr2lat.php, then make this script executable by:

chmod 755 cyr2lat.php

... and possibly move it to a location in your path:

mv cyr2lat.php /usr/local/bin

or

mv cyr2lat.php ~/bin

After this, you can run for example:

echo "Това е текст на кирилица" | cyr2lat.php

and you will get:

Tova e tekst na kirilitsa

 

NB: This filter assumes that the input text is in the utf8 encoding. If you have an input text in the cp1251 encoding, just pipe it first through iconv, like this:

echo "Това е пак текст на кирилица, но този път с кодировка cp1251" |iconv -fcp1251 -tutf8 |cyr2lat.php