Using grep to search a file using multiple words as boolean AND OR condition

Command Overview

The following extended grep  (grep -E) example shows how to search a log files for

  1. Any line containing the words
    1. init
      AND
    2. SNMP OR tomcat 
  2. Any line containing either
    1. start_tomcat
      OR
    2. stop_tomcat
      OR
    3. start_snmp
      OR
    4. stop_snmp
  3. excluding any match with the word INT

Example

# grep -E "init.*(snmp|tomcat)|(start|stop)_(tomcat|snmp)" debug.log |grep -v INT 2024-09-10T13:54:36.686053-07:00 nec-6090-01#b stop_tomcat[4019]: running, pid file: /var/run/jsvc.pid , timeout interval: 15 secs 2024-09-10T13:54:36.687429-07:00 nec-6090-01#b stop_tomcat[4019]: pid: 1651 2024-09-10T13:54:36.704588-07:00 nec-6090-01#b stop_tomcat[4019]: init-+-AuthenticationS---7*[{Authentication}] 2024-09-10T13:54:36.705498-07:00 nec-6090-01#b stop_tomcat[4019]: |-HAMon---15*[{HAMon}] 2024-09-10T13:54:36.706346-07:00 nec-6090-01#b stop_tomcat[4019]: |-PlatMon---13*[{PlatMon}] 2024-09-10T13:54:36.707142-07:00 nec-6090-01#b stop_tomcat[4019]: |-ProcMon-+-realstore---98*[{realstore}] 2024-09-10T13:54:36.707881-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.708690-07:00 nec-6090-01#b stop_tomcat[4019]: `-9*[{ProcMon}] 2024-09-10T13:54:36.709454-07:00 nec-6090-01#b stop_tomcat[4019]: |-agetty 2024-09-10T13:54:36.710315-07:00 nec-6090-01#b stop_tomcat[4019]: |-cimserver---2*[{cimserver}] 2024-09-10T13:54:36.711077-07:00 nec-6090-01#b stop_tomcat[4019]: |-corewatch 2024-09-10T13:54:36.711792-07:00 nec-6090-01#b stop_tomcat[4019]: |-crond 2024-09-10T13:54:36.712541-07:00 nec-6090-01#b stop_tomcat[4019]: |-dbus-daemon 2024-09-10T13:54:36.713388-07:00 nec-6090-01#b stop_tomcat[4019]: |-java---21*[{java}] 2024-09-10T13:54:36.714237-07:00 nec-6090-01#b stop_tomcat[4019]: |-jsvc-+-jsvc 2024-09-10T13:54:36.714977-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.715776-07:00 nec-6090-01#b stop_tomcat[4019]: `-jsvc---62*[{jsvc}] 2024-09-10T13:54:36.716540-07:00 nec-6090-01#b stop_tomcat[4019]: |-jsvc-+-jsvc 2024-09-10T13:54:36.717367-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.718192-07:00 nec-6090-01#b stop_tomcat[4019]: `-jsvc---109*[{jsvc}] 2024-09-10T13:54:36.720254-07:00 nec-6090-01#b stop_tomcat[4019]: |-lldpd---lldpd 2024-09-10T13:54:36.721010-07:00 nec-6090-01#b stop_tomcat[4019]: |-6*[mingetty] 2024-09-10T13:54:36.721717-07:00 nec-6090-01#b stop_tomcat[4019]: |-ntpd---ntpd 2024-09-10T13:54:36.722473-07:00 nec-6090-01#b stop_tomcat[4019]: |-rpcbind 2024-09-10T13:54:36.723252-07:00 nec-6090-01#b stop_tomcat[4019]: |-rsyslogd-+-log_scrub.py 2024-09-10T13:54:36.724039-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.724822-07:00 nec-6090-01#b stop_tomcat[4019]: `-9*[{rsyslogd}] 2024-09-10T13:54:36.725621-07:00 nec-6090-01#b stop_tomcat[4019]: |-runuser---postmaster---18*[postmaster] 2024-09-10T13:54:36.726416-07:00 nec-6090-01#b stop_tomcat[4019]: |-runuser---postmaster---16*[postmaster] 2024-09-10T13:54:36.727143-07:00 nec-6090-01#b stop_tomcat[4019]: |-sh---stop_tomcat---pstree 2024-09-10T13:54:36.727860-07:00 nec-6090-01#b stop_tomcat[4019]: |-sshd-+-sshd---rsync.bin 2024-09-10T13:54:36.728562-07:00 nec-6090-01#b stop_tomcat[4019]: | 2024-09-10T13:54:36.729270-07:00 nec-6090-01#b stop_tomcat[4019]: `-sshd---bash---service---tomcat---initctl 2024-09-10T13:54:36.730039-07:00 nec-6090-01#b stop_tomcat[4019]: |-thriftshell---11*[{thriftshell}] 2024-09-10T13:54:36.730807-07:00 nec-6090-01#b stop_tomcat[4019]: |-udevd---2*[udevd] 2024-09-10T13:54:36.731537-07:00 nec-6090-01#b stop_tomcat[4019]: |-watchdog 2024-09-10T13:54:36.732229-07:00 nec-6090-01#b stop_tomcat[4019]: `-winbindd---winbindd 2024-09-10T13:54:37.735365-07:00 nec-6090-01#b stop_tomcat[4019]: Tomcat is stopped 2024-09-10T13:54:37.738757-07:00 nec-6090-01#b init[4098]: tomcat post-start 2024-09-10T13:54:39.366904-07:00 nec-6090-01#b start_tomcat[4097]: LOG-TOMCAT-0003: TASKSET options 2024-09-10T13:54:39.369087-07:00 nec-6090-01#b start_tomcat[4097]: Set JAVA options -Xms128M -Xmx2800M -XX:MaxMetaspaceSize=140M -XX:ReservedCodeCacheSize=140M -XX:+UseCodeCacheFlushing -Xss512k -XX:+UseStringDeduplication -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=*:5005 -XX:+PrintClassHistogram -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/corefiles -XX:ErrorFile=/var/corefiles/hs_err_pid<>.log -Dfile.encoding=UTF8 -Dcom.tintri.platform.productid=BR2 -Dhttps.protocols=TLSv1.2 -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false 2024-09-10T13:54:39.396222-07:00 nec-6090-01#b start_tomcat[4097]: Starting server using jsvc. 2024-09-10T13:55:11.215363-07:00 nec-6090-01#b stop_snmp[15117]: Stopping snmp agent 2024-09-10T13:55:12.217316-07:00 nec-6090-01#b stop_snmp[15117]: SNMP agent is stopped 2024-09-10T13:55:12.246235-07:00 nec-6090-01#b start_snmp[15416]: Set JAVA options -Xms64M -Xmx128M -XX:MaxMetaspaceSize=48M -XX:ReservedCodeCacheSize=48M -XX:+UseCodeCacheFlushing -Xss512k -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=*:5006 -XX:+UseStringDeduplication -XX:+PrintClassHistogram -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/corefiles -Dcom.tintri.platform.productid=BR2 -Dfile.encoding=UTF8 2024-09-10T13:55:12.247242-07:00 nec-6090-01#b start_snmp[15416]: Starting SNMP agent using jsvc. 2024-09-10T14:27:39.615636-07:00 nec-6090-01#b vmstore[2582]: HA: [2961] [tid 4996] HA-STATE: errorDetected: module [1], current state [104] indicates Error 2024-09-10T14:27:39.615698-07:00 nec-6090-01#b vmstore[2582]: HA: [2962] [tid 4996] HA-STATE: setStatusLocked: setting nodeRole to [SECONDARY], nodeStatus to [DISCONNECTED] 2024-09-10T14:27:39.690762-07:00 nec-6090-01#b vmstore[2582]: HA: [2967] [tid 5043] HA-STATE: errorDetected: module [5], current state [104] indicates Error 2024-09-10T14:27:40.191327-07:00 nec-6090-01#b vmstore[2582]: HA: [2969] [tid 5020] HA-STATE: setStatusLocked: setting nodeRole to [PRIMARY], nodeStatus to [SELECTED] 2024-09-10T14:27:40.312790-07:00 nec-6090-01#b vmstore[2582]: HA: [2970] [tid 2911] HA-STATE: disconnecting Secondary; becoming Primary 2024-09-10T14:27:40.313379-07:00 nec-6090-01#b vmstore[2582]: HA: [2980] [tid 2911] HA-STATE: notified HA modules to become Primary 2024-09-10T14:27:40.914011-07:00 nec-6090-01#b vmstore[2582]: HA: [3672] [tid 2911] HA-STATE: becomePrimary: invoked 2024-09-10T14:27:40.914059-07:00 nec-6090-01#b vmstore[2582]: HA: [3673] [tid 2911] HA-STATE: becomePrimary: eligible to be Primary due to NVRAM gen num [4] >= current disk gen num [4] 2024-09-10T14:27:40.914099-07:00 nec-6090-01#b vmstore[2582]: HA: [3674] [tid 2911] HA-STATE: updateGenNum: STARTUP: writing intended gen num [5] 2024-09-10T14:27:40.956406-07:00 nec-6090-01#b vmstore[2582]: HA: [3676] [tid 2911] HA-STATE: updateGenNum: writing NVRAM gen num as [5] 2024-09-10T14:27:40.999307-07:00 nec-6090-01#b vmstore[2582]: HA: [3681] [tid 2911] HA-STATE: setStatusLocked: setting nodeRole to [PRIMARY], nodeStatus to [RECOVERING] 2024-09-10T14:27:46.172490-07:00 nec-6090-01#b vmstore[2582]: HA: [7846] [tid 2911] HA-STATE: primaryBeginAccepting: invoked 2024-09-10T14:27:46.172972-07:00 nec-6090-01#b vmstore[2582]: HA: [7856] [tid 2911] HA-STATE: setStatusLocked: setting nodeRole to [PRIMARY], nodeStatus to [ACTIVE] 2024-09-10T14:27:46.173513-07:00 nec-6090-01#b vmstore[2582]: HA: [7868] [tid 746] HA-STATE: haManagerListenerThread: invoked 2024-09-10T14:28:46.213586-07:00 nec-6090-01#b vmstore[2582]: HA: [10685] [tid 746] HA-STATE: syncComplete: module [5] indicates SyncComplete 2024-09-10T14:28:47.113674-07:00 nec-6090-01#b vmstore[2582]: HA: [10815] [tid 4997] HA-STATE: syncComplete: module [1] indicates SyncComplete
Posted in Command Line FU, Linux, Uncategorized | Comments Off on Using grep to search a file using multiple words as boolean AND OR condition

