Linux – tcpdump capture network packets

tcpdump

tcpdump is a Linux program that can be used to capture network traffic to and from a Linux server and it’s clients.

It is well-docmented and needs no further description. This post just describes a particular, restricted, way of using it to capture packets between a specific client computer and the server, and to send the output to a file which can subsequently be read by the same program.

 

From specific client (a laptop = host 198.168.1.90)
---------------------------------------------------

-- capture the packets:
-i = on interface bond0
host = from client 198.168.1.90
-A = ASCII
-s = capture size 0 (65535 bytes by default)

tcpdump -i bond0 host 198.168.1.90 -A -s0 -w /tmp/rayfox.pcap

-- read pcap file and output to text file
tcpdump -A -r /tmp/rayfox.pcap > /tmp/rayfox.txt

(File can also be read using Wireshark)

To get the date from the pcap file

[root@server1 tmp] tcpdump -tttt -qns 0 -A -r server1_capture.pcap

Capture on a Specific Interface

# Capture on a specific interface (NIC or bond)
tcpdump -vXSs0 -i bond2 -w recv_mcast_java_1_1500.pcap

Read the tcpdump file, pipe to less for more control

# read the captured file
tcpdump -qns 0 -A -r recv_mcast_java_1_1500.pcap|less

Python – local http server

Install Python for windows

F:\>python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

then connect via a browser with URL http://localhost:8000

The current directory is served in the browser

SQL Server – Truncate transaction log

The transaction log is truncated when a transaction log backup is executed (recovery mode Full).

In an emergency situation, if the log becomes full and it is not possible to backup the log normally, it can be pseudo-backed-up to a fictitious location.

Use at your own risk and only do this if you know what you’re doing.

This will prevent the possibilty of point-in-time recovery, so a full backup should be done after this operation

BACKUP LOG <db name> TO DISK='NUL:'

SQL Server – Monitor Log Space


DBCC SQLPERF(logspace)

----------------------------------------------------------------------------------------------

DECLARE @sql_command varchar(1024)
DECLARE @logtable TABLE
(
   [Database Name] VARCHAR(1000) NULL,  
   [Log Size (MB)] numeric,
   [Log Space Used (%)] numeric,
   [Status] VARCHAR(128) NULL  
)

SELECT @sql_command = 'dbcc sqlperf (logspace)'

INSERT INTO @logtable EXEC (@sql_command)

select * from @logtable where [Database Name] = 'RFDB'

SELECT /* ignore this */ command,
            r.session_id, r.blocking_session_id,
            s.text,
            start_time,
            percent_complete, 
            CAST(((DATEDIFF(s,start_time,GetDate()))/3600) as varchar) + ' hour(s), '
                  + CAST((DATEDIFF(s,start_time,GetDate())%3600)/60 as varchar) + 'min, '
                  + CAST((DATEDIFF(s,start_time,GetDate())%60) as varchar) + ' sec' as running_time,
            CAST((estimated_completion_time/3600000) as varchar) + ' hour(s), '
                  + CAST((estimated_completion_time %3600000)/60000 as varchar) + 'min, '
                  + CAST((estimated_completion_time %60000)/1000 as varchar) + ' sec' as est_time_to_go,
            dateadd(second,estimated_completion_time/1000, getdate()) as est_completion_time 
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) s

DECLARE @whotable TABLE
(
   SPID INT,  
   Status VARCHAR(1000) NULL,  
   Login SYSNAME NULL,  
   HostName SYSNAME NULL,  
   BlkBy SYSNAME NULL,  
   DBName SYSNAME NULL,  
   Command VARCHAR(1000) NULL,  
   CPUTime INT NULL,  
   DiskIO INT NULL,  
   LastBatch VARCHAR(1000) NULL,  
   ProgramName VARCHAR(1000) NULL,  
   SPID2 INT,
   REQUESTID INT
)
 
INSERT INTO @whotable EXEC sp_who2
 
select * from @whotable order by BlkBy desc



Oracle ASM – List ASM files

