Вы находитесь на странице: 1из 48

NMAP one-liners:

Port scanning
scan all reserved TCP ports (well known + registered) in verbose mode
nmap -v <host>

scan only certain ports


nmap -v <host> -p <80>,<443>

scan ranges of ports


nmap -v <host> -p <80>-<to90>

Network scanning/Host enumeration


scan a whole subnet/network
nmap -v <network>/<24>

scan only specific host ranges


nmap -v <10.0.0.1-10>
nmap -v <10.0.1-10.1-254>

Pings
ping with ICMP request to see whether host is up before probing
nmap -v <host> -PE

ping with SYN segments to see whether host is up before probing


nmap -v <host> -PS <port1>,<port2>

ping with UDP datagrams to see whether host is up before probing


nmap -v <host> -PU <port1>,<port2>,<port3>

Operating systems detection


attempt to detect/discover targets operating system by comparing all tests results with nmap known OS fingerprints
database
nmap -O <host>

attempt to detect/discover targets operating system only if one open and one closed TCP port are found (more reliable)
nmap -O <host> --osscan-limit

attempt to detect/discover targets operating system and display imperfect matches by trying to guess the closest possible
OS (accuracy level expressed in %)
nmap -O <host> --osscan-guess --version-intensity <1to9>
Services version detection
attempt to detect/discover services versions
nmap -sV <host>

change the version detection intensity level (from 1 to 9, default is 7, the higher the more reliable but the more resources
consuming)
nmap -sV <host>

DNS resolution
disable reverse DNS resolution (reverse DNS resolution is enabled by default and can add dramatically increase total scan
time)
nmap -nv <host>

Idle/Zombie Scan
works by checking the zombie's IP ID, probing the target with the zombie's IP as source IP, checking the zombie's IP ID
again: if difference = 1, then port's closed, if different = 2, port's opened
nmap -sI <zombie> <target>

TCP scans
perform a TCP connect scan (SYN, SYN-ACK, ACK)
nmap -sT <host>

perform a TCP stealth SYN scan (SYN then RST, never complete handshake)
nmap -sS <host>

perform an TCP ACK scan (to detect presence of firewall with advanced rulesets)
nmap -sA <host>

perform a TCP FIN scan (to pass by firewalls)


nmap -sF <host>

perform a TCP maimon scan (FIN + ACK)


nmap -sM <host>

perform a TCP NULL scan (no TCP flags)


nmap -sN <host>

perform a TCP Xmas tree scan (URG + PUSH + FIN)


nmap -sX

UDP scans
nmap -sU <host>

Scan timing modes


change/set the timing mode

T0 - paranoid: no parallel probing, 5 minutes between each packet

T1 - sneaky: no parallel probing, 15 secs between each packet

T2 - polite: no parallel probing, 0.4 secs between each packet

T3 - normal: default mode, runs all scans as fast as possible

T4 - aggressive: default mode + 5 minute min host timeout + 1.12sec max probe timeout)

T5 - insane: default mode + 75 sec min host timeout + 0.3 sec max probe timeout)
nmap -v <host> -T<0-5>

Timeouts/Delays
timeout in miliseconds unless a suffix is used ( 's' for seconds, 'm' for minutes, or 'h' for hours)

change/set the host timeout (time spent in total to probe a host before giving up)
nmap -v <host> --host-timeout <X>

change/set the probe max timeout (maximum time waiting for a response to a probe before giving up or retrying)
nmap -v <host> --max-rtt-timeout <X>

change/set the probe min timeout (minimum time waiting for a response to a probe before giving up or retrying)
nmap -v <host> --in-rtt-timeout <X>

change/set the max delay between 2 probes


nmap -v <host> --max-scan-delay <X>

change/set the min delay between 2 probes on to the same host


nmap -v <host> --scan-delay <X>

Parallel probing
change/set the max number of hosts probed at the same time
nmap -v <network>/<mask> --max-hostgroup <X>

change/set the min number of hosts probed at the same time


nmap -v <network>/<mask> --min-hostgroup <X>

change/set the max number of probes running at the same time


nmap -v <host> --max-parallelism <X>

change/set the min number of probes running at the same time


nmap -v <host> --min-parallelism <X>
Script scanning
nmap executes a script on UDP or opened TCP ports and provides results . nmap built-in scripts are stored in the nmap
"scripts" folder. Use "grep <keyword> scripts/script.db" to check on scripts' run level/category

scan with the "default" script set


nmap -sC

change the script set

"safe": scripts not deisgned to crash services

"intrusive": scripts that are likely to crash services, consume significant resources on target host or be perceived as
attacks

"descovery": e.g. SNMP

"external": e.g. WHOIS

"malware": check whether host is infected by malwares

"vuln": check host for known vulnerabilities


nmap --script "<safe>|<intrusive>|<discovery>|<external>|<malware>|<vuln>"

scan with default scripts that are not intrusive


nmap --script "<default> and not <intusive>"

scan with scripts that are part of the "external" or "discovery" sets and are "safe
nmap --script "(<external> or <discovery>) and <safe>"

scan only with the scripts whose names start with "snmp"
nmap --script "<snmp>*"

pass arguments to a script (argument syntax similar to Lua tables syntax)


nmap --script "</path/to/myscript>" --script-args <arg1>=<val1>,<arg2>=<val2>

read/access the arguments from within the script


local <var1> = nmap.registry.args.<arg1>
local <var2> = nmap.registry.args.<arg2>

update the scripts database ("scripts/script.db"). Use only if you have added,removed or modified scripts
nmap --script-updatedb
DIG one-liners

Basics
retrieve all information/records for a domain
dig <domain>

retrieve only A records (IP addresses) for a domain


dig <domain> A

retrieve only AAAA records (IPv6 addresses) for a domain


dig <domain> AAAA

retrieve only MX records (mail servers) for a domain


dig <domain> MX

retrieve only NS records (DNS servers) for a domain


dig <domain> NS

emulate the host command with dig to do reverse lookups (PTR records)
dig -x <ipaddress>

retrieve any records for a domain


dig <domain> ANY

Multiple domains lookup


multiple domains lookup
dig <domain1> <domain2> <domain3>