Use sed to replace last character after matching a line

If you are a coder like me we tend to discover after we have made a script that we need to change something on multiple lines.

Recently I have a script whereby added a "log_message" function which would echo a phrase.

However, I wanted to append a double parenthesis after the last character in the entire file.

For example.

Inside of myscript.sh I have the following

# grep Beginning myscript.sh
        log_message "Beginning function enableOrDisableRealstore"
        log_message "Beginning function modulePowerCycleChassis"
        log_message "Beginning function checkFipsLicense"
        log_message "Beginning function enableFips"
        log_message "Beginning function backupFips"
        log_message "FIPS: Beginning function isFipsEnabled"
        log_message "Beginning function isEncryptionEnabled"
        log_message "Beginning function isControllerPrimary"
        log_message "Beginning function isPeerControllerUpAndExecuting"
        log_message "Beginning function preZeroizeControllersValidation"
        log_message "Beginning function preZeroizeHaValidation"
        log_message "Beginning function preZeroizeDataCollection"
        log_message "Beginning function validateMachine"
        log_message "Beginning function cleanupDatabases"
        log_message "Beginning function isTxosRunning"
        log_message "Beginning function cleanupTunables"
        log_message "Beginning function checkControllerFailLogs"
        log_message "Beginning function getNetworkInfo"
        log_message "Beginning function getMDRaidInfo"
        log_message "Beginning function eraseSsdZion"
        log_message "Beginning function secureEraseSsd"
        log_message "Beginning function _retry_secerase for $disk"
        log_message "Beginning function _powercycle_disk_slot for $disk"
        log_message "Beginning function check_dd_process"
        log_message "Beginning function writeZeroesToHdd"
        log_message "Beginning function verifyBootDomPartition"
        log_message "Beginning function listDOM"
        log_message "Beginning function makePartitionsOnSSD"
        log_message "Beginning function formatRealstore"
        log_message "Beginning function updateDiskSignature"
        log_message "Beginning function deleteLogsAndCores"
        log_message "Beginning function resetRootPassword_default"
        log_message "Beginning function resetRootPassword_internal"
        log_message "Beginning function resetRootPassword"
        log_message "Beginning function resetAdminPasswordToSerialNumber"
        log_message "Beginning function validateSerialNumber"

