Using AWK to print N lines up until the FIRST MATCH of a string and then exit

Often you are looking for a keyword within a log file but you need to print out X lines BEFORE the first match of a string, THEN exit the search.

There are many ways to skin a cat, and here are a few ways.

EXAMPLE 1:

In the following I am looking for 7 lines BEFORE the keyword phrase 'Rolling back'

# cat logfile  | awk   'BEGIN{stopline=7} NR==1,/Rolling back/ {n=NR;l[n]=$0}END{last=n-stopline;for (i=last;i<=n;i++) {print l[i]}}'

Output:

2022-06-11 16:56:26 [pid 39154] LOG-THRIFTCLIENT-0002: [1] [tid 39154] Thrift transport exception type #1 (socket open() error: No route to host); failed to establish connection [addr=tt-peer-controller./port=9090].
Command killed: (signal 14) /usr/tintri/bin/fscmd -a 60 -h 'tt-peer-controller.' get-tuning-variable 'stasisDegradedModeTruncationTimeout'
Can't get 'stasisDegradedModeTruncationTimeout', error 142
Can't get timeouts, error 142
Upgrade completed; exit code 109
2022-06-11T16:57:22.166513-07:00 la-tintri01#b upgrade_pkg[30360]: Script /etc/sysconfig/tintri/post_upd/90-upgrade_fs exit code: 109
2022-06-11T16:57:22.167052-07:00 la-tintri01#b upgrade_pkg[30360]: Can not perform application upgrade, error 109
2022-06-11T16:57:22.167467-07:00 la-tintri01#b upgrade_pkg[30360]: Rolling back, state 9

EXAMPLE 2:

An easier way is to chain with tail; but who in the Linux world wants to do things the easy way right!?!?

#zcat logfile |awk 'NR==1,/Rolling back/' |tail -7

OUTPUT

2022-06-11 16:56:26 [pid 39154] LOG-THRIFTCLIENT-0002: [1] [tid 39154] Thrift transport exception type #1 (socket open() error: No route to host); failed to establish connection [addr=tt-peer-controller./port=9090].
Command killed: (signal 14) /usr/tintri/bin/fscmd -a 60 -h 'tt-peer-controller.' get-tuning-variable 'stasisDegradedModeTruncationTimeout'
Can't get 'stasisDegradedModeTruncationTimeout', error 142
Can't get timeouts, error 142
Upgrade completed; exit code 109
2022-06-11T16:57:22.166513-07:00 la-tintri01#b upgrade_pkg[30360]: Script /etc/sysconfig/tintri/post_upd/90-upgrade_fs exit code: 109
2022-06-11T16:57:22.167052-07:00 la-tintri01#b upgrade_pkg[30360]: Can not perform application upgrade, error 109
2022-06-11T16:57:22.167467-07:00 la-tintri01#b upgrade_pkg[30360]: Rolling back, state 9

EXAMPLE 3

Sometimes we need to search multiple files:

# awk   'NR==1,/Rolling back/' *.log |grep -H -B20 Rolling 

Or for GZ files

# zcat logfile*.gz|awk 'NR==1,/Rolling back/'

OR

# awk 'NR==1,/Rolling back/' <(gzip -dc logfile*.gz)
Posted in Command Line FU | Leave a comment

Ipmitool sel elist error: Timestamp Clock Sync | Asserted

The following error may be seen on a controller when viewing the 'ipmitool sel elist' output:

 275 | 06/01/2022 | 20:33:53 | Power Unit Pwr Unit Status | AC lost | Asserted
 276 | 06/01/2022 | 20:33:54 | Power Unit Pwr Unit Status | AC lost | Deasserted
 277 | 06/01/2022 | 20:34:05 | System Event BIOS Evt Sensor | Timestamp Clock Sync | Asserted
 278 | 06/01/2022 | 20:34:06 | System Event BIOS Evt Sensor | Timestamp Clock Sync | Asserted
 279 | 06/01/2022 | 20:34:45 | System Event BIOS Evt Sensor | OEM System boot event | Asserted
 27a | 06/01/2022 | 21:19:53 | Power Unit Pwr Unit Status | Power off/down | Asserted
 27b | 06/01/2022 | 21:19:59 | Power Unit Pwr Unit Status | Power off/down | Deasserted
 27c | 06/01/2022 | 21:20:12 | System Event BIOS Evt Sensor | Timestamp Clock Sync | Asserted
 27d | 06/01/2022 | 21:20:12 | System Event BIOS Evt Sensor | Timestamp Clock Sync | Asserted
 27e | 06/01/2022 | 21:20:51 | System Event BIOS Evt Sensor | OEM System boot event | Asserted
 27f | 06/01/2022 | 21:53:43 | Power Unit Pwr Unit Status | Power off/down | Asserted
 280 | 06/01/2022 | 21:53:49 | Power Unit Pwr Unit Status | Power off/down | Deasserted
 281 | 06/01/2022 | 21:54:02 | System Event BIOS Evt Sensor | Timestamp Clock Sync | Asserted
 282 | 06/01/2022 | 21:54:02 | System Event BIOS Evt Sensor | Timestamp Clock Sync | Asserted
 283 | 06/01/2022 | 21:54:41 | System Event BIOS Evt Sensor | OEM System boot event | Asserted