multiple domains lookup from a file (one domain per line)


dig -f </path/to/file>

Client behaviour
set/change the query timeout (default it 5, min is 1)
dig <domain> +time=<timeInSec>

set/change the number of retries (default is 2, doesn't include the initial query)
dig <domain> +retry=<number>

set/change the source ip and port (must be one of the host's network interfaces)
dig -b "<sourceIp>#<sourcePort>" <domain>
use TCP instead of UDP
dig <domain> +tcp

change the queried DNS server


dig @<DNShostnameOrIp> <domain>

Answer/Display formatting
retrieve only the short answer (for instance just the IP address)
dig <domain> A +short

don't display the comments


dig <domain> +nocomments

clear all display flags (the answer will be empty at this stage)
dig <domain> +noall

show only the statistics (query time, DNS server that replied, date and size of the response)
dig <domain> +noall +stats

DNS servers
send DNS requests to a particular DNS server
dig @<DNSserverIPorHostname> <domain>

attempt to find the authoritative DNS servers and provide the SOA (Start Of Authority) records from each DNS server found.
dig <domain1> +nssearch

see which DNS server resolved the query


dig <domain> +short +identify

Debugging
trace/follow the DNS resolution (see requests to each intermediate DNS server)
dig <domain> +trace

best effort to display malformed answers (not displayed by default)


dig <domain> +besteffort
TCPDUMP one-liners:

Basics
capturing everything on a particular interface
tcpdump -i <interface>

same but writing capture in binary format


tcpdump -i <interface> -w </path/to/file>

same with no name resolution


tcpdump -ni <interface> -w </path/to/file>

same while filtering on a specific host


tcpdump -ni <interface> host <ipaddress> -w </path/to/file>

same while filtering on a specific host and port


tcpdump -ni <interface> host <ipaddress> and port <port> -w </path/to/file>

Limiting the capture


limit the packet size (0 for whole packet)
tcpdump -ni <interface> -s<0> host <ipaddress> and port <port> -w </path/to/file>

limit the number of packets captured (tcpdump stops when limit is reached)
tcpdump -ni <interface> -s<0> -c<NbOfPackets>

limit the size of the output file (Size in MB, new output file created when limit reached, naming scheme is original filename
completed with the file number starting at 1)
tcpdump -ni <interface> -s<0> -C<SizeInMB> -w </path/to/file>

Networks
filter on a network range
tcpdump -ni <interface> -s<0> net <networkIP> mask <netmask>
LDAPSEARCH one-liners:

Basics
simple search without bind
ldapsearch -h <host> -b "<dc=example,dc=com>"

simple search without bind to a custom port


ldapsearch -h <host> -p <port> -b "<dc=example,dc=com>"

simple search and display result in ldif format (-L = ldifv1, -LL = no comments, -LLL = no version info)
ldapsearch -h <host> -b "<dc=example,dc=com>" -LLL

simple search while specifying the protocol version


ldapsearch -h <host> -P <2|3> -b "<dc=example,dc=com>"

Authentication and bind


search with simple authentication
ldapsearch -h <host> -b "<dc=example,dc=com>" -x

search with a bind


ldapsearch -h <host> -b "<dc=example,dc=com>" -x -D "<cn=admin,cn=users,dc=example,dc=com>" -w "<password>"

Filters and Logical operators


write a filter
ldapsearch -h <host> -b "<dc=example,dc=com>" "(<att1>=<val1>)"

AND filter ( & operator)


ldapsearch -h <host> -b "<dc=example,dc=com>" "(&(<att1>=<val1>)(<ANDatt2>=<val2>))"

OR filter ( | operator)
ldapsearch -h <host> -b "<dc=example,dc=com>" "(|(<att1>=<val1>)(<ORatt2>=<val2>))"

NOT filter ( ! operator)


ldapsearch -h <host> -b "<dc=example,dc=com>" "(&(<att1>=<val1>)(!<andNOTatt2>=<val2>))"
nc (netcat) one-liners:

Basics
listen on a port
nc -lp <portNumber>

listen on a port and disable DNS lookups


nc -lnp <portNumber>

listen verbosely on a port and disable DNS lookups


nc -lvnp <portNumber>

connect to a host on a particular port


nc <hostIP> <portNumber>

associate a process with a socket


nc -lvnp <portNumber> -e </path/to/process>

Setting up a chat
open a socket on the first machine
nc -lvnp <portNumber>

connect to this socket from the second machine


nc <hostIP> <portNumber>

File transfers
download a file from a server: 1st step: output a file to a socket
cat </path/to/file> | nc -lp <PortNumber>

download a file from a server: 2nd step a: connect to the socket and redirect the output to a file
nc <hostIP> <portNumber> > </path/to/downloaded/file>

download a file from a server: 2nd step b: show file transfer progress
nc <hostIP> <portNumber> | pv -b > </path/to/downloaded/file>

upload a file to a server: 1st step: output the socket to a file


nc -lp <PortNumber> > </path/to/uploaded/file>

upload a file to a server: 2nd step a: send file content to the socket
nc <hostIP> <portNumber> < </path/to/file>
upload a file to a server: 2nd step b: show file transfer progress
nc -lp <PortNumber> | pv -b > </path/to/uploaded/file>

Port scanning
scan UDP ports
nc -vnzu <hostIp> <startPort>-<endPort>

scan TCP ports (timeout in seconds)


nc -vnz -w <timeOut> <hostIp> <startPort>-<endPort>

Telnet server
associate bash with the socket on the linux machine
nc -lp <portNumber> -e </bin/bash>

associate cmd with the socket on the windows machine


nc -lp <portNumber> -e cmd.exe

then connect to the socket as usual


nc <hostIP> <portNumber>

Proxy
1st step: create a bi-directional pipe
mkfifo <pipeName>

2nd step: create a proxy with nc


nc -lvnp <portNumber> 0<<pipeName> | nc <newHost> <newPort> 1><pipeName>

Web server
create a simple webserver to serve the same page
while true; do nc -lp 80 -q 1 < </path/to/html>; done

File systems
backup files to a remote machine
tar -czvf - </path/to/dir> | nc <hostIp> <portNumber>
nc -lp <portNumber> | pv -b > <backup.tgz>

copy partitions to a remote machine


dd if=/dev/<deviceId> | nc <hostIp> <portNumber>
nc -lvnp <portNumber> | dd of=/dev/<deviceId>

snmpget & snmpwalk one-liners:

Basics
query a particular OID
snmpget -v <1|2c|3> -c <community> <host> <OID>

walk over MIB and retrieve subtree from a specified OID


snmpwalk -v <1|2c|3> -c <community> <host> <fromOID>

print the number of variables found


snmpwalk -v <1|2c|3> -c <community> <host> <fromOID> -Cp

print the total time it took to collect the data (it does not include snmp library initialization, shutdown, argument processing,
and any other overhead)
snmpwalk -v <1|2c|3> -c <community> <host> <fromOID> -Ct
openssl one-liners:

Read a certificate
see/read certificate and output in clear text:
openssl x509 -text -noout -in <mycert>

RSA key
create a key
openssl genrsa -out <mykey> <keySize>

create a key encrypted with passphrase


openssl genrsa -<des|des3|idea|aes128|aes192> -out <mykey> <keySize>

extract public portion of the key


openssl rsa -in <mykey> -pubout

Certificate Signing Request


create a csr (certificate signing request) using an existing key:
openssl req -new -key <mykey> -out <mycsr>

create a csr (certificate signing request) and a key altogether:


openssl req -new -newkey rsa:<keySize> -keyout <mykey> -out <mycsr>

same while providing subject line:


openssl req -new -newkey rsa:<keySize> -keyout <mykey> -out <mycsr> -subj
"</C=XX/ST=mystate/L=mytown/O=myorganisation/OU=myou/CN=myname/emailAddress=myemail>"

create a csr (certificate signing request) from a certificate


openssl x509 -x509toreq -in <mycert> -out <mycsr> -signkey <mykey>

Self-signed certificate
create a self signed certificate:
openssl req -x509 -new -out <mycert> -keyout <mykey> -days <numberOfDays>

same with key size


openssl req -x509 -new -out <mycert> -newkey rsa:<keysize> -keyout <mykey> -days <numberOfDays>
create a self signed certificate while providing subject line:
openssl req -x509 -new -out <mycert> -keyout <mykey> -days <numberOfDays> -subj
"</C=XX/ST=mystate/L=mytown/O=myorganisation/OU=myou/CN=myname/emailAddress=myemail>"

Signing a certificate
sign a csr (certificate signing request) with a certificate/key (can use a self signed certificate to emulate a CA). Validity must
be less than signer
openssl x509 -req -in <mycsr> -CA <signingCert> -CAkey <signingKey> -CAcreateserial -out <mycert> -days
<numberOfDays>

Converting/Exporting
convert/export certificate and key to pkcs12 (.pfx, .p12) format
openssl pkcs12 -export -in <mycert> -inkey <mykey> -out <mypkcs12.p12> -name "<friendlyName>"

convert/export certificate from PEM to DER format


openssl x509 -inform PEM -outform DER -in <myPEMcert> -out <myDERcert>

convert a certificate from DER to PEM:


openssl x509 in <myDERcert> inform DER out <myPEMcert> outform PEM

convert a key from PEM to DER:


openssl rsa in <myPEMkey> inform PEM out <myDERkey> outform DER

convert a key from DER to PEM:


openssl rsa in <myDERkey> inform DER out <myPEMkey> outform PEM

Info
show openssl version
openssl version -a

list/show/display all available ciphers


openssl ciphers -v

list/show/display ciphers for a particular ssl version


openssl ciphers -v -<ssl2|ssl3|tls1>

list/show/display ciphers for a particular security grade


openssl ciphers -v "<LOW|MEDIUM|HIGH>"

list/show/display ciphers for a particular algorithm


openssl ciphers -v "<DES|AES|RSA...>"

Verify
verify that a key and a certificate match (output of both commands must match)
openssl x509 -noout -modulus -in <mycert> | openssl md5
openssl rsa -noout -modulus -in <mykey> | openssl md5

verify a certificate against its intermediate chain


openssl verify -CAfile <intermediateCert> <mycert>

Cisco ios one-liners:


for calculating ACL wildcard masks use the subnet calculator at:
www.readytoolbox.com/subnet-calculator

System
set/change the hostname
hostname <hostname>

set/change the enable password


enable password <mypwd>

set/change the enable secret password (encrypted password that replaces the enable password)
enable secret <mysecretpwd>

set/change the DNS server


ip name-server <ipaddress>

start the system verification process


show system verify start

show the system verification process report


show system verify report

Configuration
show the running/current/active configuration
show running-config

show the startup/saved configuration


show startup-config

save/copy the running/current/active configuration as startup configuration


copy running-config startup-config

load/copy the startup configuration as running/current/active configuration


copy startup-config running-config

save/copy/backup the running configuration to a tftp server


copy runing-config tftp

load/import the configuration from a tftp server


copy tftp runing-config

save/backup the IOS to a tftp server


copy flash tftp

import/load the ISO from a tfp server


copy tftp flash

see why the configuration failed to load (use in EXEC mode)


show configuration failed

Interfaces
show/list all the interfaces
show interface brief

show the configuration of a particular interface


show interface <intID>

select one interface


interface <intID>

disable/shutdown an interface
shutdown

enable/bring up an interface
no shutdown

add/configure an IP address to/for an interface


ip address <ipaddress> <netmask>

Static and default routes


show/list all the static routes
show ip route static

set/add a static route to a next hope/gateway


ip route <ipaddress> <netmask> <gatewayIP>

set/add a static route to an interface


ip route <ipaddress> <netmask> <interfaceID>

set/add the default route


ip route 0.0.0.0 0.0.0.0 <gateway|interface>

remove/delete a static route (<command> is the command you used in the first place to add the route)
no <command>

RIP
enable/enter RIP mode
router rip

set/change the RIP version


version <1|2>

add a network to the RIP configuration


network <networkIP>

set/define a neighbor rip router


neighbor <ipaddress>

disable RIP
no router rip

Access Control lists (ACLs)


allow some ip traffic
access-list <aclId> permit ip <sourceIP> <sourceWildcard> <destIP> <destWildcard>

deny/block some ip traffic


access-list <aclId> deny ip <sourceIP> <sourceWildcard> <destIP> <destWildcard>

allow icmp traffic from/to particular hosts


access-list <aclId> permit icmp host <sourceIP> host <destIP>

allow some udp/tcp traffic


access-list <aclId> permit <udp|tcp> host <sourceIP> host <destIP> eq <port>

allow some web/http traffic


access-list <aclId> permit tcp host <sourceIP> host <destIP> eq www

allow some dns traffic


access-list <aclId> permit udp host <sourceIP> host <destIP> eq domain

deny/block all traffic (to add at the end of the ACL)


access-list <aclId> deny ip any any

delete/remove an ACL entry (<command> is the command you used in the first place to add the entry)
no <command>

Capturing traffic (new in 12.4)


1st) create a capture buffer (where/which the packets will be stored)
monitor capture buffer <captureBufferName> size <bufferSizeKB> max-size <bufferSizeKB> circular

1st) or create a capture buffer and export the capture to pcap format
monitor capture buffer <captureBufferName> export tftp://<ipAddress>/<path/to/file>