My goal was to replace the last quote with ()" so that each line would look like

log_message "Beginning function validateSerialNumber()"

This is easily resolved using sed!

# sed -i.bak 's/\(.*Beginning.*\)[^ ]$/\1\(\)\"/' myscript.sh

the above backs up myscript.sh to myscript.sh.bak

Then it replaces the last quote with ()"

The result is

# grep Beginn myscript.sh
        log_message "Beginning function enableOrDisableRealstore()"
        log_message "Beginning function modulePowerCycleChassis()"
        log_message "Beginning function checkFipsLicense()"
        log_message "Beginning function enableFips()"
        log_message "Beginning function backupFips()"
        log_message "FIPS: Beginning function isFipsEnabled()"
        log_message "Beginning function isEncryptionEnabled()"
        log_message "Beginning function isControllerPrimary()"
        log_message "Beginning function isPeerControllerUpAndExecuting()"
        log_message "Beginning function preZeroizeControllersValidation()"
        log_message "Beginning function preZeroizeHaValidation()"
        log_message "Beginning function preZeroizeDataCollection()"
        log_message "Beginning function validateMachine()"
        log_message "Beginning function cleanupDatabases()"
        log_message "Beginning function isTxosRunning()"
        log_message "Beginning function cleanupTunables()"
        log_message "Beginning function checkControllerFailLogs()"
        log_message "Beginning function getNetworkInfo()"
        log_message "Beginning function getMDRaidInfo()"
        log_message "Beginning function eraseSsdZion()"
        log_message "Beginning function secureEraseSsd()"
        log_message "Beginning function _retry_secerase for $disk()"
        log_message "Beginning function _powercycle_disk_slot for $disk()"
        log_message "Beginning function check_dd_process()"
        log_message "Beginning function writeZeroesToHdd()"
        log_message "Beginning function verifyBootDomPartition()"
        log_message "Beginning function listDOM()"
        log_message "Beginning function makePartitionsOnSSD()"
        log_message "Beginning function formatRealstore()"
        log_message "Beginning function updateDiskSignature()"
        log_message "Beginning function deleteLogsAndCores()"
        log_message "Beginning function resetRootPassword_default()"
        log_message "Beginning function resetRootPassword_internal()"
        log_message "Beginning function resetRootPassword()"
        log_message "Beginning function resetAdminPasswordToSerialNumber()"
        log_message "Beginning function validateSerialNumber()"