This message is one of two you will see when the system is powered on, or power cycled. 

Note in the above you also see "Power Unit Pwr Unit Status | Power off/down "

Posted in IPMI, Network Attached Storage (NAS) | Tagged , , | Leave a comment

Telnet to a port on a remote machine without ncat or telnet, or nmap

There are situations where you may be working on Linux based system, and telnet, ncat or nmap does not exist.

In Linux, you can route a test thru the protocol, destination IP address, and destination  port using the "/dev" linux 'pseudo device' 

The 'telnet' command we are all familiar with is essentially a wrapper for this procedure.

This can be done safely without impact to system operation.

# echo > /dev/tcp/10.10.154.121/10600 && echo "TCP Port 10600 is open"

The same works with UDP

 echo > /dev/udp/10.10.154.121/111 && echo "UDP Port 111 is open"

To test multiple TCP ports via for loop:

# for i in 88 111 135 139 445 464 3268 3269 ; do
echo > /dev/tcp/10.10.154.133/$i && echo "TCP Port $i is open"
done

Posted in Command Line FU, Networking | Tagged , , , , | Leave a comment

sed print only line AFTER string match

sed -n '/pattern/{n;p;}' infile

File

# cat infile
line 1
line 2
pattern
line 3
line 4

Example

# sed -n '/pattern/{n;p;}' infile
line 3
Posted in Command Line FU | Tagged , , | Leave a comment

Remove Leading and Trailing Whitespace using gsub in AWK

# echo "  this  test   "|awk '{gsub(/^[[:space:]]+|[[:space:]]+$/,"",$0); print}'
this  test
Posted in Command Line FU | Tagged | Leave a comment

Cut all characters after last match using SED

# echo "xxx/xxxx/xxxxx/yyy" | sed 's|\(.*\)/.*|\1|'
# echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|\(.*\)/.*|\1|'

Output

xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x
Posted in Command Line FU | Tagged , , | Leave a comment

Increment a variable name within a BASH loop

#!/bin/bash
c=0
for i in a b; do
	log[$c]="$i"
	((c++))
done
for i in ${log[@]}; do
	echo $i
done

Example

# ./btest
a
b
Posted in Command Line FU | Tagged , , | Leave a comment

Enable CIFS/SMB Debug Logging

The following shows how you may enable additional debug logging on Linux for troubleshooting SMB related issues.

# modprobe cifs
# echo 'module cifs +p' > /sys/kernel/debug/dynamic_debug/control
# echo 'file fs/cifs/* +p' > /sys/kernel/debug/dynamic_debug/control
# echo 7 > /proc/fs/cifs/cifsFYI

Example log output:

# dmesg |tail -50
[75527.604137] CIFS: fs/cifs/connect.c: sndbuf 16384 rcvbuf 131072 rcvtimeo 0x1b58
[75527.606233] CIFS: fs/cifs/connect.c: cifs_get_tcp_session: next dns resolution scheduled for 600 seconds in the future
[75527.606246] CIFS: fs/cifs/connect.c: VFS: in cifs_get_smb_ses as Xid: 59 with uid: 0
[75527.606254] CIFS: fs/cifs/connect.c: Existing smb sess not found
[75527.606274] CIFS: fs/cifs/smb2pdu.c: Negotiate protocol
[75527.606298] CIFS: fs/cifs/transport.c: wait_for_free_credits: remove 1 credits total=0
[75527.606342] CIFS: fs/cifs/transport.c: Sending smb: smb_len=106
[75527.606530] CIFS: fs/cifs/connect.c: Demultiplex PID: 1177
[75527.606911] CIFS: fs/cifs/connect.c: RFC1002 header 0xe1
[75527.606940] CIFS: fs/cifs/smb2misc.c: SMB2 data length 97 offset 128
[75527.606949] CIFS: fs/cifs/smb2misc.c: SMB2 len 225
[75527.606963] CIFS: fs/cifs/smb2ops.c: smb2_add_credits: added 10 credits total=10
[75527.606976] CIFS: fs/cifs/transport.c: cifs_sync_mid_result: cmd=0 mid=0 state=4
[75527.606991] CIFS: fs/cifs/misc.c: Null buffer passed to cifs_small_buf_release
[75527.607001] CIFS: fs/cifs/smb2pdu.c: mode 0x1
[75527.607004] CIFS: fs/cifs/smb2pdu.c: negotiated smb3.0 dialect
[75527.607028] CIFS: fs/cifs/connect.c: Security Mode: 0x1 Capabilities: 0x300055 TimeAdjust: 0
[75527.607033] CIFS: fs/cifs/smb2pdu.c: Session Setup
[75527.607036] CIFS: fs/cifs/smb2pdu.c: sess setup type 3
[75527.607051] CIFS: fs/cifs/cifs_spnego.c: key description = ver=0x2;host=172.16.236.17;ip4=172.16.236.17;sec=krb5;uid=0x1388;creduid=0x0;user=bpatridge;pid=0x496
[75527.637196] CIFS: VFS: Verify user has a krb5 ticket and keyutils is installed
[75527.637225] CIFS: VFS: \\172.16.236.17 Send error in SessSetup = -126
[75527.637235] CIFS: fs/cifs/connect.c: VFS: leaving cifs_get_smb_ses (xid = 59) rc = -126
[75527.637245] CIFS: fs/cifs/dfs_cache.c: cache_refresh_path: search path: \172.16.236.17\tintri
[75527.637260] CIFS: fs/cifs/dfs_cache.c: get_dfs_referral: get an DFS referral for \172.16.236.17\tintri
[75527.637281] CIFS: fs/cifs/connect.c: VFS: leaving mount_put_conn
Posted in SAMBA/SMB/CIFS | Tagged , , , | Leave a comment

