sapo sessions linux power tools

46
SAPO S e s s io n s Linux P o w e r To o ls - 3 Armando R e is DTP

Upload: armando-reis

Post on 04-Jul-2015

752 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sapo sessions linux power tools

S AP O S e s s io n s

Lin u x P o w e r To o ls

- 3Arm a n d o R e is D TP

Page 2: Sapo sessions linux power tools

Lin u x P o w e r To o ls

' ' , P o w e r t o o ls s ã o f e r r a m e n t a s c o m a n d o s o u, u t i l i t á r io s q u e n o s p e rm it e m m a io r

/ / e f ic á c ia r a p id e z a u t o m a t is m o n a e x e c u ç ã o .d e t a r e f a s

S ig n if ic a m d if e r e n t e s c o is a s p a r a d if e r e n t e s.p e s s o a s

Page 3: Sapo sessions linux power tools

s he ll – ' A p r im e ir a p o w e r't o o l

;A s h e ll é a p r im e ir a in t e r f a c e c o m o s is t e m a

S e ja q u a l f o r a v o s s a s h e ll p r e f e r id a . a p r e n d a m a t ir a r p a r t id o d e la

: , G o o d ie s c o m m a n d c o m p le t io n c o m m a n d lin e. . . e d it in g

– b u ilt in s E x is t e m v á r io s c o m a n d o s . e m b u t id o s n a p r ó p r ia s h e ll

Page 4: Sapo sessions linux power tools

( )s h e ll b u ilt in s b a s h

- :b a s h d e f in e s t h e f o llo w in g b u ilt in c o m m a n d s