1st) you can even use an ACL as a capture filter


monitor capture buffer <captureBufferName> filter access-list <aclID>

2nd) create a capture point to capture on a specific interface


capture point ip cef <capturePointName> <interfaceID> both

2nd) or capture on all interface


capture point ip cef <capturePointName> all both

3rd) associate/link a capture point with a capture buffer


capture point associate <capturePointName> <captureBufferName>

4th) start the capture/trace


capture point start <capturePointName>

5th) stop a specific packet capture


monitor capture point stop <capturePointName>

5th) stop all packet captures


monitor capture point stop all
ssldump one-liners:

Basics
capture
ssldump -i <interface> host <ipAddress> and port <portNumber> > <outputFile>

decrypt
ssldump -r <inputFile> -k <keyFile> -d > <outputFile>

capture and decrypt at the same time


ssldump -Ad -k <keyFile> -p <keyPassphrase> -i <interface> host <ipAddress> and port <portNumber> > <outputFile>

SQL one-liners:

Select (SELECT, DISTINCT, FIRST, LAST, LIMIT,


WHERE)
select all information/columns from a table
SELECT * FROM 'table';

select only 1 column from a table


SELECT 'column' FROM 'table';

select only unique values


SELECT DISTINCT 'column' FROM 'table';

select only the first entry