Obtain TCP Network netstat output without netstat

There are times you may be on a system and for whatever insane reason, netstat is not installed.

Here is how you can obtain interface information without netstat

# awk '(f==0) { i=1; while ( i<=NF) {n[i] = $i; i++ }; f=1; next} (f==1){ i=2; while ( i<=NF){ printf "%s = %d\n", n[i], $i; i++}; f=0}'  /proc/net/netstat |column -t

Example results

# awk '(f==0) { i=1; while ( i<=NF) {n[i] = $i; i++ }; f=1; next} (f==1){ i=2; while ( i<=NF){ printf "%s = %d\n", n[i], $i; i++}; f=0}'  /proc/net/netstat |column -t
SyncookiesSent             =  0
SyncookiesRecv             =  0
SyncookiesFailed           =  5
EmbryonicRsts              =  0
PruneCalled                =  0
RcvPruned                  =  0
OfoPruned                  =  0
OutOfWindowIcmps           =  0
LockDroppedIcmps           =  0
ArpFilter                  =  0
TW                         =  1160
TWRecycled                 =  0
TWKilled                   =  0
PAWSPassive                =  0
PAWSActive                 =  0
PAWSEstab                  =  5
DelayedACKs                =  25254
DelayedACKLocked           =  21
DelayedACKLost             =  4129
ListenOverflows            =  0
ListenDrops                =  0
TCPPrequeued               =  419476
TCPDirectCopyFromBacklog   =  7131447
TCPDirectCopyFromPrequeue  =  70660208
TCPPrequeueDropped         =  0
TCPHPHits                  =  650154
TCPHPHitsToUser            =  35433
TCPPureAcks                =  241811
TCPHPAcks                  =  29236
TCPRenoRecovery            =  0
TCPSackRecovery            =  21
TCPSACKReneging            =  0
TCPFACKReorder             =  0
Posted in Linux, Networking | Tagged , | Leave a comment

Using Windows version of IPMITool called (ipmiutil) does not work when attempting to activate an SOL connection.

IPMIUtil is the Windows Equivalent of IPMITOOLS.

It may be downloaded from Sourceforge:  http://ipmiutil.sourceforge.net/

After installing the appropriate Windows Binary, here is the basic syntax of ipmiutil after you install the binary

c:\Users\Support>ipmiutil
ipmiutil ver 3.18
Usage: ipmiutil <command> [other options]
   where <command> is one of the following:
        alarms  show/set the front panel alarm LEDs and relays
        leds    show/set the front panel alarm LEDs and relays
        discover        discover all IPMI servers on this LAN
        cmd     send a specified raw IPMI command to the BMC
        config  list/save/restore BMC configuration parameters
        dcmi    get/set DCMI parameters
        ekanalyzer      run EKeying analyzer on FRU files (deprecated, see fru)
        events  decode IPMI events and display them
        firewall        show/set firmware firewall functions
        fru     show decoded FRU inventory data, write asset tag
        fwum    OEM firmware update manager extensions
        getevt  get IPMI events and display them, event daemon
        getevent        get IPMI events and display them, event daemon
        health  check and show the basic health of the IPMI BMC
        hpm     HPM firmware update manager extensions
        lan     show/set IPMI LAN parameters, users, PEF rules
        picmg   show/set picmg extended functions
        power   issue IPMI reset or power control to the system
        reset   issue IPMI reset or power control to the system
        sel     show/clear firmware System Event Log records
        sensor  show Sensor Data Records, readings, thresholds
        serial  show/set IPMI Serial & Terminal Mode parameters
        sol     start/stop an SOL console session
        smcoem  SuperMicro OEM functions
        sunoem  Sun OEM functions
        delloem Dell OEM functions
        tsol    Tyan SOL console start/stop session
        user    list or modify IPMI LAN users
        wdt     show/set/reset the watchdog timer
   common IPMI LAN options:
       -N node  Nodename or IP address of target system
       -U user  Username for remote node
       -P/-R pswd  Remote Password
       -E   use password from Environment IPMI_PASSWORD
       -F   force driver type (e.g. imb, lan2)
       -J 0 use lanplus cipher suite 0: 0 thru 14, 3=default
       -T 1 use auth Type: 1=MD2, 2=MD5(default), 4=Pswd
       -V 2 use priVilege level: 2=user(default), 4=admin
       -Y   prompt for remote password
       -Z   set slave address of local MC
