Unixcyclopedia - cheat și lsof
May 4th, 2008 Alex
Pentru că am cam aberat în ultimul timp numai prostii nefolositoare
și pentru că săptămâna asta am fost liber, mă simt dator să reiau o mica serie de articole ce se concentra pe învățarea de comenzi din terminal … unixcyclopedia. Nu știu dacă folosește cuiva, dar personal mi-e atât de lene să învăț unelte de productivitate că-mi trebuie o motivație să o fac.
command line ruby cheat sheets
cheat este un utilitar în linia de comandă cu ajutorul căruia se pot afișa trucuri și sfaturi despre diverse unelte (ce au în general legătură cu Ruby1 , dar nu numai).
Pentru instalare, aveți nevoie în prealabil de Ruby și de RubyGems2. Dacă cerințele sunt respectate, instalarea se realizează cu un simplu:
gem install cheatOdată ce instalarea a fost efectuată, putem afla exemple de utilizare despre o grămadă de unelte.
Și sincer să fiu, articolul inițial aș fi vrut să fie despre lsof (list open files), un utilitar extrem de util ce afișează informații despre fișiere deschise de procese. Și aici vine șmecheria … un fișier în Unix poate fi un fișier fizic de pe harddisk, sau un director, sau un device hardware, sau un fișier de rețea (e.g. Internet socket, NFS sau Unix Domain socket). Practic aproape orice resursă Unix poate fi (și este în general) accesată ca un fișier.
Și aici intervine lsof:
- vrei să afli ce procese comunică pe portul 80?
- vrei să afli ce procese blochează un anumit fișier? sau un anumit port?
- vrei să afli ce fișiere/porturi sunt folosite de o anumită aplicație sau de un anumit utilizator?
- sau poate vrei să afli ce procese s-au blocat așteptând la o resursă blocată de un alt proces?
Și acestea sunt doar câteva din întrebuințările posibile … comanda este atât de cool. Și din fericire nu a trebuit să sap prin documentația man. Tot ce a trebuit să fac a fost ceva de genul …
cheat lsof | less
Iar output-ul este ceva de genul …
lsof -i -- Show all connections lsof -iTCP -- Show only TCP connections (works the same for UDP) lsof -i :22 -- -i :port shows all networking related to a given port lsof -i@192.168.1.5 -- To show connections to a specific host, use @host lsof -i@192.168.1.5:22 -- Show connections based on the host and the port using @host:port lsof -i| grep LISTEN -- Grepping for "LISTEN" shows what ports your system is waiting for connections on lsof -i| grep ESTABLISHED -- Grepping for "ESTABLISHED" shows current active connections lsof -u ecable -- Show what a given user has open using -u lsof -c syslog-ng -- See what files and network connections a command is using with -c lsof /var/log/messages -- Pointing to a file shows what's interacting with that file lsof -p 10075 -- The -p switch lets you see what a given process ID has open, which is good for learning more about unknown processes lsof -t -c Mail -- The -t option returns just a PID lsof -a -u ecable -i @1.1.1.1 -- Using-a allows you to combine search terms, so the query below says, "show me everything running as daniel connected to 1.1.1.1" kill -HUP `lsof -t -c sshd` -- Using the -t and -c options together you can HUP processes kill -9 `lsof -t -u daniel` -- You can also use the -t with -u to kill everything a user has open lsof +L1 -- lsof +L1 shows you all open files that have a link count less than 1, often indicative of a cracker trying to hide something
Wow, that’s cool.
Note:
- Ruby este un limbaj de scripting modern, robust, orientat pe obiecte, și cu o comunitate sănătoasă
- RubyGems este manager de pachete din comunitatea Ruby
Posted in tips&tricks, unixcyclopedia | No Comments »