SELECT FIRST('column') FROM 'table';

select on the first n entries


SELECT 'column' FROM 'table' LIMIT 'n';

select on the last entry


SELECT LAST('column') FROM 'table';

select only if column matches a condition


SELECT 'column1' FROM 'table' WHERE 'column2' = 'value';

Inserting data into a table (INSERT INTO)


insert data into a table
INSERT INTO 'table' ('column1','column2') VALUES ('value1','value2');

insert/copy data from 1 table to another


INSERT INTO 'table1' ('column1','column2') SELECT 'column3','column4' FROM
'table2';

Updating a table (UPDATE, REPLACE)


update all entries in the table
UPDATE 'table' SET 'column' = 'value';

update only entries that match a condition


UPDATE 'table' SET 'column' = 'value1' WHERE 'column' = 'value2';

update/replace only the value of one particular column


SELECT REPLACE('column','replaceThis','withThis') FROM 'table';

Deleting data from a table (DELETE FROM)


delete/remove all entries from a table
DELETE FROM 'table';

delete/remove only entries that match a condition


DELETE FROM 'table' WHERE 'column' = 'value';

Conmparing values (WHERE, IN, BETWEEN, =, ', !=,


>,', '=, <=)
select only where column equals a value
SELECT * FROM 'table' WHERE 'column' = 'value';

select only where column is greater than value


SELECT * FROM 'table' WHERE 'column' > 'value';

select only where column is lower than value


SELECT * FROM 'table' WHERE 'column' ' <VALUE';< pre>

select only where column matches one of the listed values


SELECT * FROM 'table' WHERE 'column' IN ('value1','value2','value3');

select only where column between 2 values


SELECT * FROM 'table' WHERE 'column' BETWEEN ('value1','value2');SELECT * FROM
'table' WHERE 'column2' > 'value2' AND 'column2' ' <VALUE3';< pre>
Logical Operators (AND, OR)
AND logical operator
SELECT * FROM 'table' WHERE 'column1' = 'value1' AND 'column2' = 'value2';

OR logical operator
SELECT * FROM 'table' WHERE 'column1' = 'value1' OR 'column1' = 'value2';

combining multiple logical operators with brackets


SELECT * FROM 'table' WHERE 'column1' = 'value1' OR ('column2' > 'value2' AND
'column2' ' <VALUE3');< pre>

Working with dates (BETWEEN, SYSDATE,


GETDATE, DATEDIFF)
select only if column between 2 dates
SELECT * FROM 'table' WHERE 'datecolumn' BETWEEN 'Jan-01-2010' AND 'Jan-25-2010';

retrieve/get the system date with MySQL or Oracle


SELECT SYSDATE();

retrieve/get the system date with SQL server


SELECT GETDATE();

calculate the difference between 2 dates with MySQL


SELECT DATEDIFF('Jan-01-2010','Jan-25-2010');

Working with wildcards (LIKE, %, _)


select where string starts with abc (% for 0 or more characters)
SELECT * FROM 'table' WHERE 'column' LIKE 'abc%';

select where string ends with abc (% for 0 or more characters)


SELECT * FROM 'table' WHERE 'column' LIKE '%abc';

select where string stars with ab and is 3 character long (_ for 1 character only)
SELECT * FROM 'table' WHERE 'column' LIKE 'ab_';

Ordering the results (ORDER BY, ASC, DESC)


show results in ascending order
SELECT * FROM 'table' ORDER BY 'column' ASC;

show results in descending order


SELECT * FROM 'table' ORDER BY 'column' DESC;

show results with multiple order by


SELECT * FROM 'table' ORDER BY 'column1' ASC, 'column2' DESC;