For help on each command (e.g. 'sel'), enter:
   ipmiutil sel -?
ipmiutil , usage or help requested

Many of the basic commands DO work.

C:\>ipmiutil user list 1 -N 10.132.14.27 -U admin -P mypass99 -V 3
ipmiutil user ver 3.18
Connecting to node  10.132.14.27
User  1: chan=1         disabled          Admin
User  2: chan=1         disabled          Admin         szadm
User  3: chan=1          enabled        No access       admin
User  4: chan=1         disabled        No access
User  5: chan=1         disabled        No access
User  6: chan=1         disabled        No access
User  7: chan=1         disabled        No access
User  8: chan=1         disabled        No access
User  9: chan=1         disabled        No access
User 10: chan=1         disabled        No access
ipmiutil user, completed successfully

Example viewing sensor data also works

C:\>ipmiutil sensor -N 10.132.14.27 -U admin -P mypass99 -V 3
ipmiutil sensor version 3.18
Connecting to node  10.132.14.27
-- BMC version 2.21, IPMI version 2.0
_ID_ SDR_Type_xx ET Own Typ S_Num   Sens_Description   Hex & Interp Reading
0001 SDR Full 01 01 20 a 01 snum 33 DRIVE14_TEMP     = 20 OK   32.00 degrees C
0002 SDR Full 01 01 20 a 02 snum 6f PVCCSA_CPU1      = 2e OK   0.92 Volts
0003 SDR Full 01 01 20 a 01 snum 01 PCH_TEMP         = 2d OK   45.00 degrees C
0004 SDR Full 01 01 20 a 01 snum 13 CPU1_DIMMB1_TEMP = 00 Absent 0.00 na
0005 SDR Full 01 01 20 a 04 snum 4b FAN_TACH5        = 70 OK   11200.00 RPM
0006 SDR Full 01 01 20 a 01 snum 11 CPU1_DIMMA1_TEMP = 00 Absent 0.00 na
0007 SDR Full 01 01 20 a 02 snum 6d PVCCIN_CPU1      = 59 OK   1.78 Volts
0008 SDR Full 01 01 20 a 01 snum 36 DRIVE17_TEMP     = 1f OK   31.00 degrees C
0009 SDR Full 01 01 20 a 04 snum 48 FAN_TACH2        = 83 OK   13100.00 RPM
000a SDR Full 01 01 20 a 01 snum 27 DRIVE2_TEMP      = 1f OK   31.00 degrees C
000b SDR Full 01 01 20 a 02 snum 72 VOLT_PVNN        = 32 OK   1.00 Volts
000c SDR Full 01 01 20 a 04 snum 4f FAN_TACH9        = 73 OK   11500.00 RPM
000d SDR Full 01 01 20 a 01 snum 32 DRIVE13_TEMP     = 1f OK   31.00 degrees C
000e SDR Full 01 01 20 a 01 snum 3a DRIVE21_TEMP     = 20 OK   32.00 degrees C
000f SDR Full 01 01 20 a 04 snum 5d PSU0_FAN1        = 82 OK   13000.00 RPM
0010 SDR Full 01 01 20 a 04 snum 4d FAN_TACH7        = 73 OK   11500.00 RPM

However, when attempting to initialize a Serial Over Lan (SOL) connection using ipmiutil you may encounter a condition where you do not see a login prompt or the expected screen output.

Example:

Even when following the usage and specifying a ~? for help you do not SEE A login prompt. The terminal remains black and does not show anything.

Even if you attempt to TYPE on the screen, you will see text but it will then disconnect.

The PROBLEM is that the Windows Command Prompt does not correctly render the Terminal Output.

The SOLUTION is to use a Linux and/or MAC system and ipmitool.

Posted in Windows | Tagged , , , , | 1 Comment