:, . , [, , , , , , , , , a lia s b g b in d b r e a k b u ilt in c a s e c d c o m m a n d, , , , , , c o m p g e n c o m p le t e c o n t in u e d e c la r e d ir s d is o w n

, , , , , , , , , e c h o e n a b le e v a l e x e c e x it e x p o r t f c f g g e t o p t s, , , , , , , , , , h a s h h e lp h is t o r y if jo b s k il l le t lo c a l lo g o u t p o p d

, , , , , , , , p r in t f p u s h d p w d r e a d r e a d o n ly r e t u r n s e t s h if t, , , , , , , , s h o p t s o u r c e s u s p e n d t e s t t im e s t r a p t y p e t y p e s e t, , , , , , .u lim it u m a s k u n a lia s u n s e t u n t il w a it w h ile

. . .A lg u n s f ic h e ir o s p a r a v is it a r

. _b a s h p r o f ile

.b a s h r c

. _b a s h a lia s e s

. _b a s h h is t o r y

Page 5: Sapo sessions linux power tools

– m a n m a n is y o u r f r ie n d

'O c o m a n d o man' p e rm it e a c e d e r à s p á g in a s .d o m a n u a l

, C o n t é m a d e s c r iç ã o d e c a d a c o m a n d o c o m a , .s u a f u n ç ã o p a r â m e t r o s e q u a lif ic a d o r e s

$ man man <- ' ' a m a n p a g e d o p r ó p r io c o m a n d o.m a n

$ apropos$ info

Page 6: Sapo sessions linux power tools

a lia s

[- ] [ [= ] . . . ]a lia s p n a m e v a lu e

P e rm it e d e f in ir n o m e s q u e s e r ã o in t e r p r e t a d o s p e la s h e l l e s u b s t it u íd o s p e lo

;v a lo r

$ alias SSH=”eval 'ssh-agent -s'; ssh-add”$ alias ll=”ls -l”

Page 7: Sapo sessions linux power tools

Te x t p r o c e s s in g t o o ls

awk; sed; tr. . .

O r ig in a lm e n t e d e s e n v o lv id o s n o â m b it o d oUNIX, - f o r a m m a is t a r d e r e e s c r it a s p e lo

p r o je c t o GNU.

E x is t e m n a d is t r ib u iç ã o b a s e d e q u a lq u e r .l in u x c o m u m

Page 8: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k I

P a t t e r n s c a n n in g a n d t e x t p r o c e s s in gla n g u a g e

'O a w k p r o c e s s a u m fiche iro ' . d e in p u t

'Av a lia o in p u t record' ' ' a r e c o r d p r o c u r a n d o' 'p a d r õ e s

'S e o padrão' 'e x is t ir e x e c u t a o s comandos ' .a s s o c ia d o s

'C a d a r e c o r d p o d e t e r v á r io s fie lds ' - 1 .. .$ $ n

:Va r iá v e is e s p e c ia is $0; NR; RS; NF; FS; OFS. . . - :P a d r õ e s p r é d e f in id o s < >; n a d a BEGIN; END;

Page 9: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k II

F o rm a s m a is c o m u n s

$ awk 'pattern {program}' file$ cat file | awk 'pattern {program}'

$ awk -f program-file input-file

- O f ic h e ir o p r o g r a m f ile c o n t e r á o s p a d r õ e s e e x p r e s s õ e s q u e c o n s t it u e m o n o s s o

' ' .p r o g r a m a

Page 10: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k III

.P a d r õ e s

, . . .P o d e m s e r s t r in g s r e g e x p s

/ /s t r in g - .Va lid a a s t r in g “ s t r in g ”

/[0 -9 ]+/ - .Va lid a s e e x is t e m d íg it o s

/ 1 /P o d e m r e f e r e n c ia r c a m p o s $

||P o d e m u s a r && e( $1 == “foo” ) && ( $2 == “bar” ) { print }

Page 11: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k IV

.P r o g r a m a

.S ã o a s in s t r u ç õ e s a s e r e m e x e c u t a d a s

S ã o m u it a s in s t r u ç õ e s e o p e r a d o r e s e n t r e o s:q u a is

print; print0;= +=...== < > <= ( )e m u it a c o is a f a m il ia r p a r a q u e m u s a C

Suporta ARRAYS...

Page 12: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k V

:E x e m p lo s im p le s$ cat /etc/resolv.confsearch old.bk.sapo.pt co.sapo.pt sapo.ptnameserver 212.55.154.194nameserver 212.55.154.174

$ cat /etc/resolv.conf | awk '/nameserver/ {print $2}'212.55.154.194212.55.154.174

Page 13: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k VI

E x e m p lo m a is:e la b o r a d o

BEGIN {FS=”\n”RS=””ORS=””

}{ x=1

while (x < NF ) {print $x “; ”x++

}print $NF “\n”

}

Page 14: Sapo sessions linux power tools

– Te x t p r o c e s s in g a w k VII

E x e m p lo m a is:e la b o r a d o

BEGIN {FS=”\n”RS=””ORS=””

}{ x=1

while (x < NF ) {print $x “; ”x++

}print $NF “\n”

}

$ cat textfiletexto linha 12Três

linha 5e acaba na 6

$ awf -f programa textfile

texto linha 1; 2; Trêslinha 5; e acaba na 6

Page 15: Sapo sessions linux power tools

– Te x t p r o c e s s in g s e d I

.S t r e a m e d it o r

, Ac t u a s o b r e o s e u in p u t p e rm it in d o e f e c t u a r .t a r e f a s d e e d iç ã o

S in t a x e s e m e lh a n t e à d o vi.

.E d it o r d e lin h a

.Ac e it a e x p r e s s õ e s r e g u la r e s

Page 16: Sapo sessions linux power tools

– Te x t p r o c e s s in g s e d II

:F o rm a s m a is c o m u n s

$ sed -e 'comandos' ficheiro

$ cat ficheiro | sed -e 'comandos'

$ sed -f ficheiro-comandos ficheiro

.O s c o m a n d o s s ã o e x e c u t a d o s e m s e q u ê n c ia

Page 17: Sapo sessions linux power tools

– Te x t p r o c e s s in g s e d III

. . .C o m a n d o s

– s S u b s t it u t es/XX/YY/ - S u b s t i t u i p r im e ir o XX p o r YY

s/XX/YY/g - S u b s t i t u i t o d o s o s XX p o r YY

- d D e le t ed - Ap a g a a lin h a

1d - 1Ap a g a a lin h a

1,5d - 1 5Ap a g a a s l in h a s a

Page 18: Sapo sessions linux power tools

– Te x t p r o c e s s in g s e d IV

Ac e it a r e g e x p s

?R e t ir a r c o m e n t á r io s$ sed -e '/^#/d' ficheiro | more

Ve r a p e n a s a f u n ç ã o m a in d e u m p r o g r a m a ?e m C

$ sed -n -e '/main[[:space:]]*(/,/^}/p' source.c | more

(2 : r e g e x p s /main[[:space:]]*(/ e /^}/ )

Page 19: Sapo sessions linux power tools

– Te x t p r o c e s s in g s e d V

.In lin e e d it in g

$ sed -i -e 's/Hello/Goodbye/g' ficheiro

' ' ' ' M u d a t o d a s a s s t r in g s H e llo p a r a G o o d b y e .n o f ic h e ir o

' - ' !S e m u m p a r â m e t r o n o i n ã o f a z b a c k u p

$ sed -i.bak -e 's/Hello/Goodbye/g' ficheiro

Page 20: Sapo sessions linux power tools

– Te x t p r o c e s s in g t r I

.Tr a n s la t e o r d e le t e c h a r a c t e r s

$ tr [options] [set1 [set2] ]

Lê o in p u t e s u b s t it u i c a r a c t e r e s c o m b a s e , .e m d o is s e t s e s c r e v e n o o u t p u t

U m d o s p o u c o s c o m a n d o s q u e a p e n a s .r e c e b e in p u t v ia S TD IN

Page 21: Sapo sessions linux power tools

– Te x t p r o c e s s in g t r II

:E x e m p lo s$ echo 'text' | tr “[:lower:]” “[:upper:]”$ echo 'text' | tr “a-z” “A-Z”

:F la g s

-c 1C o m p le m e n t s e t

-d D e le t e

-s ( S q u e e z e C o n v e r t e c a r a c t e r e s r e p e t id o s )n u m s ó

-t ( 1 Tr u n c a t e Tr u n c a o s e t p a r a f ic a r c o m o 2 . m e s m o t a m a n h o q u e o s e t O b r ig a a

)e s p e c if ic a r o s d o is s e t s

Page 22: Sapo sessions linux power tools

– Te x t p r o c e s s in g t r III

$ tr -s ' ' ' ' < input-file > outputfile

( )a g r e g a o s e s p a ç o s n o f ic h e ir o

$ echo $PATH | tr ':' '\n'/home/areis/bin/usr/local/bin/usr/local/sbin/usr/bin/usr/sbin

Page 23: Sapo sessions linux power tools

– Te x t p r o c e s s in g g r e p I

.P e s q u is a p o r p a d r õ e s n o s e u in p u t$ grep [options] patterns [files...]$ grep -f pattern-file [files...]

P e s q u is a p o r t e x t o s im p le s o u p o r , e x p r e s s õ e s r e g u la r e s p r o c e s s a n d o lin h a a

.l in h a o in p u t

Page 24: Sapo sessions linux power tools

– Te x t p r o c e s s in g g r e p II

:A lg u m a s o p ç õ e s m a is c o m u n s

-i C a s e In s e n s it iv e

-e In t e r p r e t a r o p a d r ã o c o m o u m a r e g e x p

-v In v e r t e r o m a t c h

-c C o n t a r o n ú m e r o d e m a t c h e s

-l M o s t r a r a p e n a s o n o m e d o s f ic h e ir o s o n d e h o u v e m a t c h

-n M o s t r a r o n ú m e r o d a lin h a q u e f e z m a t c h

-R P e s q u is a r r e c u r s iv a m e n t e n o s v á r io sf ic h e ir o s

Page 25: Sapo sessions linux power tools

– Te x t p r o c e s s in g g r e p III

:C o n t e x t o

-A n ( ) M o s t r a a s n lin h a s d e p o is a f t e r d om a t c h

-B n ( ) M o s t r a a s n lin h a s a n t e s b e f o r e d om a t c h

-C n M o s t r a n lin h a s a n t e s e d e p o is d om a t c h

--color D e s t a c a o p a d r ã o c o m t e x t oc o lo r id o

Page 26: Sapo sessions linux power tools

– Te x t p r o c e s s in g g r e p IV

.R e g e x p s v s E x t e n d e d R e g e x p s

( -e v s -E)

In ic ia lm e n t e a s e x p r e s s õ e s r e g u la r e s d o grep e d o egrep . e r a m m u it o b á s ic a s N ã o

s u p o r t a v a m v á r io s d o s m e t a c a r a c t e r e s ' ? ' , ' +' , ' {' . . .c o m o e t c

, O G N U g r e p já a s s u p o r t a n o e n t a n t o h á q u e - (d e c id ir e n t r e r e t r o c o m p a t ib il id a d e -e) e a

(im p le m e n t a ç ã o c o r r e c t a d a s r e g e x p s -E).

!P o r t a b il i t y Wa r n in g

Page 27: Sapo sessions linux power tools

s e q

P r in t a s e q u e n c e o f n u m b e r s

$ seq 1 5

$ seq 1 2 10

$ seq -sSEP 1 5

$ seq -f”%03.f” 1.6 0.5 4

Page 28: Sapo sessions linux power tools

s e q

$ seq 1 512345

$ seq 1 2 1013579

$ seq -s: 1 5

1:2:3:4:5

$ seq -f”%0.3f” 1.6 0.5 4

1.600

2.100

2.600

3.100

3.600

Page 29: Sapo sessions linux power tools

s e q

$ for a in `seq -f”%02.f” 1 3`> do> scp file$a web${a}.domain> done

$ for a in “01 02 03”> do> scp file$a web${a}.domain> done

Page 30: Sapo sessions linux power tools

f in d a n d x a r g s I

.P r o c u r a r e c u r s iv a d e f ic h e ir o s

[ ] [ ]$ f in d in íc io f i lt r o c o m a n d o s

$ find / -type f -size +10000c -ls$ find / -mount -type -f -size 100M -ls

$ find /path -type f -newer file

$ find / -type f -iname “*.log” -mtime +15 -ls$ find / -type f -name “*log” -mtime +15 -exec gzip {} \;

Page 31: Sapo sessions linux power tools

f in d a n d x a r g s II$ for a in `seq 1 100000` do touch /path/ola.$a; done$ rm /path/ola*

/ / : <--- ? ? ?b in rm Ar g u m e n t l is t t o o lo n g

$ find /path -type f -exec rm {} \;

?Q u a n t o s f ic h e ir o s a p a g a

C o m o c o n f ir m a r q u e v a i a p a g a r o q u e?q u e r e m o s

Page 32: Sapo sessions linux power tools

f in d a n d x a r g s III$ for a in `seq 1 100000` do touch /path/ola.$a; done$ rm /path/ola*

/ / : <--- ? ? ?b in rm Ar g u m e n t l is t t o o lo n g

xargs .c o n s t r ó i e e x e c u t a c o m a n d o s

C o n t r o la o t a m a n h o d a lis t a d e a r g u m e n t o s

$ find /path -type f -print | xargs rm

$ find /path -type f -print0 | xargs -0 rm 

Page 33: Sapo sessions linux power tools

f in d a n d x a r g s IV

?M o v e r u m g r a n d e n ú m e r o d e f ic h e ir o s

$ find . -maxdepth 1 -type f -name “*.log” -print0 \> | xargs -0 -I XXX mv XXX /dead-archive

-maxdepth - .N ú m e r o d e d ir e c t o r ia s a p r o c e s s a r

-I XXX - ' - ' – R e p la c e s t r in g Id e n t if ic a o sp a r â m e t r o s

p a r a o c o m a n d o q u e s e v a i.c o n s t r u ir

( ' ' XXX a s e g u ir a o m v é o n d e s e ir ã o c o lo c a r o s )f ic h e ir o s a m o v e r

Page 34: Sapo sessions linux power tools

s c r e e n I

.M u lt ip le x b e t w e e n m u lt ip le p r o c e s s e s

screen p e rm it e t e r v á r ia s s e s s õ e s d e n t r o d o . ' ' m e s m o t e rm in a l E f e c t u a u m d e c o u p lin g d o

.t e rm in a l d a s s e s s õ e s p o r e le in ic ia d a s

.P e rm it e “ d e s l ig a r ” e “ r e t o m a r m a is t a r d e ”

CTRL A – . S e q u ê n c ia d e c o m a n d o d o s c r e e n

Page 35: Sapo sessions linux power tools

s c r e e n II

E x e c u ç ã o d o s c r e e n n a s h e l l

$ screen comando [args] - e x e c u t a o c o m a n d o

$ screen -opções comando [args] - id e m

$ screen -ls - Lis t a a s s e s s õ e s e x is t e n t e s

$ screen -r [sessão] - R e s u m e d e u m a s e s s ã o

Page 36: Sapo sessions linux power tools

s c r e e n III

D e n t r o d o s c r e e n

CTRL A – C : La n ç a u m a n o v a s e s s ã o

CTRL A – W : Lis t a a s s e s s õ e s c r ia d a s

CTRL A – N : N e x t

CTRL A – P : P r e v io u s

CTRL A – D : D e t a c h

Page 37: Sapo sessions linux power tools

s c r e e n IV

$ hostnameHOST1$ screen ssh user@sistema1 ....user@sistema1:~$ hostnamesistema1

user@sistema1:~$ - <- C TR L A D

D e t a c h[detached]$ hostnameHOST1

$ screen -ls8551.pts-2.HOST1 ...

$ screen -ruser@sistema1:~$

Page 38: Sapo sessions linux power tools

s t r a c e I

.Tr a c e s y s t e m c a lls

É u m a f e r r a m e n t a d e debug .in t e r e s s a n t e

M o s t r a a s v á r ia s c h a m a d a s a o s is t e m a q u e .u m p r o g r a m a e x e c u t a

P o d e s e r a p lic a d o a u m p r o g r a m a e m.e x e c u ç ã o

.P r o f il in g r u d im e n t a r

Page 39: Sapo sessions linux power tools

s t r a c e II - - $ s t r a c e p P ID P a r a u m p r o c e s s o já a

.c o r r e r

– $ s t r a c e p r o g r a m a r g s P a r a u m n o v o.p r o c e s s o

$ strace pwd 2>&1 | wc -l115$ strace uptime 2>&1 | wc -l218$ ltrace pwd 2>&1 | wc -l16$ ltrace uptime 2>&1 | wc -l4

Page 40: Sapo sessions linux power tools

s t r a c e III$ strace pwdexecve("/bin/pwd", ["pwd"], [/* 41 vars */]) = 0brk(0) = 0xe70000...open("/etc/ld.so.cache", O_RDONLY) = 3...getcwd("/home/areis"..., 4096) = 10...write(1, "/home/areis\n"..., 10/home/areis

Page 41: Sapo sessions linux power tools

s t r a c e IV

A lg u m a s f la g s

-c , , .C o n t a s t a t s d e c h a m a d a s e r r o s e t e m p o s

-f .F o llo w c h ild p r o c e s s e s

-ff . (- )F o llo w c h ild s in f i le s o f i le n a m e

-r R e la t iv e t im e s t a m p

-e (- = , ) (! ) E x p r e s s io n e t r a c e o p e n c lo s e o p e n(= )s ig n a l

Page 42: Sapo sessions linux power tools

s t r a c e V$ strace -c ps aux...% time seconds usecs/call calls errors syscall------ ----------- ----------- --------- --------- -------- 55.56 0.000060 0 495 read 36.11 0.000039 0 505 2 open 8.33 0.000009 0 158 write... 0.00 0.000000 0 1 geteuid 0.00 0.000000 0 1 arch_prctl------ ----------- ----------- --------- --------- --------100.00 0.000108 2327 16 total

Page 43: Sapo sessions linux power tools

s t r a c e VI$ strace -eopen pwd (Output parcial)open("/etc/ld.so.cache", O_RDONLY) = 3open("/lib/libc.so.6", O_RDONLY) = 3open("/dev/urandom", O_RDONLY) = 3open("/usr/lib/locale/locale-archive", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/usr/share/locale/locale.alias", O_RDONLY) = 3open("/usr/lib/locale/en_US.UTF-8/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No such file or directory)

open("/usr/lib/locale/en_US.utf8/LC_IDENTIFICATION", O_RDONLY) = 3

open("/usr/lib/locale/en_US.utf8/LC_CTYPE", O_RDONLY) = 3/home/areis

Page 44: Sapo sessions linux power tools

s t r a c e VII$ strace -p 8547attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted

$ strace -efile hostnameexecve("/bin/hostname", ["hostname"], [/* 41 vars */]) = 0access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (...)access("/etc/ld.so.preload", R_OK) = -1 ENOENT (...)open("/etc/ld.so.cache", O_RDONLY) = 3access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (...)open("/lib/libc.so.6", O_RDONLY) = 3open("/dev/urandom", O_RDONLY) = 3aqua

Page 45: Sapo sessions linux power tools

. . .C lo s in g t im e

? --> , , ?M a is in f o m a n p a g e s g o o g le f r ie n d s

O u t r a s power tools:lsof – .Lis t o p e n f ile s

netcat - - / .“ S w is s a rm y k n if e ” f o r TC P IP

netstat – P r in t n e t w o r k in f o rm a t io n

tcpdump – D u m p n e t w o r k t r a f f ic

terminator – .M u lt ip le t e rm in a ls in o n e w in d o w

Page 46: Sapo sessions linux power tools

S AP O S e s s io n s

Lin u x P o w e r To o ls

- 3Arm a n d o R e is D TP