Posted in Command Line FU | Tagged , , | Leave a comment

Viewing contents of an open filehandle on a deleted file

There are times when you may unintentionally delete a file which is being written to.

When this occurs it will leave an Open Filehandle.

In Linux open filehandles may be viewed via the 'lsof' command.

# lsof +L1

In the following example here was the situation.

  1. I executed a script using nohup
    (i.e.   "nohup vmstore-tnt.sh  &")   
  2. File /root/nohup.out was being written to.
  3. I deleted the file nohup.out
  4. Thus this left an open filehandle.
$ lsof +L1
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NLINK    NODE NAME
vmstore-t 57520 root    1w   REG  252,1    26136     0 1671175 /root/nohup.out (deleted)
vmstore-t 57520 root    2w   REG  252,1    26136     0 1671175 /root/nohup.out (deleted)

Merely recreating the file via (touch /root/nohup.out) will not restore the file

To view the contents of the file that was deleted, you have to locate the Process ID (PID) from the above 'lsof' output.

(The above shows the PID as 57520)

Now wee can look for the fd (file descriptors) in Linux /proc pseudo filesystem by looking in /proc/<pid>/fd

# ls /proc/57520/fd
0  1  2  255
NUMBER MEANING
0Standard Input
1Standard Output
2Standard Error
255The File

*note: There may be additional File Descriptor Numbers which are beyond  the scope of this document.

Now, to view the real-time file progress you may issue

# cat /proc/57520/fd/1

To view any standard error  real-time you may view

# cat /proc/57520/fd/2

If you wish to temporarily restore the standard output of the file so you can monitor the output, you may create a 'soft' link via:

# ln -s /proc/57520/fd/1  /root/nohup.out

