Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, 14 August 2020

Generate .pem file from .ppk file

 Introduction

I recently came across a requirement from our DBA Team wherein they needed to access a server on Oracle Cloud and all they had was a .ppk file received from the App Team that managed the OCI server. Since the source server from which we had to connect did not have a GUI, we needed to generate a corresponding .pem file to use on the command line.

For this, I installed putty on a lab machine.


yum install putty


 I then used the .ppk file to generate a .pem file using the following command:


puttygen oci.ppk -O private-openssh -o oci.pem


This step did prompt me for the passphrase for the .ppk file which I was are of.

To login to the OCI server, I used .pem file by specifying the -i option with the ssh command as follows.


-bash-4.1$ ssh -i oci.pem opc@11.59.17.209

Enter passphrase for key 'oci.pem':

Last login: Thu Aug 13 07:17:30 UTC 2020 from 10.22.88.26 on pts/0

Last login: Thu Aug 13 07:18:17 2020 from 10.22.88.26


The passphrase was the same one I was prompted for while generating the .pem file.


Conclusion

This concludes this post on how to generate a .pem file from a .ppk file. I hope you found the post to be useful.

Tuesday, 19 February 2019

Troubleshooting disk addition in multipath configuration in Linux


Introduction
Let me start by saying that the issue in this scenario was actually the result of a human error but I would still like to share the setup and steps I followed that finally helped me understand the problem and eventually resolve it.

The scenario
We received a new LUN of size 64GB from our storage team and it was to be scanned on the server, added to the multipath configuration and subsequently be used within LVM.

Step 1: Detect new disk
I executed the scsi-rescan -r and rescan-scsi-bus.sh commands so that the disk gets detected by the OS. I then used a script to get the disk device name to get the disk WWID mapping. The was basically a wrapper around the scsi_id command. For example:

[ssuri@example:~] $ sudo scsi_id -g -u -d /dev/sdamd
36005076801810718a800000000002a3b
[ssuri@example:~] $


Step 2: Add disk to udev and multipath
The disk was intended to be used for the /grid file system on a DB node. So I added the following lines in the /etc/udev/rules.d/99-dell-oracle-asm.rules file

KERNEL=="dm-*", PROGRAM="scsi_id --page=0x83 --whitelisted --device=/dev/%k", RESULT=="36005076801810718a800000000002c12", OWNER="root", GROUP="root"
ENV{DM_NAME}=="grid_pv1", OWNER="root", GROUP="root"

I wanted the disk alias name to be grid_pv1 and so I proceeded to add the corresponding entry in the /etc/multipath.conf file. I first added a line for the disk WWID in the blacklist_exceptions stanza.

wwid "36005076801810718a800000000002c12"

I then added the below stanza for the grid_pv1 disk in the multipaths stanza.

        multipath {
                uid 0
                gid 0
                wwid "36005076801810718a800000000002c12"
                alias grid_pv1
                mode 0600
        }

I then reloaded the required services with the following commands:

/sbin/service multipathd reload
/sbin/udevadm control --reload-rules
/sbin/udevadm trigger --type=subsystems --action=change
/sbin/udevadm trigger --type=devices --action=change


Step 3: Check that disk is visible in multipath configuration
After doing all the above, to my astonishment I could not see the disk in the multipath -ll output. I frantically rescanned the disk again, removed and added the udev and multipath configuration and reloaded the services numerous times. What finally caught my eye was when I saw that the disk was showing up as being blacklisted in the multipath -v3 command output.

 [ssuri@example:/var/ ] $ sudo multipath -v3 | grep -i 36005076801810718A800000000002c12
 Feb 16 05:56:30 | sdbgs: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgt: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgu: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgv: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgw: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgx: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgy: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted
 Feb 16 05:56:30 | sdbgz: uid = 36005076801810718a800000000002c12 (callout)
 Feb 16 05:56:30 | (null): (36005076801810718a800000000002c12) wwid blacklisted

In our multipath configuration we have a blanket wwid '*' as a blacklist rule. On seeing the above output I finally realized my mistake that the WWID entry I had added in udev and multipath configuration files actually had the alphabets 'a' and 'c' capitalized which was causing the disk WWID to still show up as blacklisted. 
On this realization I immediately corrected my mistake and restarted the services and rest assured the disk was now available in the multipath -ll output.

[ssuri@example:~] $ sudo multipath -ll grid_pv1
grid_pv1 (36005076801810718a800000000002c12) dm-384 IBM,2145
size=64G features='1 queue_if_no_path' hwhandler='0' wp=rw
`-+- policy='round-robin 0' prio=1 status=active
  |- 4:0:2:194 sdep  129:16   active ready running
  |- 6:0:0:194 sdaev 67:944   active ready running
  |- 5:0:0:194 sdpv  131:336  active ready running
  |- 7:0:3:194 sdazl 69:1328  active ready running
  |- 4:0:3:194 sdgl  132:16   active ready running
  |- 6:0:1:194 sdagr 70:944   active ready running
  |- 5:0:2:194 sdto  65:608   active ready running
  `- 7:0:7:194 sdbgz 65:1648  active ready running
[ssuri@example:~] $


Conclusion:
Although this issue was the result of a typing issue I do hope this helps you in diagnosing multipath related issues that you might be facing.

Saturday, 24 November 2018

Configuring chrony time server in Centos 7

In this quick article we'll demonstrate how to configure the chrony time service in Linux. Configuring chrony is an exam objective for the RedHat as well as Linux Foundation exams.
Chrony is touted as an alternative for the popular ntpd daemon that has been used in UNIX/Linux systems for years now. Chrony has become more popular in desktop environments since time synchronization using chrony is very quick making it ideal for systems that get restarted frequently.

To install chrony, type the following command:

[root@cent7~]# yum install chrony
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                                          | 7.7 kB  00:00:00
 * base: centos.mirror.myduniahost.com
 * epel: mirrors.aliyun.com
 * extras: mirror.0x.sg
 * updates: mirror.0x.sg
base                                                                                                                                          | 3.6 kB  00:00:00
epel                                                                                                                                          | 3.2 kB  00:00:00
extras                                                                                                                                        | 3.4 kB  00:00:00
updates                                                                                                                                       | 3.4 kB  00:00:00
(1/2): epel/x86_64/updateinfo                                                                                                                 | 932 kB  00:00:01
(2/2): epel/x86_64/primary                                                                                                                    | 3.6 MB  00:00:14
epel                                                                                                                                                     12716/12716
Package chrony-3.2-2.el7.x86_64 already installed and latest version
Nothing to do

Apparently it's already installed on my system.

Let's take a look at the available time servers in the chrony configuration file /etc/chrony.conf.

[root@cent7~]# grep -E '^server' /etc/chrony.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
[root@cent7~]#

Now we'll enable and start the service.

[root@cent7 ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@cent7 ~]# systemctl start chronyd

Let's check the status of the service now:

[root@cent7 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-11-24 11:53:55 IST; 5s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1796 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1792 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1794 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─1794 /usr/sbin/chronyd

Nov 24 11:53:55 cent7 systemd[1]: Starting NTP client/server...
Nov 24 11:53:55 cent7 chronyd[1794]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH +SIGND +ASYNCDNS +IPV6 +DEBUG)
Nov 24 11:53:55 cent7 systemd[1]: Started NTP client/server.

To obtain information about the time server we are currently syncing our time from, type the following command:

[root@cent7 ~]# chronyc tracking
Reference ID    : 768CB862 (sr-97-184-140-128-on-nets.com)
Stratum         : 3
Ref time (UTC)  : Sat Nov 24 06:24:05 2018
System time     : 0.000006578 seconds slow of NTP time
Last offset     : +0.007599837 seconds
RMS offset      : 0.007599837 seconds
Frequency       : 0.000 ppm slow
Residual freq   : +3831.410 ppm
Skew            : 1000000.000 ppm
Root delay      : 0.178650528 seconds
Root dispersion : 39.540195465 seconds
Update interval : 1.6 seconds
Leap status     : Normal

From the above output we can determine that we are currently syncing time from the server sr-97-184-140-128-on-nets.com.

To view the list of time servers available to us to sync from type the following command:

[root@cent7~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^- ntp.fra.de.as206479.net       2   6    37    53    +36ms[  +36ms] +/-  252ms
^* hkg1.m-d.net                  2   6    37    54  -1579us[  -17ms] +/-  117ms
^? 30-213-226-103-static.ch>     1   6     1    54  +1364us[  -14ms] +/-   93ms
^+ sr-97-184-140-128-on-net>     2   6    37    54  +1105us[+1105us] +/-  122ms

The * symbol indicates the time server we are currently syncing from.

I hope that you found this quick and easy setup of chrony to be useful.

Tuesday, 13 November 2018

host command as an alternative to nslookup

The nslookup command is the go to command for performing hostname/IP address lookups. It's a standard command found in Linux, UNIX and Windows distributions alike. While writing scripts we may come across requirements wherein we need the hostname and IP address information to be extracted from the output of the command.

If we use nslookup for this purpose then we would need to employ some interesting awk or grep commands to get the desired result. Given below is an example:

[root@lpic ~]# nslookup google.com
Server:         192.168.178.1
Address:        192.168.178.1#53

Non-authoritative answer:
Name:   google.com
Address: 172.217.24.238

[root@lpic ~]# nslookup google.com | awk '/Address/ && !/#/ {print $2}'
172.217.160.206
[root@lpic ~]#

On the contrary now let's talk about the host command. host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. But I found it's output easier to read and extract desired fields from it. Here's an example:

[root@lpic ~]# host google.com
google.com has address 172.217.24.238
google.com has IPv6 address 2404:6800:4002:803::200e
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
[root@lpic ~]#
[root@lpic ~]# host google.com | awk '/has address/ {print $4}'
172.217.24.238
[root@lpic ~]#

I hope this quick tip has been helpful for you.

Saturday, 14 October 2017

Workaround for "Error getting private key" error while starting realvnc on Linux

Recently we received a user complaint that they were unable to access a server through vncviewer.
When I checked I found that the service was not running and when I attempted to start the service I got the below message:

[root@pbox bin]# /etc/init.d/vncserver start
Starting VNC server: 1:vncuser xhost:  unable to open display ""
chmod: cannot access `/tmp/.Xauthority-vncuser': No such file or directory
VNC(R) Server 5.3.2 (r19179) x64 (Jun 6 2016 19:59:17)
Copyright (C) 2002-2016 RealVNC Ltd.
RealVNC and VNC are trademarks of RealVNC Ltd and are protected by trademark
registrations and/or pending trademark applications in the European Union,
United States of America and other jurisdictions.
Protected by UK patent 2481870; US patent 8760366.
See http://www.realvnc.com for information on VNC.
For third party acknowledgements see:
http://www.realvnc.com/products/vnc/documentation/5.3/acknowledgements.txt

Error getting private key from /var/home/vncuser/.vnc/private.key: End of stream
Underlying X server release 609000, The X.Org Foundation

error opening security policy file /usr/X11R6/lib/X11/xserver/SecurityPolicy
Could not init font path element /usr/X11R6/lib/X11/fonts/misc/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/TTF/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/Type1/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/CID/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/75dpi/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/100dpi/, removing from list!
FreeFontPath: FPE "/usr/share/vnc/fonts/" refcount is 2, should be 1; fixing.
2:vncuser xhost:  unable to open display ""
chmod: cannot access `/tmp/.Xauthority-vncuser': No such file or directory
VNC(R) Server 5.3.2 (r19179) x64 (Jun 6 2016 19:59:17)
Copyright (C) 2002-2016 RealVNC Ltd.
RealVNC and VNC are trademarks of RealVNC Ltd and are protected by trademark
registrations and/or pending trademark applications in the European Union,
United States of America and other jurisdictions.
Protected by UK patent 2481870; US patent 8760366.
See http://www.realvnc.com for information on VNC.
For third party acknowledgements see:
http://www.realvnc.com/products/vnc/documentation/5.3/acknowledgements.txt

Error getting private key from /var/home/vncuser/.vnc/private.key: End of stream
Underlying X server release 609000, The X.Org Foundation

error opening security policy file /usr/X11R6/lib/X11/xserver/SecurityPolicy
Could not init font path element /usr/X11R6/lib/X11/fonts/misc/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/TTF/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/Type1/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/CID/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/75dpi/, removing from list!
Could not init font path element /usr/X11R6/lib/X11/fonts/100dpi/, removing from list!
FreeFontPath: FPE "/usr/share/vnc/fonts/" refcount is 2, should be 1; fixing.
                                                           [  OK  ]


I wasn't able to query the status of the service either:

[root@pbox init.d]# service vncserver status
Xvnc dead but subsys locked

This error pointed me to the /var/lock/subsys/Xvnc file which I removed and then restarted the service but it did not work.

If I attempted to stop the service then that also did not succeed in the first attempt.

[root@pbox ~]# service vncserver stop
Shutting down VNC server: 1:vncuser 2:vncuser              [FAILED]

Although it did work the second time I tried.

[root@pbox subsys]# service vncserver stop
Shutting down VNC server:                                  [  OK  ]



When I checked the /var/home/vncuser/.vnc/private.key I found it to be empty.

[root@pbox .vnc]# ls -l private.key
-rw------- 1 vncuser vncuser 0 Oct  5 04:12 private.key

I restarted the vnc service multiple times and even installed realvnc again but that did not work. According to the documentation I found on realvnc, the private key should've been regenerated after a restart of the service or created at least when I re-installed the software but that did not happen.

I finally ended up copying the private key from a server on which realvnc was already running and started the service on the problematic server and it finally worked.

[root@pbox ~]# service vncserver restart
Shutting down VNC server: 1:vncuser 2:vncuser              [  OK  ]
Starting VNC server: 1:vncuser xhost:  unable to open display ""
chmod: cannot access `/tmp/.Xauthority-vncuser': No such file or directory
VNC(R) Server 5.3.2 (r19179) x64 (Jun 6 2016 19:59:17)
Copyright (C) 2002-2016 RealVNC Ltd.
RealVNC and VNC are trademarks of RealVNC Ltd and are protected by trademark
registrations and/or pending trademark applications in the European Union,
United States of America and other jurisdictions.
Protected by UK patent 2481870; US patent 8760366.
See http://www.realvnc.com for information on VNC.
For third party acknowledgements see:
http://www.realvnc.com/products/vnc/documentation/5.3/acknowledgements.txt

If a desktop environment fails to load for this virtual desktop, please see:
 http://www.realvnc.com/doclink/kb-345?version=5.3.2.19179
Running applications in /var/home/vncuser/.vnc/xstartup

VNC Server catchphrase: "Member barcode connect. Desire college gong."
             signature: 88-c7-cb-1a-2c-9b-90-31

Log file is /var/home/vncuser/.vnc/pbox.dev.test.org:1.log
New desktop is pbox.dev.test.org:1 (10.22.217.69:1)
2:vncuser xhost:  unable to open display ""
chmod: cannot access `/tmp/.Xauthority-vncuser': No such file or directory
VNC(R) Server 5.3.2 (r19179) x64 (Jun 6 2016 19:59:17)
Copyright (C) 2002-2016 RealVNC Ltd.
RealVNC and VNC are trademarks of RealVNC Ltd and are protected by trademark
registrations and/or pending trademark applications in the European Union,
United States of America and other jurisdictions.
Protected by UK patent 2481870; US patent 8760366.
See http://www.realvnc.com for information on VNC.
For third party acknowledgements see:
http://www.realvnc.com/products/vnc/documentation/5.3/acknowledgements.txt

If a desktop environment fails to load for this virtual desktop, please see:
 http://www.realvnc.com/doclink/kb-345?version=5.3.2.19179
Running applications in /var/home/vncuser/.vnc/xstartup

VNC Server catchphrase: "Member barcode connect. Desire college gong."
             signature: 88-c7-cb-1a-2c-9b-90-31

Log file is /var/home/vncuser/.vnc/pbox.dev.test.org:2.log
New desktop is pbox.dev.test.org:2 (10.22.217.69:2)
                                                           [  OK  ]
[root@pbox ~]#
[root@pbox ~]#
[root@pbox ~]# ps -ef | grep [v]nc
vncuser   49576      1  0 02:05 ?        00:00:00 /usr/bin/Xvnc-core :1 -auth /var/home/vncuser/.Xauthority -pn -geometry 800x600 -nolisten tcp
root      49577  49576  0 02:05 ?        00:00:00 /usr/bin/Xvnc-realvnc -rootHelper 816219 4
vncuser   49608      1  0 02:05 ?        00:00:00 /bin/sh /etc/vnc/xstartup
vncuser   49630  49608  0 02:05 ?        00:00:00 xterm -geometry 80x24+10+10 -ls
vncuser   49632  49608  0 02:05 ?        00:00:00 twm
vncuser   49654  49630  0 02:05 pts/2    00:00:00 -bash
vncuser   49667      1  0 02:05 ?        00:00:00 /usr/bin/Xvnc-core :2 -auth /var/home/vncuser/.Xauthority -pn -geometry 1600x1200 -nolisten tcp
root      49671  49667  0 02:05 ?        00:00:00 /usr/bin/Xvnc-realvnc -rootHelper 816219 4
vncuser   49711      1  0 02:05 ?        00:00:00 /bin/sh /etc/vnc/xstartup
vncuser   49731  49711  0 02:05 ?        00:00:00 xterm -geometry 80x24+10+10 -ls
vncuser   49732  49711  0 02:05 ?        00:00:00 twm
vncuser   49734  49731  0 02:05 pts/3    00:00:00 -bash
vncuser   49753  49576  0 02:05 ?        00:00:00 /usr/bin/vncserverui virtual 13
vncuser   49799  49753  0 02:05 ?        00:00:00 /usr/bin/vncserverui -statusicon 5
vncuser   49800  49667  0 02:05 ?        00:00:00 /usr/bin/vncserverui virtual 13
vncuser   49822  49800  0 02:05 ?        00:00:00 /usr/bin/vncserverui -statusicon 5


This is definitely not ideal but a quick fix just to keep things going.

Sunday, 8 October 2017

Fixing NTP sync issues in RHEL 6

In this article I'll be exploring two distinct issues I faced with ntp sync wherein the servers were not able to properly sync their time with ntp servers.

We observed a high offset when we checked the ntpq -p output.

I did a couple of stop-start operations followed by troubleshooting procedure outlined in RedHat KB articles 35640 and 64868 whose logs I'm sharing below:

[ssuri@usporiainfrar00:~]' $ sudo ntpq
ntpq> peers
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+ntp.nova.org   .GPS.            1 u   43   64  377    0.189  309.823 175.931
*mtntime.emrets. .GPS.            1 u   44   64  377   13.401  309.833 174.006
xusporz-infrac15 10.16.64.15      3 u   43   64  377    0.208  286.078 169.984
xgblonz-infrac07 10.16.64.15      3 u   39   64  377   91.365  287.473 167.180
ntpq> as

ind assid status  conf reach auth condition  last_event cnt
===========================================================
  1 57822  941a   yes   yes  none candidate    sys_peer  1
  2 57823  961a   yes   yes  none  sys.peer    sys_peer  1
  3 57824  9124   yes   yes  none falsetick   reachable  2
  4 57825  9124   yes   yes  none falsetick   reachable  2
ntpq> rv 57822
associd=57822 status=941a conf, reach, sel_candidate, 1 event, sys_peer,
srcadr=ntp.nova.org, srcport=123, dstadr=10.16.216.184, dstport=123,
leap=00, stratum=1, precision=-20, rootdelay=0.000, rootdisp=0.290,
refid=GPS, reftime=dd6328c0.a7684541  Wed, Sep 13 2017  3:47:12.653,
rec=dd6328c3.fc501541  Wed, Sep 13 2017  3:47:15.985, reach=377,
unreach=0, hmode=3, pmode=4, hpoll=6, ppoll=6, headway=29, flash=00 ok,
keyid=0, offset=504.129, delay=0.171, dispersion=1.525, jitter=183.751,
xleave=0.028,
filtdelay=     0.19    0.17    0.19    0.22    0.21    0.19    0.20    0.21,
filtoffset=  553.93  504.13  454.51  405.18  356.99  309.82  250.70  191.10,
filtdisp=      0.00    0.96    1.94    2.93    3.92    4.91    5.88    6.87
ntpq> q

[ssuri@usporiainfrar00:~]' $ sudo ntpdate -u ntp.nova.org
13 Sep 03:51:50 ntpdate[87098]: step time server 10.16.64.124 offset 0.757074 sec
[ssuri@usporiainfrar00:~]' $ sudo ntpdate -d ntp.nova.org
13 Sep 03:51:59 ntpdate[87462]: ntpdate 4.2.6p5@1.2349-o' Tue May  3 15:12:51 UTC 2016 (1)
Looking for host ntp.nova.org and service ntp
host found : ntp.nova.org
transmit(10.16.64.124)
receive(10.16.64.124)
transmit(10.16.64.124)
receive(10.16.64.124)
transmit(10.16.64.124)
receive(10.16.64.124)
transmit(10.16.64.124)
receive(10.16.64.124)
server 10.16.64.124, port 123
stratum 1, precision -20, leap 00, trust 000
refid [GPS], delay 0.02580, dispersion 0.00000
transmitted 4, in filter 4
reference time:    dd6329d6.e0896c6a  Wed, Sep 13 2017  3:51:50.877
originate timestamp: dd6329df.4e7f4739  Wed, Sep 13 2017  3:51:59.306
transmit timestamp:  dd6329df.4d0d781a  Wed, Sep 13 2017  3:51:59.300
filter delay:  0.02585  0.02580  0.02580  0.02585
         0.00000  0.00000  0.00000  0.00000
filter offset: 0.005469 0.005474 0.005467 0.005453
         0.000000 0.000000 0.000000 0.000000
delay 0.02580, dispersion 0.00000
offset 0.005474

13 Sep 03:51:59 ntpdate[87462]: adjust time server 10.16.64.124 offset 0.005474 sec


We also did some comparisons with the system hardware clock and system time set by ntp by running the following command snippet:

 s(){ printf "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";}; s; printf '`date`:\t '; date; printf '`sudo hwclock`: ';sudo hwclock; s; printf '`sudo ntpq -pn`:\n\n'; sudo ntpq -pn; s; printf '`sudo ntpq -c as`:\n'; sudo ntpq -c as; for id in $(sudo ntpq -c as | awk '/^ ./{print $2}'); do s; printf "\`sudo ntpq -c \"rv $id\"\`:\n\n"; sudo ntpq -c "rv $id"; done

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `date`: Wed Sep 13 05:23:47 UTC 2017 `sudo hwclock`: Wed 13 Sep 2017 05:23:48 AM UTC -0.343942 seconds ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `sudo ntpq -pn`: remote refid st t when poll reach delay offset jitter ============================================================================== *10.16.64.124 .GPS. 1 u 14 64 377 0.160 428.657 142.242 +10.20.64.124 .GPS. 1 u 40 64 377 13.456 416.054 136.215 +10.16.64.11 10.16.64.15 3 u 30 64 377 0.214 411.056 141.797 +10.24.64.11 10.16.64.15 3 u 28 64 377 91.317 419.916 141.874 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `sudo ntpq -c as`: ind assid status conf reach auth condition last_event cnt =========================================================== 1 33492 961a yes yes none sys.peer sys_peer 1 2 33493 943a yes yes none candidate sys_peer 3 3 33494 9414 yes yes none candidate reachable 1 4 33495 9414 yes yes none candidate reachable 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `sudo ntpq -c "rv 33492"`: associd=33492 status=961a conf, reach, sel_sys.peer, 1 event, sys_peer, srcadr=ntp.emrsn.org, srcport=123, dstadr=10.16.216.184, dstport=123, leap=00, stratum=1, precision=-20, rootdelay=0.000, rootdisp=0.305, refid=GPS, reftime=dd633f51.48ffa52c Wed, Sep 13 2017 5:23:29.285, rec=dd633f55.f6c4f89f Wed, Sep 13 2017 5:23:33.963, reach=377, unreach=0, hmode=3, pmode=4, hpoll=6, ppoll=6, headway=50, flash=00 ok, keyid=0, offset=428.657, delay=0.160, dispersion=0.980, jitter=142.242, xleave=0.033, filtdelay= 0.16 0.20 0.18 0.15 0.25 0.23 0.19 0.20, filtoffset= 428.66 397.34 365.10 333.82 302.01 269.84 237.60 205.38, filtdisp= 0.00 1.01 2.04 3.05 4.07 5.10 6.14 7.17 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `sudo ntpq -c "rv 33493"`: associd=33493 status=943a conf, reach, sel_candidate, 3 events, sys_peer, srcadr=mtntime.emrets.net, srcport=123, dstadr=10.16.216.184, dstport=123, leap=00, stratum=1, precision=-20, rootdelay=0.000, rootdisp=0.458, refid=GPS, reftime=dd633f2b.eeaa5a6e Wed, Sep 13 2017 5:22:51.932, rec=dd633f3b.031f6da1 Wed, Sep 13 2017 5:23:07.012, reach=377, unreach=0, hmode=3, pmode=4, hpoll=6, ppoll=6, headway=21, flash=00 ok, keyid=0, offset=416.054, delay=13.456, dispersion=0.947, jitter=136.215, xleave=0.026, filtdelay= 13.46 13.42 13.43 13.45 13.42 13.50 13.50 13.47, filtoffset= 416.05 385.25 355.33 324.97 294.15 264.31 232.99 202.63, filtdisp= 0.00 0.99 1.95 2.93 3.92 4.88 5.88 6.86 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `sudo ntpq -c "rv 33494"`: associd=33494 status=9414 conf, reach, sel_candidate, 1 event, reachable, srcadr=usstlz-pinfdc15.emrsn.org, srcport=123, dstadr=10.16.216.184, dstport=123, leap=00, stratum=3, precision=-6, rootdelay=62.500, rootdisp=121.170, refid=10.16.64.15, reftime=dd633ccb.357101ca Wed, Sep 13 2017 5:12:43.208, rec=dd633f45.f8454467 Wed, Sep 13 2017 5:23:17.969, reach=377, unreach=0, hmode=3, pmode=4, hpoll=6, ppoll=6, headway=32, flash=00 ok, keyid=0, offset=411.056, delay=0.214, dispersion=16.513, jitter=141.797, xleave=0.018, filtdelay= 0.21 0.27 0.27 0.22 0.23 0.26 0.27 0.26, filtoffset= 411.06 377.74 348.50 307.74 276.47 246.26 226.04 197.64, filtdisp= 15.63 16.60 17.61 18.57 19.57 20.58 21.57 22.54 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `sudo ntpq -c "rv 33495"`: associd=33495 status=9414 conf, reach, sel_candidate, 1 event, reachable, srcadr=gblonz-pinfdc07.emrsn.org, srcport=123, dstadr=10.16.216.184, dstport=123, leap=00, stratum=3, precision=-6, rootdelay=109.375, rootdisp=122.223, refid=10.16.64.15, reftime=dd633c4d.a4ae07e5 Wed, Sep 13 2017 5:10:37.643, rec=dd633f47.0cce5653 Wed, Sep 13 2017 5:23:19.050, reach=377, unreach=0, hmode=3, pmode=4, hpoll=6, ppoll=6, headway=33, flash=00 ok, keyid=0, offset=419.916, delay=91.317, dispersion=16.524, jitter=141.874, xleave=0.010, filtdelay= 91.32 91.61 91.40 91.13 91.36 92.86 91.49 91.38, filtoffset= 419.92 388.51 354.44 316.21 298.64 270.20 226.80 195.30, filtdisp= 15.63 16.62 17.62 18.61 19.60 20.58 21.58 22.57


 We disabled slew mode for ntp as shown below by updating the OPTIONS parameter in etc/sysconfig/ntpd  file:

# grep -i ntpd ps ntp 1082 0.0 0.0 26520 1980 ? Ss 03:09 0:00 ntpd -x -u ntp:ntp -p /var/run/ntpd.pid -g -4 

 # grep -v "^#" /etc/sysconfig/ntpd 
OPTIONS="-x -u ntp:ntp -p /var/run/ntpd.pid -g -4" 

After that we restarted the service:

# ps -ef | grep -E ^ntp 
ntp 59539 1 0 04:48 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g -4 


After this we did a stop and start of the service a few times and a manual update with the ntp server to finally fix the high offset problem.

sudo ntpdate -u ntp.nova.org 
13 Sep 02:20:49 ntpdate[19667]: step time server 10.16.64.124 offset -42.674761 sec 

Just a note that apart from the troubleshooting we did here to fix the high offset, this problem could also be caused by network latency so that is worth checking out.


In the second scenario we were again facing high offsets on one of our linux servers but in this case the time displayed by the hardware clock was accurate.

We did a few stop, start and restart operations along with a manual update with the ntp server but with no success.

[ssuri@usporinfradr00:~] $ sudo nptdate -u ntpls.nova.org
  7 Oct 09:13:38 ntpdate[14262]: step time server 10.20.64.124 offset -173.793480 sec
 [ssuri@usporinfradr00:~] $ sudo ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
 *ntp.nova.org   .GPS.            1 u 4971   64    1    0.087  -173811   0.000
  10.16.64.124    .INIT.          16 u    -   64    0    0.000    0.000   0.000
  usporz-infrac21 10.16.64.15      3 u 4971   64    1    0.183  -173819   0.000
  gblonz-infrac07 10.16.64.15      3 u 4971   64    1  101.189  -173806   0.000


The system hardware clock was accurate though.

[ssuri@usporinfradr00:~] $ sudo hwclock -r
 Sat 07 Oct 2017 09:18:34 AM UTC  -0.485089 seconds
 [ssuri@usporinfradr00:~] $ date
 Sat Oct  7 09:16:05 UTC 2017


We then tried to sync the system and hardware clocks by executing hwclock -s but to our surprise now the hardware clock was also out of sync.

[ssuri@usporinfradr00:~] $ sudo hwclock -s
 [ssuri@usporinfradr00:~] $ date
 Sat Oct  7 09:20:18 UTC 2017

[root@usporinfradr00 ~]# hwclock -r
 Sat 07 Oct 2017 09:20:38 AM UTC  -0.265782 seconds

We did a manual update with our ntp server again followed by a restart of the service.

[root@usporinfradr00 ~]# sudo ntpdate -u ntpls.nova.org
  7 Oct 09:18:05 ntpdate[88723]: step time server 10.20.64.124 offset -173.991557 sec
 [root@usporinfradr00 ~]# sudo ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
  ntpserver.nova .GPS.            1 u 4971   64   37    0.076   82.388 123032.
  10.16.64.124    .INIT.          16 u    -   64    0    0.000    0.000   0.000
  usporz-infrac21 10.16.64.15      3 u 4971   64   37    0.161   70.839 123031.
  gblonz-infrac07 10.16.64.15      3 u 4971   64   37  101.189  -173806 150569.

 [root@usporinfradr00 ~]# sudo ntpq -pservice ntpd restart
 Shutting down ntpd: [  OK  ]

 Starting ntpd: [  OK  ]


This finally corrected the offset and brought the system time in sync with the hardware clock as well.

[root@usporinfradr00 ~]# date
 Sat Oct  7 09:18:56 UTC 2017
 [root@usporinfradr00 ~]#  hwclock -r
 Sat 07 Oct 2017 09:19:26 AM UTC  -0.031508 seconds
 [root@usporinfradr00 ~]# date
 Sat Oct  7 09:19:30 UTC 2017
 [root@usporinfradr00 ~]# sudo ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
 *ntpls.nova.org .GPS.            1 u   14   64    3    0.081    9.357  32.137
  10.16.64.124    .INIT.          16 u    -   64    0    0.000    0.000   0.000
  usporz-infrac21 10.16.64.15      3 u    9   64    3    0.170   10.491  32.264
  gblonz-infrac07 10.16.64.15      3 u   10   64    3  101.359   22.452  32.468
 [root@usporinfradr00 ~]# 
 [root@usporinfradr00 ~]# date
 Sat Oct  7 09:20:21 UTC 2017

Saturday, 26 August 2017

Converting text on CLI to images using ImageMagick



In this article I'll demonstrate how we can convert the output of commands we type on the Linux command line to images using ImageMagick software. This software allows users to create and modify images. It's generally meant for use in an X windows environment but we'll be using it purely on the command line for the purposes of this article.

The ImageMagick software is not installed on Linux distributions by default but is available in the base repo so you don't have to search around for it.

Let's begin by installing the package.

[root@walk ~]# yum search ImageMagick
Loaded plugins: fastestmirror, security
Determining fastest mirrors
epel/metalink                                                                                                | 5.7 kB     00:00
 * base: ftp.iitm.ac.in
 * epel: ftp.yz.yamagata-u.ac.jp
 * extras: ftp.iitm.ac.in
 * jpackage-generic: sunsite.informatik.rwth-aachen.de
 * updates: ftp.iitm.ac.in
base                                                                                                         | 3.7 kB     00:00
epel                                                                                                         | 4.3 kB     00:00
epel/primary_db                                                                                              | 5.9 MB     01:15
extras                                                                                                       | 3.4 kB     00:00
jpackage-generic                                                                                             | 1.9 kB     00:00
spacewalk                                                                                                    | 2.1 kB     00:00
updates                                                                                                      | 3.4 kB     00:00
updates/primary_db                                                                                           | 3.1 MB     00:12
===================================================== N/S Matched: ImageMagick =====================================================
ImageMagick-c++.i686 : ImageMagick Magick++ library (C++ bindings)
ImageMagick-c++.x86_64 : ImageMagick Magick++ library (C++ bindings)
ImageMagick-c++-devel.i686 : C++ bindings for the ImageMagick library
ImageMagick-c++-devel.x86_64 : C++ bindings for the ImageMagick library
ImageMagick-devel.i686 : Library links and header files for ImageMagick app development
ImageMagick-devel.x86_64 : Library links and header files for ImageMagick app development
ImageMagick-doc.x86_64 : ImageMagick html documentation
ImageMagick-perl.x86_64 : ImageMagick perl bindings
GraphicsMagick.i686 : An ImageMagick fork, offering faster image generation and better quality
GraphicsMagick.x86_64 : An ImageMagick fork, offering faster image generation and better quality
ImageMagick.i686 : An X application for displaying and manipulating images
ImageMagick.x86_64 : An X application for displaying and manipulating images
php-magickwand.x86_64 : PHP API for ImageMagick
php-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library

  Name and summary matches only, use "search all" for everything.

  
[root@walk ~]# yum install ImageMagick -y
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: ftp.iitm.ac.in
 * epel: ftp.yz.yamagata-u.ac.jp
 * extras: ftp.iitm.ac.in
 * jpackage-generic: sunsite.informatik.rwth-aachen.de
 * updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package ImageMagick.x86_64 0:6.7.2.7-6.el6 will be installed
--> Processing Dependency: libwmflite-0.2.so.7()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libwmf-0.2.so.7()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: librsvg-2.so.2()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libltdl.so.7()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libImath.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libIlmThread.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libIlmImf.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libIex.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Processing Dependency: libHalf.so.6()(64bit) for package: ImageMagick-6.7.2.7-6.el6.x86_64
--> Running transaction check
---> Package OpenEXR-libs.x86_64 0:1.6.1-8.1.el6 will be installed
---> Package ilmbase.x86_64 0:1.0.1-6.1.el6 will be installed
---> Package librsvg2.x86_64 0:2.26.0-14.el6 will be installed
--> Processing Dependency: libgsf >= 1.6.0 for package: librsvg2-2.26.0-14.el6.x86_64
--> Processing Dependency: libgsf-1.so.114()(64bit) for package: librsvg2-2.26.0-14.el6.x86_64
--> Processing Dependency: libcroco-0.6.so.3()(64bit) for package: librsvg2-2.26.0-14.el6.x86_64
---> Package libtool-ltdl.x86_64 0:2.2.6-15.5.el6 will be installed
---> Package libwmf.x86_64 0:0.2.8.4-25.el6_7 will be installed
---> Package libwmf-lite.x86_64 0:0.2.8.4-25.el6_7 will be installed
--> Running transaction check
---> Package libcroco.x86_64 0:0.6.2-5.el6 will be installed
---> Package libgsf.x86_64 0:1.14.15-5.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================
 Package                          Arch                       Version                                 Repository                Size
====================================================================================================================================
Installing:
 ImageMagick                      x86_64                     6.7.2.7-6.el6                           base                     1.9 M
Installing for dependencies:
 OpenEXR-libs                     x86_64                     1.6.1-8.1.el6                           base                     197 k
 ilmbase                          x86_64                     1.0.1-6.1.el6                           base                      72 k
 libcroco                         x86_64                     0.6.2-5.el6                             base                     100 k
 libgsf                           x86_64                     1.14.15-5.el6                           base                     116 k
 librsvg2                         x86_64                     2.26.0-14.el6                           base                     140 k
 libtool-ltdl                     x86_64                     2.2.6-15.5.el6                          base                      44 k
 libwmf                           x86_64                     0.2.8.4-25.el6_7                        base                     132 k
 libwmf-lite                      x86_64                     0.2.8.4-25.el6_7                        base                      51 k

Transaction Summary
====================================================================================================================================
Install       9 Package(s)

Total download size: 2.7 M
Installed size: 9.8 M
Downloading Packages:
(1/9): ImageMagick-6.7.2.7-6.el6.x86_64.rpm                                                                  | 1.9 MB     00:07
(2/9): OpenEXR-libs-1.6.1-8.1.el6.x86_64.rpm                                                                 | 197 kB     00:03
(3/9): ilmbase-1.0.1-6.1.el6.x86_64.rpm                                                                      |  72 kB     00:00
(4/9): libcroco-0.6.2-5.el6.x86_64.rpm                                                                       | 100 kB     00:00
(5/9): libgsf-1.14.15-5.el6.x86_64.rpm                                                                       | 116 kB     00:00
(6/9): librsvg2-2.26.0-14.el6.x86_64.rpm                                                                     | 140 kB     00:01
(7/9): libtool-ltdl-2.2.6-15.5.el6.x86_64.rpm                                                                |  44 kB     00:00
(8/9): libwmf-0.2.8.4-25.el6_7.x86_64.rpm                                                                    | 132 kB     00:00
(9/9): libwmf-lite-0.2.8.4-25.el6_7.x86_64.rpm                                                               |  51 kB     00:00
------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                               111 kB/s | 2.7 MB     00:25
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : ilmbase-1.0.1-6.1.el6.x86_64                                                                                     1/9
  Installing : libwmf-lite-0.2.8.4-25.el6_7.x86_64                                                                              2/9
  Installing : libwmf-0.2.8.4-25.el6_7.x86_64                                                                                   3/9
  Installing : OpenEXR-libs-1.6.1-8.1.el6.x86_64                                                                                4/9
  Installing : libtool-ltdl-2.2.6-15.5.el6.x86_64                                                                               5/9
  Installing : libgsf-1.14.15-5.el6.x86_64                                                                                      6/9
  Installing : libcroco-0.6.2-5.el6.x86_64                                                                                      7/9
  Installing : librsvg2-2.26.0-14.el6.x86_64                                                                                    8/9
  Installing : ImageMagick-6.7.2.7-6.el6.x86_64                                                                                 9/9
  Verifying  : libwmf-lite-0.2.8.4-25.el6_7.x86_64                                                                              1/9
  Verifying  : librsvg2-2.26.0-14.el6.x86_64                                                                                    2/9
  Verifying  : ImageMagick-6.7.2.7-6.el6.x86_64                                                                                 3/9
  Verifying  : libwmf-0.2.8.4-25.el6_7.x86_64                                                                                   4/9
  Verifying  : libcroco-0.6.2-5.el6.x86_64                                                                                      5/9
  Verifying  : OpenEXR-libs-1.6.1-8.1.el6.x86_64                                                                                6/9
  Verifying  : libgsf-1.14.15-5.el6.x86_64                                                                                      7/9
  Verifying  : libtool-ltdl-2.2.6-15.5.el6.x86_64                                                                               8/9
  Verifying  : ilmbase-1.0.1-6.1.el6.x86_64                                                                                     9/9

Installed:
  ImageMagick.x86_64 0:6.7.2.7-6.el6

Dependency Installed:
  OpenEXR-libs.x86_64 0:1.6.1-8.1.el6       ilmbase.x86_64 0:1.0.1-6.1.el6              libcroco.x86_64 0:0.6.2-5.el6
  libgsf.x86_64 0:1.14.15-5.el6             librsvg2.x86_64 0:2.26.0-14.el6             libtool-ltdl.x86_64 0:2.2.6-15.5.el6
  libwmf.x86_64 0:0.2.8.4-25.el6_7          libwmf-lite.x86_64 0:0.2.8.4-25.el6_7

Complete!
[root@walk ~]#


With the package and it's dependencies installed let's query the rpm to get some information about the package.

[root@walk ~]# rpm -qa | grep -i Image
genisoimage-1.1.9-12.el6.x86_64
ImageMagick-6.7.2.7-6.el6.x86_64
[root@walk ~]# rpm -qi ImageMagick-6.7.2.7-6.el6.x86_64
Name        : ImageMagick                  Relocations: (not relocatable)
Version     : 6.7.2.7                           Vendor: CentOS
Release     : 6.el6                         Build Date: Wed 22 Mar 2017 04:54:38 PM IST
Install Date: Sat 26 Aug 2017 12:35:53 AM IST      Build Host: c1bm.rdu2.centos.org
Group       : Applications/Multimedia       Source RPM: ImageMagick-6.7.2.7-6.el6.src.rpm
Size        : 7321917                          License: ImageMagick
Signature   : RSA/SHA1, Thu 23 Mar 2017 08:28:57 PM IST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.imagemagick.org/
Summary     : An X application for displaying and manipulating images
Description :
ImageMagick is an image display and manipulation tool for the X
Window System. ImageMagick can read and write JPEG, TIFF, PNM, GIF,
and Photo CD image formats. It can resize, rotate, sharpen, color
reduce, or add special effects to an image, and when finished you can
either save the completed work in the original format or a different
one. ImageMagick also includes command line programs for creating
animated or transparent .gifs, creating composite images, creating
thumbnail images, and more.

ImageMagick is one of your choices if you need a program to manipulate
and display images. If you want to develop your own applications
which use ImageMagick code or APIs, you need to install
ImageMagick-devel as well.


The ImageMagick software provides the convert command which I found very diverse because it allows the user to draw, crop and resize images along with many other things. You can get more information about it by going through the man page.

[root@walk ~]# which convert
/usr/bin/convert
[root@walk ~]#


I intended to use the convert command along with the label feature to pipe out the STDOUT of a command run on the shell to draw that text on an image. 

I tried hard to get this to work but I kept getting the below error while testing.



After a lot of frantic searching I came to know that using the labels feature has been deliberately disabled in RedHat Linux/CentOS systems. I cannot comment on the authenticity of this but after reading it I began to look for alternatives to achieve the desired outcome with convert and without using label.

Finally after a lot of searching I came up with this one liner that did the job:

convert -size 1000x600 -gravity Northwest xc:khaki -font Courier-Bold -fill black  -pointsize 18 -draw "text 5,15 '$(cat /etc/passwd)'" passwd.png


This command generated the following passwd.png image in the current working directory of my CenOS system.


Let's deconstruct the above convert command options:

-size option determines the size of the image.

-gravity option determines the position of text on the image.

-xc option decides the image background or the canvas color.

-font option determines the font type of the text being drawn on the image.

-fill option decides the text color.

-pointsize determines the font size of the text being drawn.

-draw option provides the text to be drawn on the image.


For this demonstration I've done a command substitution of the "fdisk -l" command output to serve as the text to be drawn on the image.

The final parameter passwd.png is the name we'd like for the resulting image.


We can use the list flag with the convert keyword to view the available options for the various flag that we use.

For example:

[root@walk ~]# convert -list font | less
[root@walk ~]#
[root@walk ~]# convert -list gravity
None
Center
East
Forget
NorthEast
North
NorthWest
SouthEast
South
SouthWest
West
Static


Let's take another example changing a few of the options. Let's create an image with the output of "cat /etc/passwd" command.

convert -size 1000x600 -gravity Northwest xc:khaki -font Courier-Bold -fill black  -pointsize 18 -draw "text 5,15 '$(cat /etc/passwd)'" passwd.png




This time I modified the image size, canvas color and font size along with the text to be drawn as I anticipated the output to be larger thereby requiring a larger canvas to display the text.

If we could use labels then the label feature would autosize the text for us to fit on the image.

We can easily change the image format by simply changing the extension of the final image to be created.

To validate, let's create a JPEG image of the df -h command output.

[root@walk ~]# convert -size 700x200 -gravity Northwest xc:white -font Courier-Bold -fill blue  -pointsize 18  -draw "text 5,15 '$(df -h)'" df.jpeg


This command generates the below image:


I hope you found this article useful and perhaps ImageMagick gave you some interesting ideas.

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...