Mathematical functions (COUNT, MIN, MAX, SUM,


AVG)
count the number of entries
SELECT COUNT('column') FROM 'table';

count the number of unique entries


SELECT COUNT(DISTINCT 'column') FROM 'table';

select the smallest/lowest/minimum/min value


SELECT MIN('column') FROM 'table';

select the biggest/highest/maximum/max value


SELECT MAX('column') FROM 'table';

calculate the average value


SELECT AVG('column') FROM 'table';

calculate the sum of all the values


SELECT SUM('column') FROM 'table';

Use mathematical functions as conditions


(HAVING)
using methematical functions as a condition (let's say only people older than the average)
SELECT 'name','age' FROM 'people' HAVING 'age' > AVG('age');

Grouping (GROUP BY)


calculate the average age and group by gender (let's say the average age for males and females)
SELECT 'gender', AVG('age') FROM 'people' GROUP BY 'gender';

Aliases (AS)
create an alias name for a column
SELECT 'column' AS 'newcolumnName' FROM 'table';

create an alias name for a table


SELECT 'column' FROM 'table' as 'newTableName';

Joining tables (JOIN)


join 2 tables
SELECT 'table1'.'column1', 'table2'.'column1' FROM 'table1','table2'WHERE
'table1'.'column3' = 'table2'.'column3';

an example (show customers that have subscribed to both memberships)


SELECT 'membership1'.'fname','membership2'.'lname'FROM
'membership1','membership2'WHERE 'membership1'.'customerID' =
'membership2'.'customerID';

Creating, deleting, and altering tables (CREATE


TABLE, DROP TABLE, ALTER TABLE, CHANGE,
DROP, RENAME)
create a table
CREATE TABLE 'people'('fname' char(25),'lname' char(25),'age'
integer(3),'dateOfBirth' date);

remove/delete/drop a table
DROP TABLE 'table';

alter/modify a table and add a column


ALTER TABLE 'table' ADD 'column' 'type';

alter/modify a table and add multiple columns


ALTER TABLE 'table' ADD ('column1' 'type','column2' 'type');

alter/modify a table and change the column data type with MySQL and Oracle
ALTER TABLE 'table' ADD 'column' 'newType';

alter/modify a table and change the column data type with SQL Server
ALTER TABLE 'table' ALTER COLUMN 'column' 'newType';

alter/modify a table and rename a column with MySQL


ALTER table 'table' CHANGE 'column' 'newName' 'type';

alter/modify a table and rename a column with Oracle


ALTER TABLE 'table' RENAME 'column' 'newName';

alter/modify a table and drop/remove/delete a column with MySQL


ALTER TABLE 'table' DROP 'column';

alter/modify a table and drop/remove/delete a column with Oracle or SQL server


ALTER TABLE 'table' DROP COLUMN 'column';

mysql & mysqladmin & mysqldump one-liners:

Connecting
mysql to local host
mysql -u<username> -p

mysql to a specific host


mysql -h <hostname> -P <port> -u<username> -p

Security
clear/delete mysql history
rm ~/.mysql_history

Databases
list all the databases
show databases;

create a database
create database <database>;

connect to a database
connect <database>;

delete/remove a database
drop database <database>;

Tables
list all the tables
show tables;

see the table structure


describe <table>;
delete/remove a table
drop table <table>;

Users
Create a new user with mysql
mysql -u root -p
use mysql;
INSERT INTO user (Host,User,Password) VALUES('%','<username>',PASSWORD('<password>'));
flush privileges;

change user password with mysql


mysql -u root -p
SET PASSWORD FOR '<username>'@'<hostname>' = PASSWORD('<newPwd>');
flush privileges;

change user password with mysqladmin


mysqladmin -u <username> -h <hostname> -P <port> -p password <newPwd>

grant user all privileges for a database


mysql -u root -p
use mysql;
grant all privileges on <database>.* to <username>@<hostname>;
flush privileges;

grant user all privileges for a specific table


grant all privileges on <database>.<table> to <username>@<hostname>;

Root password change / recovery


set root password if not set with mysqladmin
mysqladmin -u root password <newPwd>

change root password with mysqladmin


mysqladmin -u root -p <oldPwd> <newPwd>

recover/change root password with dpkg (ubuntu)


dpkg-reconfigure mysql-server-5.0

recover root password with mysql


/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &
mysql -u root
use mysql;
update user set password=PASSWORD("<newPwd>") where User='root';
flush privileges;
quit
/etc/init.d/mysql restart

Backup / restore
save/backup all the databases
mysqldump -user <username> -password=<password> --all-databases > </path/to/file>

save/backup a database
mysqldump -user <username> -password=<password> <database> > </path/to/file>

save/backup a database with compression


mysqldump -user <username> -password=<password> -C <database> > </path/to/file>

save/backup tables with compression


mysqldump -user <username> -password=<password> -C <database> <table1> <table2> > </path/to/file>

restore/import a backup (with mysql)


mysql -u<username> -p<password> <database> < </path/to/file>

copy database from local to remote mysql server


mysqldump --opt <database> | mysql --host=<removeServer> -C <database>

Ruby examples:
Variables
create a local varaiable
localvar = 1 # integer

localvar = "string" # string


lobalvar = false # boolean, can take true

create a global variable (name begins with $)


$globalvar = "string"

create a constant (name begins with an uppercase character)


$CONSTANT = 1

multiple variables assignment


a, b, c = 1, 2, 3 #a=1 b=2 c=3

increment variables
x += 1 # increment by 1

decrement variables
x -= 1 # decrement by 1

concatenate variables and strings


myvar = 1

puts "some text " + myvar # error: can't convert integer to string. convert
myvar with to_s() first

puts "some text #{myvar}" # some text 1

myvar2 = "abc"

puts "some text " + myvar2 # some text abc

Pre-defined variables
regular expressions variables
$& # string that matches the whole pattern

$` # string outside the pattern to the left

$' # string outside the pattern to the right

$1 # string that maches the 1st group

$9 # string that matches the 9th group (limit)


$+ # string that matches the last group

script name
$0 # script name

arguments
ARGV[0] # 1st argument

ARGV[2] # 2nd argument

ARGV.each do|arg| # process all arguments

# do something on arg

end

Numbers
convert an integer to a string
int = 12

puts "some text" + int.to_s() # "some text12"

convert a string to an integer


Integer(123) # 123

convert an integer to a float


Float(5) # 5.0

convert a string to a float


Float("1.234") # 1.234

convert a binary number to decimal (string starts with "0b")


Integer("0b10000000") # 128

convert an octal number to decimal (string starts with "0") Integer("01") # 8

convert an hexadecimal number to decimal


"0A".hex # 10

convert a decimal to an hexadecimal number


mynb = 10

mynb.to_s(16) # "a"

255.to_s(16) # "ff"

round up a float to an integer


fl = 0.6

puts fl.ceil # 1

puts 0.4.ceil # 1

Strings
upper case a string
myvar = "hello"

myvar.upcase # "HELLO", myvar still equals "hello"

myvar.upcase! # "HELLO", myvar modified to "HELLO"

lower case a string


myvar = "HELLO"

myvar.downcase # "hello", myvar still equals "HELLO"

myvar.downcase! # "hello", myvar modified to "hello"

capitalize a string (first character of each word in upper case)


myvar = "hello"

myvar.capitalize # "Hello", myvar still equals "hello"

myvar.capitalize! # "Hello", myvar modified to "Hello"

compare 2 strings (case sensitive)


str1 <=> str2

"ab" <=> "ab" # 0 if str1 equals str2

"abc" <=> "ab" # 1 if str1 longer than str2


"ab" <=> "abc" # -1 if str2 longer than str1

compare 2 strings (case insensitive)


str1.casecmp(str2)

"ab".casecmp("ab") # 0 if str1 equals str2

"ab".casecmp("AB") # 0 if str1 equals str2

"abc".casecmp("ab") # 1 if str1 longer than str2

"ab".casecmp("abc") # -1 if str2 longer than str1

replace substring within a string (1st occurence only)


str = "abc abc"

str.sub("abc","---") # "--- abc"

replace substring within a string (all occurennces)


str = "abc abc"

str.gsub("abc","---") # "--- ---"

remove trailing line breaks/new lines characters (\r, \n, \r\n, \n\r)
"hello\n".chomp # "hello"

"hello\r".chomp # "hello"

"hello\r\n".chomp # "hello"

remove trailing white spaces


"hello ".rstrip # "hello"

remove leaving white spaces


" hello".lstrip # "hello"

remove leading, trailing whitespaces, tabulations, new lines and lines breaks
" hello ".strip # "hello"

" hello \r\n".strip # "hello"

check whether a string is empty


"abc".empty? # false
"".empty? # true

check whether a string includes a substring


"hello".include?("he") # true

"hello".include?("ab") # false

insert a substring inside a string


"hello".insert(0, '-') # 0 to insert at the begeinning

"hello".insert(1, '-') # 1 to insert after 1st char

"hello".insert(-1, '-') # -1 to insert at the end

"hello".insert(-2, '-') # -2 to insert before last char

count the number of characters in a string


"abc".length # 3

count the number of words in a string


"abc def".count(" ")+1 # 2

devide a string into an array


"a,b,,d".split(',') # ["a", "b", "", "d" ]

"abc".split(//) # ["a", "b", "c" ]

center a string by adding padding whitespaces to left and right


"ab".center(10) # " ab "

justify a string to the left by adding padding whitespaces to the right


"ab".ljust(10) # "ab "

justify a string to the right by adding padding whitespaces to the left


"ab".rjust(10) # " ab"

perform operations on each character of a string


str = "abc"

str.split(//).each { |char|
puts char # first "a", then "b", then "c"

perform operations on each line of a string


str = "abc\ndef"

str.each_line() { |line|

puts line # first "abc" then "def"

Text files
open a file in read mode
myfile = File.new("/path/to/file","r")

open a file in write mode


myfile = File.new("/path/to/file","w")

open a file in append mode


myfile = File.new("/path/to/file","a")

close a file
myfile.close

read from a file (just one line)


line = myfile.gets

read the entire file content


content = File.read("/path/to/file")

write to a file
myfile.puts "some text"

read a file line by line (entire file)


myfile.each { |line|

puts line.chomp # remove trailing line breaks/new lines


}

read a file line by line until a condition is met


while (line = myfile.gets and condition)

# do something

end

read a file line by line until string is found


begin

line = myfile.gets

end while !myfile.eof? and !line.include?("what you're looking for")

Ruby regular expression cheatsheat


characters patterns
[abc] # either a, b, or c

[^abc] # any character but a, b or c

[a-z] # any lower case character

[A-Z] # any upper case character

[a-z][A-Z] # any alphabetical character

[1-9] # any digit

[a-z][A-Z][1-9] # any alphanumerical character

. # any character

\d # any digit

\D # any non-digit

\w # any word character (letters, numbers, underscores)

\W # any non-word character

\b # any word boundary character


\s # any whitespace character

\S # any non-whitespace character

position patterns
^ # start of line

$ # end of line

\A # start of string

\z # end of string

quantifiers
? # zero or one

* # zero or more

+ # one or more

{x} # exactly x, where x is integer

{x,} # x or more, where x is integer

{x,y } # between x and y, inclusive, with x and y integers

Logical operatros
(red|black) # matches either "red" or "black"

options
/pattern/i # case insensitive

/pattern/x # ignore white spaces

/pattern/m # . now designates newlines instead of any character

create groups
( ) # whatever is in brackets will be captured. see predefined
variables to reuse captured data

regular expressions variables (available once regular expression processing has been performed)
$& # string that matches the whole pattern

$` # string outside the pattern to the left

$' # string outside the pattern to the right

$1 # string that maches the 1st group

$9 # string that matches the 9th group (limit)

$+ # string that matches the last group

Using regular expressions


perform regular expression match
"abc 123".match(/[a-z]/) # "abc"

"abc 123".match(/(.*) (.*)/)[1] # "abc"

"abc 123".match(/(.*) (.*)/)[2] # "123"

"abc 123".match(/(.*) (.*)/).each { |match|

# do something

perform a task on each occurence of pattern in a string


str = "http://url1 whatever http://url2"

while (str.slice!(/(http[^\s]*)/))

# for isntance query each url using $1

end

replace a substring within a string (1st occurence only)


str = "abc abc ABC"

str.sub(/[a-z]/,"---") # "--- abc ABC"

replace substrings within a string (all occurennces)


str = "abc abc ABC"
str.gsub(/[a-z]/,"---") # "--- --- ABC"

split a string into an array


"abc".split(//) # ["a", "b", "c" ]

str = "Mike John+Ben-Aaron"

str.split(/[\s\+-]/) # ["Mike", "John", "Ben", "Aaron" ]

Arrays
Creating an array
create an empty array
myarray = Array.new

specify the size of an array


myarray = Array.new(2)

create an array with predefined values


myarray = [ "red", "black", "yellow"]

Adding elements
add/append an element to the end of an array
myarray << "blue"

add/prepend elements to beginning of the array


myarray = [ 2, 3]

myarray.unshift(1) # [1,2,3]

Removing elements
remove an element from an array
myarray = [ "a", "b", "c", "d" ]

myarray.slice!(2) # returns "c"


myarray # [ "a", "b", "d" ]

remove duplicate elements in array (unique)


myarray = [1 ,2, 3, 1, 4]

myarray.uniq # [ 1,2,3,4]

Accessing elements
access/get specific elements of an array
myarray = [ "a", "b", "c", "d" ]

myarray[0] # 1st element: "a"

myarray[0,2] # 1st and 2nd elements: [ "a" ,"b" ]

myarray[0..2] # 1st to 3rd elements: [ "a" ,"b", "c" ]

myarray[-2,2] # last 2 elements [ "c", "d" ]

access/get the last element of an array


[ 1, 2, 3 ].last # 3

access/get the first element of an array


[ 1, 2, 3 ].first # 1

Multidimensional arrays
create a multidimensional array
myarray = Array.new(5, Array.new(5)) # 5x5 array

alternatively you can just add array elements into your array
myarray[0] << [ "a" , "b"]

you can also combine arrays and hashes


myarray = Array.new(2, Hash.new)

myarray[0]['color'] = 'red'

myarray[1]['color'] = 'blue'
accessing a multidimensional array.
myarray[0][3] # 1st colomn, 4th row

Working with multiple arrays


compare 2 arrays to see whether they're equal
[ 1, 2] == [1,3] # false

[ 1, 2] == [1,2] # true

Concatenation (return values from both arrays)


[ 1, 2] + [ 3,4 ] # [1,2,3,4]

Intersection (return values that are in both arrays)


myarray1 = [ "red", "black"]

myarray2 = [ "black", "yellow"]

myarray1 & myarray2 # [ "black" ]

Repetition
myarray = [ 1,2 ]

myarray * 2 # [ 1,2,1,2]

Difference
myarray1 = [ 1,2,3 ]

myarray2 = [ 1,2 ]

myarray1 - myarray2 = [ 3 ]

Sorting/Reversing arrays
sort array elements in ascending order
myarray = [ "b", "a", "c" ]

myarray.sort # [ "a", "b", "c" ]

sort array elements in descending order


myarray = [ "b", "a", "c" ]

myarray.sort {|x,y| y <=> x } # [ "c", "b", "a" ]

reverse an array
myarray = [ "b", "a", "c" ]

myarray.reverse # [ "c", "a", "b" ]

Searching elements
search/check if element exists in array
[ 1, 2, 3 ].include?(3) # true

[ 1, 2, 3 ].include?(4) # false

Array length
get the size/number of elements of an array
[ 1, 2, 3 ].length # 3

Iterations
get each element of an array
[ "a", "b", "c" ].each do |element|

puts element # "a", then "b", then "c"

end

get each element of an array from the end


[ "a", "b", "c" ].reverse_each do |element|

puts element # "c", then "b", then "a"

end

get each element of an array with its index


[ "a", "b", "c" ].each_with_index do |el, i|
puts "#{i}: #{el}" # "0: a", then "1: b", then "2: c"

end

get each element of an array with its index from the end
[ "a", "b", "c" ].reverse.each_with_index do |el, i|

puts "#{i}: #{el}" # "0: c", then "1: b", then "2: a"

end

Misc
extract all elements of an array and convert them into one single string
["Mike", "John", "Ben" ].join # "MikeJohnBen"

["Mike", "John", "Ben" ].join(' ') # "Mike John Ben"

Loops
if loop
if condition

# do something

elseif anothercondition

# do something

else

# do something

end

while loop
while condition

# do something

end
case statement
case myvar

when 1,3,5

puts "this is odd"

when 2,4,6

puts "this is even"

else

puts "it's something else

end

do until loop
begin

# do something

end while !condition

for loop (example 1)


for i in (1..10)

# do something

end

for loop (example 2)


(1..10).each do |i|

# do something

end

for loop (allows you decrementing, incrementing by other than 1)


i = 0

10.times do

i += 2 # increment by 2
# do something

end

Files
create a file
File.new("filename","r") # read mode

File.new("filename","a") # append mode

File.new("filename","w") # write mode

check whether the file exist


File.exist?("/path/to/file") # true or false

check whether object is a file


File.file?("/path/to/object") # true or false

extract/get the extension of a file


File.extname("img.jpg") # ".jpg"

extract/get the base name of a file


filename = "/path/to/file.ext"

File.basename(filename, File.extname(filename)) #file

split the directory and file names into an array


File.split("/path/to/file") # [ "/path/to" , "file" ]

rename a file
File.rename("oldname","newname")

get the file size (in bytes)


File.size("filename")

check whether the file is empty


File.zero?("filename") # true or false

compare 2 files (returns true if content of both files is identical)


require 'ftools'

File.compare("file1","file2") # true or false

copy a file
require 'FileUtils'

FileUtils.copy("source","copy")

move a file
require 'FileUtils'

FileUtils.copy("source","copy")

remove/delete a file
File.delete("filename")

File.unlink("filename") # equivalent

remove/delete multiple files


File.delete("file1", "file2")

File.unlink("file1", "file2") # equivalent

Directories
create a directory
require 'ftools'

File.makedirs("dirname")

remove a directory
require 'fileutils'

FileUtils.rm_rf("dirname")

for each file in directory


Dir.new("/path/to/dir").each do |file|

if file != "." and file != ".." # exclude "." and ".."

puts file # prints filenames


end

end

for each file in current directory


Dir.new(".").each do |file|

if file != "." and file != ".." # exclude "." and ".."

puts file # prints filenames

end

end

check whether object is a directory


File.directory?("/path/to/object") # true or false

extract/get directory name from filename


File.directory?("/path/to/file") # "/path/to"

convert relative paths to absolute paths


File.expand_path(".") # full path to current directory

File.expand_path("../") # full path to parent directory

Statistics
to optimize performances first create a stat object then access stats information as follows
stats = File.stat("filename")

stats.atime

although you can get the stats directly from the file
File.atime("filename")

get the last access time


File.atime("filename")

get the last modified time


File.mtime("filename")
get the creation time
File.ctime("filename")

update/set the access and modification time


File.utime(atime, mtime, "filename")

Symbolic links
check whether file is a symbolic link
File.symlink?("filename")

create a symbolic link


File.symlink("source","link")

Permissions
set/change the file permissions
File.chmod(0777, "file1", "file2")

set/change the group and owner of a file (gid and uid as integers)
File.chown(gid,uid,"filename")

check whether file is readable


File.readable?("filename")

check whether file is executable


File.executable?("filename")

check whether file is writable


File.writable?("filename")

iptables one-liners:

Set the default policies


by default allow only outgoing traffic
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

Flush all the rules


so only default policy settings apply so far
iptables -F INPUT
iptables -F FORWARD
iptables -F OUTPUT
iptables -F -t nat
iptables -F -t filter

Filtering at layer 3
filter on specific source/destination IPs on all interfaces
iptables -A <INPUT|FORWARD|OUTPUT> -s <sourceIP>/<mask> -d <destIp>/<mask> -j <DROP|ACCEPT|REJECT>

# filter on specific source/destination IPs on a specific interface


iptables -A <INPUT|FORWARD|OUTPUT> -i <interface> -s <sourceIP>/<mask> -d <destIp>/<mask> -j <DROP|ACCEPT|
REJECT>

IP forwarding
first enable IP forwarding on the linux host, then write the rules as appropriate
sysctl -w net.ipv4.ip_forward=1
iptables -A FORWARD -i <sourceInt> -o <destInt> -j ACCEPT

NATing
use DNAT to change the destination IP
iptables -t nat -A PREROUTING -d <destIp> -j DNAT --to-destination <newDestIp>

SNATing / Masquerading
replace/change the source IP leaving out on specific outbound interface and specify the new source ip
iptables -t nat -A POSTROUTING -t nat -s <sourceIP>/<mask> -o <outboundInt> -j SNAT --to-source <newSourceIP>

replace/change the source IP leaving out on specific outbound interface and use this interface's IP as a new source
(masquerade)
iptables -t nat -A POSTROUTING -t nat -s <sourceIP>/<mask> -o <outboundInt> -j MASQUERADE

Filtering at layer 4
accept traffic to a particular TCP service
iptables -A INPUT -p tcp -d <serviceIp>/32 --destination-port <servicePort> -j ACCEPT

accept traffic to a particular UDP service


iptables -A INPUT -p udp -d <serviceIp>/32 --destination-port <servicePort> -j ACCEPT

accept what is already part of an existing connection (works with "iptables -P OUTPUT ACCEPT")
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

Limiting the number of connections


limit the number of connections per client host (<limit> being the max number of connections)
iptables -p tcp --syn --dport <servicePort> -m connlimit --connlimit-above <limit> -j REJECT

limit the number of connections per network/subnet


iptables -p tcp --syn --dport <servicePort> -m connlimit --connlimit-above <limit> --connlimit-mask <networkMask> -j
REJECT

awk one-liners:

Basics
use string as separator
awk -F"<string>" '{ print $<fieldId> }' <file>

use regular expression as separator


awk '/<regexp>/ {print $<fieldId>}' <file>

use spaces (white spaces or tabs) as sperator


awk '/[ \t]*/ {print $<fieldId>}' <file>
use tabs to format outputs
awk -F" " '{ print $1 "\t\t" $2}' <file>

Counting
count the number of lines (not number of occurences) that contain a specific pattern (+0 used if n = 0)
awk '/<pattern>/{n++}; END {print n+0}' <file>

count the number of occurences of a string


awk '/<pattern>/ { count["<pattern>"]++ }' <file>

count the number of fields per line and display result as "Nb:Line"
awk '{ print NF ":" $0 }' <file>

Selective Printing
print only lines that match a specific pattern
awk '/<regexp>/ { print }' <file>

print only lines that do NOT match a specific pattern


awk '!/<regexp>/ { print }' <file>

print only lines that are longer than ...


awk 'length($0) > <NbOfCharacters>' <file>

Field manipulation
remove/delete a particular field
awk '{$<fieldId> = ""; print}' <file>

print each field on seperate line


awk '{ for (i = 1; i <= NF; i++) print $i }' <file>

sed one-liners:
test the below sed one-liners first. Once you're happy with the result, use -i to modify the input file or > to
write to another file

Basic examples
replace/substitute only the first occurence of each line
sed 's/<replaceThis>/<withThis>/' <file>

replace/substitute the N first occurences of each line


sed 's/<replaceThis>/<withThis>/<N>' <file>

replace/substitute all occurrences


sed 's/<replaceThis>/<withThis>/g' <file>

replace/substitute only if the line contains


sed '/<IfLineWithThis>/s/<replaceThis>/<withThis>/g' <file>

replace/substitute only if the line doesn't contain


sed '/<IfLineWithoutThis>/!s/<replaceThis>/<withThis>/g' <file>

Regular expressions
Create a group and make backreference to the value (for instance deleting everything before and after the group)
sed '/<before>\(.*\)<after>/\1/d'

Emulate non-greedy behaviour (not supported by sed) by excluding unwanted character from the match (here for instance
extract URL from href by excluding ")
file contains: <a href="/url" name="link">link</a>

Вам также может понравиться