You may verify the link via

$ ls -Al nohup.out
lrwxrwxrwx 1 root root    16 May  1 15:02 nohup.out -> /proc/57520/fd/1

*note: Once the process stops you will have to delete the soft link!

When you try to view the contents you will see

# tail nohup.out
tail: cannot open `nohup.out' for reading: No such file or directory

Therefore remove the file

# rm nohup.out

We should no longer see any open files

# lsof +L1
#
Posted in Command Line FU, Internals, Linux | Leave a comment

MACOS – Clicking on webpage automatically jumps to top of the page

I recently encountered yet another MAC Madness issue!

I changed out my wired MacBook extended keyboard for a used Apple 'Magic' Keyboard.

Well the 'magic' was that it introduced an unwanted 'feature' whereby when I switch between external monitors, and the webpage in view is scrolled downward, the page would automagically jump to the top of the page. I am at Geek Level 4 in that I have 3 external monitors. So switching between them would move any object on that page automatically to the top; text box, text editor, webpage/etc.

After trolling the Google Verse I didn't find anything useful.

So after hacking at the Settings on MacOS Ventura I figured it out.

I went to Settings --> Accessibility --> Keyboard

Then uncheck Full Keyboard Access

After that I was back in business.

Posted in MAC-OSX | Leave a comment

Cscope search results are invisible

Part of my 'day job' is trolling thru source code when looking for answers. My preferred mechanism, other than a recursive grep, is to use cscope

Cscope is a useful tool for searching a source code tree after you build a list of files to search

The Problem

However, in this conundrum, when I would use any of the search options within cscope the File list (or list of source files) was empty, blank, envisible... basically, I could not SEE any of the results!

Example

Recreating the issue

In the following example, I built a list of files so that I could populate cscope

# cd /export/hg/default/pstoolkit
# find PstkCore/ -type f -not -path "*__In*" -not -path "*__Out*" -not -path "*ThirdPart*" -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" -o -name "*.cs*" > cscope.files

Then I read the results into cscope

# ls -al cscope.files
-rw-rw-r-- 1 bpatridge bpatridge 77196 Feb 13 12:46 cscope.files
# cscope -q -R -b -i cscope.files
# 

Now I attempt to open cscope

# cscope -d

Now I see the window:

However,

When I search for string "TimeOfDay" the search results are invisible?

I even attempted to change the colors but the results were the same

Oddly enough, when I scroll down and press enter I CAN still view one of the source files?

However when I return it is still empty.

The Solution

The solution in this case was surprising.

I trolled the internet for weeks until I finally found a nugget on old ye faithful - stackoverflow which mentioned that cscope has problems with control characters!

I noted when viewing the contents of several files there was a ^M appended to each line.

Therefore I ran dos2unix on all of the files in cscope.files

# cat cscope.files |xargs -n1 -I{}  dos2unix  {}
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/SraPeerArray.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/SraPrepareReverseReplicationResponse.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/RecommendationSpaceOutcome.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/RemoteCopyInfo.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/ReplLinkStat.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/ReplicationStat.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/RestApi.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/RestApiPrivilege.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/RestoreInfo.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/ServiceGroup.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/ServiceGroupOperationStatus.cs to Unix format ...
dos2unix: converting file PstkCore/dotnetSDK/src/SDK/Tintri.Types/Generated/Severity.cs to Unix format ...
....

After this was complete, I was able to run cscope normally and now I can see the search results!!!


Posted in Command Line FU, Linux | Leave a comment

What every Programmer *should* know about Memory

For my fellow Insomniacs out there…

If you are up scrolling your phone for things around the Google-verse to read to shut off your brain so you can possibly go to sleep.. I came across this article on LWN which provides a trough of information regarding Linux Memory Management.

The original document prints out at over 100 pages and is WAY more than I may ever digest in this lifetime.

Be sure to have a soft pillow for your head to fall down onto!!


Posted in Internals, Linux | Leave a comment

Obtain the grub boot parameters from /var/log/messages

There are times you may need to quickly confirm the grub boot parameters that were issued either at the console (or by default)

This information is logged to /var/log/messages when you grep with keywords "Kernel command"


# grep "Kernel command" messages
2023-09-19 2023-09-19T15:06:08.687 benvm1 NOT Kernel command line: BOOT_IMAGE=/vmlinuz-3.10.0-1160.76.1.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet, transparent_hugepage=never LANG=en_US.UTF-8
Posted in Linux | Leave a comment

Data Storage Funny #1

Let this sink in!!

Posted in Uncategorized | Leave a comment

Display ethernet devices on a Fedora system using the Linux Pseudo Filesystem and create a static IP Address

Encountered a fun riddle recently whereby on a Fedora 34 system I needed to locate all ethernet interfaces on a system that are obtaining a DHCP address, then after the first reboot create a static IP address.

I am a big fan of looking thru the Linux pseudo filesystem in /sys

In this instance I wanted to exclude any Virtual, Bridge and Local Interfaces.

Therefore I do the following:

Listing all Interfaces in sysfs

# ls /sys/class/net|egrep -v "^vir|^lo|^wl|^br"

Example

# ls /sys/class/net|egrep -v "^vir|^lo|^wl|^br"
eno1

PART 1 - CHANGE FROM DHCP TO STATIC IP ADDRESS

To change from DHCP to a STATIC IP Address

First display the current interfaces

# nmcli con show
NAME    UUID                                  TYPE      DEVICE
enp1s0  46145016-ead7-4128-8d91-4b0b828162db  ethernet  enp1s0

and to set a STATIC IP with NAME "System-NIC-110" the format would be

# nmcli con add con-name "System-NIC-110" ifname enp1s0 type ethernet ip4 192.168.122.110/24 gw4 192.168.122.1

Then you would delete the original interface afterwards

# nmcli con del enp1s0

Then either reboot or restart networking

# nmcli networking off
# nmcli networking on

Now you will see the new interface name.

# nmcli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  fc1ca250-1481-4014-82bd-ad97d1c48ae4  ethernet  enp1s0

PART II - Script method to change all interfaces after first reboot

Here is a script example how to iterate thru all devices on the system and create static IP addresses:

#!/bin/bash
###################################################################
# SCRIPT BY The Data Storage Guy (https://datastorageguy.com)
# As an example how iterate thru all ethernet devices on a Fedora
# system and assign Static IP Addresses with a custom name
#
# location: /usr/local/bin/set-static-ip.sh
###################################################################
# The following will go thru all of the ethernet devices found
#
# It is used to on create static IP addresses with connection NAME
#
# $connection_prefix-$starting_octet
#
# with IP address using the format
#
# $ip_subnet.$starting_octet
#
# It will also increment should there be more
# than one ethernet device found
#
# the parameters are used within the following configuration file
###################################################################
connection_prefix="System-NIC"
ip_subnet="192.168.122"
starting_octet="110"
mask="24"
gateway="192.168.122.1"
###################################################################
PS4='${LINENO}: '
###################################################################
SCRIPT=$(basename ${BASH_SOURCE[0]})
###################################################################
export PATH=$PATH:/usr/bin/
log=/var/log/$(echo $SCRIPT|cut -d. -f1).log
line="-----------------------------------------------"
step=1
n=1
###################################################################
logm() {
        LOGDATE=`date '+%Y-%m-%dT%H:%M:%S'`
        echo $line |tee -a $log
        echo "[$LOGDATE] [$step]  $1" |tee -a $log |tee >(logger -t $SCRIPT)
        ((step++))
}
###################################################################
logm "Displaying Network General Status"
logm "Issuing command: nmcli general status"
nmcli general status  >>$log

logm "Displaying Network Interfaces"
logm "Issuing command: nmcli con show"
nmcli con show |tee -a $log | logger -t $SCRIPT

while read ethname; do
        ip="${ip_subnet}.${starting_octet}"
        UUID=$(nmcli conn show |awk -v iface=$ethname '$NF ~ iface {print $(NF-2)}')
        logm "Setting $ethname ip address to ${ip}/24 with name "${connection_prefix}-$ip" for ethernet $ethname UUID=$UUID"
        logm "Issuing Command:  nmcli con add con-name "${connection_prefix}-$starting_octet" ifname $ethname type ethernet ip4 ${ip}/$mask gw4 $gateway"
        nmcli con add con-name "${connection_prefix}-$starting_octet" ifname $ethname type ethernet ip4 ${ip}/$mask gw4 $gateway  |tee -a $log
        if [ $? == 0 ]; then
                logm "Successfully added con-name ${connection_prefix}-$starting_octet"
        else
                logm "ERROR: Problem adding con-name ${connection_prefix}-$starting_octet"
        fi
        sleep .2
        logm "Deleting $ethname: nmcli con del $ethname"
        logm "Issuing command: nmcli con del $ethname"
        nmcli con del $ethname |tee -a $log
        if [ $? == 0 ]; then
                logm "Successfully deleted $ethname: nmcli con del $ethname"
        else
                logm "ERROR: Unable to delete $ethname: nmcli con del $ethname"
        fi
        ((n++))
        ((starting_octet++))
done< <(ls /sys/class/net|egrep -v "^vir|^lo|^wl|^br")

logm "Displaying Network Interfaces"
logm "Issuing command: nmcli con show"
nmcli con show |tee -a $log |logger -t $SCRIPT

logm "Checking for the install cron job entry"
logm "Issuing command: crontab -l"
crontab -l |tee -a $log|logger -t $SCRIPT

logm "Restarting Network Services"
logm "Issuing command: nmcli networking off"
nmcli networking off |tee -a $log
sleep .2
logm "Issuing command: nmcli networking on"
nmcli networking on |tee -a $log
sleep 2
logm "Displaying Network Interfaces"
logm "Issuing command: ncmli con show"
nmcli con show |tee -a $log |logger -t $SCRIPT

if [ $(nmcli con show|grep -q $connection_prefix;echo $?) == 0 ]; then
        logm "GOOD: Found $connection_prefix Network Device"
        crontab -l |tee -a $log
        if [ $? == 0 ]; then
                logm "Sleeping for 15 seconds... then removing cron job"
                sleep 15
                logm "Removing cron jobs"
                logm "Issuing command: crontab -r"
                crontab -r &>/dev/null
        fi
else
        logm "ERROR: Unable to find $connection_prefix Network device"
fi


logm "Completed Network Setting Setup"
###################################################################

Now we create a cron entry to run only at reboot and log to /root/cron.log

@reboot  /usr/bin/bash /usr/local/bin/set-static-ip.sh >> /root/cron.log

Now when we reboot we see the new network settings.

# nmcli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  62a904ab-2be2-4223-bc13-0b7daba2409e  ethernet  enp1s0
System-NIC-111  350008d7-f075-4ca5-ad97-f5ba8e6f20d0  ethernet  enp7s0

The /var/log/set-static-ip.log shows the following script steps

# cat /var/log/set-static-ip.log
-----------------------------------------------
[2023-09-11T13:28:20] [1]  Displaying Network General Status
-----------------------------------------------
[2023-09-11T13:28:20] [2]  Issuing command: nmcli general status
STATE                  CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
connected (site only)  limited       enabled  enabled  enabled  enabled
-----------------------------------------------
[2023-09-11T13:28:20] [3]  Displaying Network Interfaces
-----------------------------------------------
[2023-09-11T13:28:20] [4]  Issuing command: nmcli con show
NAME    UUID                                  TYPE      DEVICE
enp1s0  197a76a0-a28a-4db4-8e64-a90557e47c9f  ethernet  enp1s0
enp7s0  d0cf6083-8f35-4be1-8705-95ac804eecac  ethernet  enp7s0
-----------------------------------------------
[2023-09-11T13:28:20] [5]  Setting enp1s0 ip address to 192.168.122.110/24 with name System-NIC-192.168.122.110 for ethernet enp1s0 UUID=197a76a0-a28a-4db4-8e64-a90557e47c9f
-----------------------------------------------
[2023-09-11T13:28:20] [6]  Issuing Command:  nmcli con add con-name System-NIC-110 ifname enp1s0 type ethernet ip4 192.168.122.110/24 gw4 192.168.122.1
Connection 'System-NIC-110' (6bd11875-8971-41d0-9089-c3caebc02bb0) successfully added.
-----------------------------------------------
[2023-09-11T13:28:20] [7]  Successfully added con-name System-NIC-110
-----------------------------------------------
[2023-09-11T13:28:20] [8]  Deleting enp1s0: nmcli con del enp1s0
-----------------------------------------------
[2023-09-11T13:28:20] [9]  Issuing command: nmcli con del enp1s0
Connection 'enp1s0' (197a76a0-a28a-4db4-8e64-a90557e47c9f) successfully deleted.
-----------------------------------------------
[2023-09-11T13:28:20] [10]  Successfully deleted enp1s0: nmcli con del enp1s0
-----------------------------------------------
[2023-09-11T13:28:20] [11]  Setting enp7s0 ip address to 192.168.122.111/24 with name System-NIC-192.168.122.111 for ethernet enp7s0 UUID=d0cf6083-8f35-4be1-8705-95ac804eecac
-----------------------------------------------
[2023-09-11T13:28:20] [12]  Issuing Command:  nmcli con add con-name System-NIC-111 ifname enp7s0 type ethernet ip4 192.168.122.111/24 gw4 192.168.122.1
Connection 'System-NIC-111' (8eee96f7-8a42-4bb4-b57a-e15d222743f1) successfully added.
-----------------------------------------------
[2023-09-11T13:28:20] [13]  Successfully added con-name System-NIC-111
-----------------------------------------------
[2023-09-11T13:28:20] [14]  Deleting enp7s0: nmcli con del enp7s0
-----------------------------------------------
[2023-09-11T13:28:20] [15]  Issuing command: nmcli con del enp7s0
Connection 'enp7s0' (d0cf6083-8f35-4be1-8705-95ac804eecac) successfully deleted.
-----------------------------------------------
[2023-09-11T13:28:20] [16]  Successfully deleted enp7s0: nmcli con del enp7s0
-----------------------------------------------
[2023-09-11T13:28:20] [17]  Displaying Network Interfaces
-----------------------------------------------
[2023-09-11T13:28:20] [18]  Issuing command: nmcli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  6bd11875-8971-41d0-9089-c3caebc02bb0  ethernet  enp1s0
System-NIC-111  8eee96f7-8a42-4bb4-b57a-e15d222743f1  ethernet  enp7s0
virbr0          02b8c655-36de-4379-b096-b48de5941117  bridge    virbr0
-----------------------------------------------
[2023-09-11T13:28:21] [19]  Checking for the install cron job entry
-----------------------------------------------
[2023-09-11T13:28:21] [20]  Issuing command: crontab -l
@reboot  /usr/bin/bash /usr/local/bin/set-static-ip.sh >> /root/cron.log
-----------------------------------------------
[2023-09-11T13:28:21] [21]  Restarting Network Services
-----------------------------------------------
[2023-09-11T13:28:21] [22]  Issuing command: nmcli networking off
-----------------------------------------------
[2023-09-11T13:28:21] [23]  Issuing command: nmcli networking on
-----------------------------------------------
[2023-09-11T13:28:21] [24]  Displaying Network Interfaces
-----------------------------------------------
[2023-09-11T13:28:21] [25]  Issuing command: ncmli con show
NAME            UUID                                  TYPE      DEVICE
System-NIC-110  6bd11875-8971-41d0-9089-c3caebc02bb0  ethernet  enp1s0
System-NIC-111  8eee96f7-8a42-4bb4-b57a-e15d222743f1  ethernet  enps7
-----------------------------------------------
[2023-09-11T13:28:21] [26]  GOOD: Found System-NIC Network Device
@reboot  /usr/bin/bash /usr/local/bin/set-static-ip.sh >> /root/cron.log
-----------------------------------------------
[2023-09-11T13:28:21] [27]  Sleeping for 15 seconds... then removing cron job
-----------------------------------------------
[2023-09-11T13:28:36] [28]  Removing cron jobs
-----------------------------------------------
[2023-09-11T13:28:36] [29]  Issuing command: crontab -r
-----------------------------------------------
[2023-09-11T13:28:36] [30]  Completed Network Setting Setup
Posted in Command Line FU, Linux, Networking | Tagged , , | Leave a comment

Modify gnome dock from the command line

To modify the gnome desktop dock (i.e. launcher or launchpad) from the command line an easy way is to perform the following:

/usr/bin/dbus-launch dconf write /org/gnome/shell/favorite-apps "['firefox.desktop', 'org.gnome.Nautilus.desktop', 'org.gnome.Terminal.desktop', 'terminator.desktop', 'gnome-control-center.desktop']"

This above will add Firefox, The File Manager, Terminal, Terminator and Gnome Settings to the desktop

Posted in Linux | Leave a comment