SELECT concat('+'||gname, sys_connect_by_path(aname, '/')) full_path,
       gnumber group_number, gname group_name, file_number,
       system_created, nvl(file_type, 'Directory') file_type,
       decode(file_type, null, 'N', decode (system_created, 'N', 'Y', 'N')) file_alias,
       block_size, blocks, bytes, space, redundancy, striped, creation_date,
       modification_date
       , redundancy_lowered
FROM (SELECT  g.name gname, g.group_number gnumber, a.parent_index pindex, a.name aname,
              a.reference_index rindex , a.system_created, a.alias_directory,
              f.file_number,
              f.type file_type, f.block_size, f.blocks, f.bytes, f.space, f.redundancy, f.striped, f.creation_date, f.modification_date
              ,f.redundancy_lowered
      FROM   v$asm_alias a, v$asm_diskgroup g, v$asm_file f
      WHERE  a.group_number = g.group_number
      and    a.group_number = 20
      and    a.group_number = f.group_number (+)
      and    a.file_number = f.file_number (+))
START WITH (mod(pindex, power(2, 24))) = 0
CONNECT BY PRIOR rindex = pindex

postgresql – show sql commands behind shortcuts

Connect with the -E option
psql -E .....

Then:

postgres=# \du
********* QUERY **********
SELECT r.rolname, r.rolsuper, r.rolinherit,
  r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
  r.rolconnlimit, r.rolvaliduntil,
  ARRAY(SELECT b.rolname
        FROM pg_catalog.pg_auth_members m
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
        WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1;
**************************

                                        List of roles
      Role name      |                         Attributes                         | Member of
---------------------+------------------------------------------------------------+-----------

postgresql – help

psql is the PostgreSQL interactive terminal.

Usage:
  psql [OPTION]... [DBNAME [USERNAME]]

General options:
  -c, --command=COMMAND    run only single command (SQL or internal) and exit
  -d, --dbname=DBNAME      database name to connect to (default: "RAYFOX")
  -f, --file=FILENAME      execute commands from file, then exit
  -l, --list               list available databases, then exit
  -v, --set=, --variable=NAME=VALUE
                           set psql variable NAME to VALUE
                           (e.g., -v ON_ERROR_STOP=1)
  -V, --version            output version information, then exit
  -X, --no-psqlrc          do not read startup file (~/.psqlrc)
  -1 ("one"), --single-transaction
                           execute as a single transaction (if non-interactive)
  -?, --help[=options]     show this help, then exit
      --help=commands      list backslash commands, then exit
      --help=variables     list special variables, then exit

Input and output options:
  -a, --echo-all           echo all input from script
  -b, --echo-errors        echo failed commands
  -e, --echo-queries       echo commands sent to server
  -E, --echo-hidden        display queries that internal commands generate
  -L, --log-file=FILENAME  send session log to file
  -n, --no-readline        disable enhanced command line editing (readline)
  -o, --output=FILENAME    send query results to file (or |pipe)
  -q, --quiet              run quietly (no messages, only query output)
  -s, --single-step        single-step mode (confirm each query)
  -S, --single-line        single-line mode (end of line terminates SQL command)

Output format options:
  -A, --no-align           unaligned table output mode
  -F, --field-separator=STRING
                           field separator for unaligned output (default: "|")
  -H, --html               HTML table output mode
  -P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \pset command)
  -R, --record-separator=STRING
                           record separator for unaligned output (default: newline)
  -t, --tuples-only        print rows only
  -T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)
  -x, --expanded           turn on expanded table output
  -z, --field-separator-zero
                           set field separator for unaligned output to zero byte
  -0, --record-separator-zero
                           set record separator for unaligned output to zero byte

Connection options:
  -h, --host=HOSTNAME      database server host or socket directory (default: "local socket")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name (default: "RAYFOX")
  -w, --no-password        never prompt for password
  -W, --password           force password prompt (should happen automatically)

For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.

Report bugs to <pgsql-bugs@postgresql.org>.