iT邦幫忙

0

[以解決]Shellscript 擷取指定欄位時間問題

  • 分享至 

  • xImage

請問一下,我想截取下表 Kubernetes 的 EXPIRES 或 RESIDUAL TIME 來提示到期日,及計算到期日並發告警。因為會受到
像 RESIDUAL TIME、CERTIFICATE、May 04, 2032 12:32 UTC 這類有空格,所以沒法抓完整,不知這樣表格型的怎麼截
https://ithelp.ithome.com.tw/upload/images/20220511/20094721m9Xg7BIYT8.png

CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
admin.conf                 May 04, 2032 12:32 UTC   9y                                      no
apiserver                  May 04, 2032 12:32 UTC   9y              ca                      no
apiserver-etcd-client      May 04, 2032 12:32 UTC   9y              etcd-ca                 no
apiserver-kubelet-client   May 04, 2032 12:32 UTC   9y              ca                      no
controller-manager.conf    May 04, 2032 12:32 UTC   9y                                      no
etcd-healthcheck-client    May 04, 2032 12:32 UTC   9y              etcd-ca                 no
etcd-peer                  May 04, 2032 12:32 UTC   9y              etcd-ca                 no
etcd-server                May 04, 2032 12:32 UTC   9y              etcd-ca                 no
front-proxy-client         May 04, 2032 12:32 UTC   9y              front-proxy-ca          no
scheduler.conf             May 04, 2032 12:32 UTC   9y                                      no

CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
ca                      Apr 27, 2031 23:37 UTC   8y              no
etcd-ca                 Apr 27, 2031 23:37 UTC   8y              no
front-proxy-ca          Apr 27, 2031 23:37 UTC   8y              no
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
wiseguy
iT邦超人 1 級 ‧ 2022-05-18 11:44:13
最佳解答

使用 cut -c 配合位置來截。比如你想截取 EXPIRES 欄位,就是下
cut -c 28-50 /tmp/Kubernetes.log
(假設你上面的內容在 /tmp/Kubernetes.log 裡)
就可以抓到 EXPIRES 欄位的值了。同理,你要抓 CERTIFICATE AUTHORITY 的值,就是下
cut -c 69-90 /tmp/Kubernetes.log
要兩欄一起抓就是
cut -c 28-50,69-90 /tmp/Kubernetes.log

vicentli iT邦研究生 4 級 ‧ 2022-05-28 15:11:04 檢舉

感謝,知道怎麼用了
kubeadm certs check-expiration |cut -c 1-39,28-49| awk 'NR>3'

ERTIFICATE                EXPIRES
admin.conf                 May 04, 2032 12:32 UTC
apiserver                  May 04, 2032 12:32 UTC
apiserver-etcd-client      May 04, 2032 12:32 UTC
apiserver-kubelet-client   May 04, 2032 12:32 UTC
controller-manager.conf    May 04, 2032 12:32 UTC
etcd-healthcheck-client    May 04, 2032 12:32 UTC
etcd-peer                  May 04, 2032 12:32 UTC
etcd-server                May 04, 2032 12:32 UTC
front-proxy-client         May 04, 2032 12:32 UTC
scheduler.conf             May 04, 2032 12:32 UTC

CERTIFICATE AUTHORITY   EXPIRES
ca                      Apr 27, 2031 23:37 UTC
etcd-ca                 Apr 27, 2031 23:37 UTC
front-proxy-ca          Apr 27, 2031 23:37 UTC

我要發表回答

立即登入回答