2019年10月23日 星期三

[TIPS] 檢查CPU溫度 Raspberry PI


命令

 /opt/vc/bin/vcgencmd

CPU溫度


pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd version

Jul  9 2019 14:40:53

Copyright (c) 2012 Broadcom

version 6c3fe3f096a93de3b34252ad98cdccadeb534be2 (clean) (release) (start)

pi@raspberrypi:~ $ uname -a

Linux raspberrypi 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux

pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd measure_temp

temp=45.1'C

2019年9月23日 星期一

阿班 Bash 常用指令範例

Last Modified Date: 2020-08-27

mkdir

Date: 08-27 v1
建立新目錄

語法


mkdir  [-p]  <目錄名>


OPTION

無參數: 如果該目錄不存在,才建立它。若該目錄在則顯示目錄已存在錯誤訊息,並且回傳值為1 
-p: 如果該目錄不存在,才建立它。若該目錄在則略過,並且回傳值為 0

範例

# mkdir new_directory
#
# mkdir new_directory
mkdir: can't create directory 'new_directory': File exists
# echo $?
1
# mkdir -p new_directory
# echo $?
0


mktemp


Date: 05-14 v1
產生暫存檔案或目錄

語法


mktemp [OPTION] ... [TEMPLATE]

OPTION

-u: --dry-run 不產生任何檔案或目錄,只顯示名稱 (不安全, 僅供測試, 因為有可能重複產生)

範例

bash-5.0# mktemp -u /you_path/path2/your_prefix.XXXXXXXXXX

/you_path/path2/your_prefix.zjvveu0Ncs

# mktemp /tmp/your_prefix.XXXXXXXXXX
/tmp/your_prefix.5eEu7vU0zi
# ls -la /tmp/your_prefix.5eEu7vU0zi

-rw------- 1 test test 0 May 15 00:06 /tmp/your_prefix.5eEu7vU0zi


read


語法

read [argument1] [argument2] ...

範例


bash-5.0# find . -type f | while read filename ; do echo "$fiilename" ; done



sed

刪除空白行

語法

 sed -e "/^$/d" 

範例


bash-5.0# echo -e "te\n\nst" | sed "/^$/d"

te

st

type

語法

type command1 command2 ...

範例

bash-5.0# type type

type is a shell builtin       

type a b

a is a function

a ()

{

    echo 'a'

}

b is a function

b ()

{

    echo 'b'

}

xargs

語法

xargs [argument1] [argument2] ...

範例


bash-5.0# find . -type f | xargs wc -l | sed -e "s/^[ \t]*//g"

2 ./b.txt
1 ./c.txt
3 ./a.txt
6 total



2019年9月16日 星期一

阿班 Docker 指令備忘範例

啟動 Docker Container


自動啟動 Container

 編修日期: 2019-9-23

使用情境

docker machine 重啟後,依設定規則來自動執行對應的 Docker Containers

參考文件

https://docs.docker.com/config/containers/start-containers-automatically/

語法

docker update --restart always container-id [...]

or
docker update --restart always container-name [...]

範例

docker update --restart always my-testapp my-testapp2

2019年6月29日 星期六

Bash Tip - Why .bashrc (dot bashrc) not loaded after new user created?


Recently, I reinstalled ubuntu 16.04LTS on my desktop as a Linux server.

After a new user created, ssh into the server each time, but .bashrc (dot bashrc) not loaded after new user created? Please try to modify the profile as bellow.

Append some commands into ~/.profile

result of test-cases.sh example

~$ source .profile
load /home/ben/.bashrc by ~/.profile
.bashrc has been executed once

Appendix


2019年5月13日 星期一

開源 RISC OS Pi 系統 - 初探





1987 開始發展的RISC OS, 終於在2018年10月以Apache 2.0 License 開源,允許商業使用。

近來忙裡偷閒,用樹梅派最新版本 Raspberry Pi 3 Model B+ 安裝了RISC OS PI ,號稱是ARM 架構下快速也易於客制化的作業系統 (A fast and easily customized operating system for ARM devices)。

初步試用, 它的視窗環境用起來執行程式反應很快(相比Raspbian  based X-Window 來說)。

並試了一些常用工具都有替代的APP可以使用
Ex:
  • Nettle(ssh/telnet client)
  • LanMan (Samba client)
  • NetSurf (Browser) ...


不過還需要更多開源高手投入時間進一步改善。

安裝方式是以NOOBS來安裝 RISC OS PI




小結

待機耗電量約在 2.93W (4.89V, 0.6A)運行較吃CPU資源大約升高到 3.9W (4.89V, 0.8A),以一個迷你作業系統而言,還算省電。有興趣的朋友可以裝起來試玩看看。






參考資料


延伸閱讀

2019年5月9日 星期四

DevOps Tip - Got error - not found aws-iam-authenticator for kubectl using?


Problem

I've got error to run kubectl command with aws-iam-authenticator not found

(venv) ➜  k8s git:(develop) ✗ kubectl get pod
Unable to connect to the server: getting credentials: exec: exec: "aws-iam-authenticator": executable file not found in $PATH


Solutions

Installation tool on OSX Mojave

brew install aws-iam-authenticator

Example of installation

 (venv) ➜  k8s git:(develop) ✗ brew install aws-iam-authenticator
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (homebrew/core, homebrew/cask and caskroom/cask).
==> New Formulae
..
==> Downloading https://homebrew.bintray.com/bottles/
aws-iam-authenticator-0.4.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/52...
######################################################################## 100.0%
==> Pouring aws-iam-authenticator-0.4.0.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/aws-iam-authenticator/0.4.0: 5 files, 31.1MB

References


2019年4月11日 星期四

Python Tip - Examples of initialize variables with sequence number in different ways

Purpose

It is used to collect sample code for python's coding tips of initialization variables.

A. Variables with constants

test_init_vars.py

#!/usr/bin/env python

a=0
b=1
c=2
d=3
e=4
f=5
print(a, b, c, d, e, f)

B. Use range()

test_init_vars_by_range.py

#!/usr/bin/env python

a, b, c, d, e, f = range(6)

print(a, b, c, d, e, f)

C. Use enum since python 3.4 supported

test_init_vars_by_enum.py

#!/usr/bin/env python3.6

from enum import IntEnum,auto

class Vars(IntEnum):
    a=0
    b=auto()
    c=auto()
    d=auto()
    e=auto()
    f=auto()

print("{}, {}, {}, {}, {}, {}".format(Vars.a, Vars.b, Vars.c, Vars.d, Vars.e, Vars.f))

Result

$ python2.7 test_init_vars.py
(0, 1, 2, 3, 4, 5)
$ python3.6 test_init_vars.py
0 1 2 3 4 5

$ python2.7 test_init_vars_by_range.py
(0, 1, 2, 3, 4, 5)
$ python3.6 test_init_vars_by_range.py
0 1 2 3 4 5

$ python2.7 test_init_vars_by_enum.py
Traceback (most recent call last):
  File "test_init_vars_by_enum.py", line 3, in
    from enum import IntEnum,auto
ImportError: cannot import name auto

$ python3.6 test_init_vars_by_enum.py
0, 1, 2, 3, 4, 5

References

2019年3月14日 星期四

Bash Tip - Run Command with multiple arguments with ssh




This is a note for logging my simple way to use ssh and bash with continuous integration on remote server.

You can execute the following test-cases.sh on your host.

It will execute remote script of the test with correct argument contents.

source code of test-cases.sh on host





directory layout of tester account on test-server



source code of tests.sh on test-server




result of test-cases.sh example

source ./test-cases.sh
➜  ~ run_cases "\"1\"\"" "b'\"" "c'#\"" "\"\"" "\'"
ssh test-server cd test-cases; /bin/bash ./tests.sh  "\"1\"\"" "b\'\"" "c\'\#\"" "\"\"" "\\'"
argv[1]=["1""]
argv[2]=[b\'"]
argv[3]=[c\'\#"]
argv[4]=[""]
argv[5]=[\\']

Notes

printf %q  is use to make safe quote the argument if hashtag(#), quote('), double quote(") and so on.

Appendix



2019年2月22日 星期五

Bash Tip - Use bash to implement like C's argc and argv

Test bash script capacity

I want to show how to write the advanced bash script with shift and array.
Feature:

  • filter the arguments with '-' leading character as options
  • the following inputs are the data arguments


Tested environments: - bash 4.3.48 x86_64-pc-linux-gnu ubuntu-16.04.4 LTS

2019年1月30日 星期三

DevOps Tip - How to use terraform apply without prompt and input yes for CI/CD?

Problem

Took a memo about integration of CI/CD applied by terraform without prompt to input 'yes'
It is annoying to input yes every time if you already make sure already by `terraform plan`.
On the other hand, I need to integrate with different system for CI/CD. I need a way to approve it automatically.
By tests with commands official documents, I found the correct command and arguments.

Tested Solutions


    terraform apply -input=false -auto-approve

or as my test, you can shorten to the following command

    terraform apply -auto-approve

Test Environments

  • Ubuntu 16.04.04 LTS
  • OSX 10.14

Reference:




Extended Reading



2019年1月29日 星期二

DevOps Tip - How to use shared terraform plugins by -plugin-dir argument?


Problem


If you executed `terraform init`, you may already have the .terraform in your folder.


Because you may have many terraform repo similar to another, you can use the -plugin-dir to reduce the usage of disk space.

As a shared plugins so that we don’t need many copies of plugins into different locations.

Example

  • Here I used my OSX Macbook as a Demo Environment.
  ====================
  cd /build/setupdev/
    sudo mkdir -p  /opt/terraform/terraform.d
mv .terraform/plugins to /opt/terraform/terraform.d/plugins


  • Please run the following command to apply it.
  ====================
terraform init -input=false \
-plugin-dir=/opt/terraform/terraform.d/plugins/darwin_amd64





Tree of .terraform

Tree of terraform plugins

Reference

2019年1月27日 星期日

OrangeOS's 作業系統實現範例出現微軟防毒Defender的誤偵測





年關將近,最近繼續在整理舊書,偶然間翻到了一本放了很久的舊書 OrangeOS's 一個作業系統的實現。

在Windows 10 系統中,想試試它的範例,發現 a.img 檔案複製不過來,覺得十分奇怪。

突然跳出病毒移除警告,微軟的防毒軟體偵到 Trojan: DOS/Killmbr。


出現的路徑剛好是範例1的Hello World boot.asm 所對應的檔案 a.img

程式碼

原本的測試範例沒有Makefile, 筆者寫了一個測試用的Makefile 以方便在Ubuntu 16.04.4 重覆編譯

編譯測試檔案
/Dev/orangeos/chapter1/a$ make
qemu-img create -f raw a.img 1440k
Formatting 'a.img', fmt=raw size=1474560
mkfs.msdos -s 1 a.img
mkfs.fat 3.0.28 (2015-05-16)
nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=./a.img bs=512 count=1
1+0 records in
1+0 records out
512 bytes copied, 0.000185582 s, 2.8 MB/s


被偵測到有問題的boot.asm 範例


 org 07c00h
 mov ax, cs
 mov ds, ax
 mov es, ax
 call DispStr
 jmp $
DispStr:
 mov ax, BootMessage
 mov bp, ax
 mov cx, 16
 mov ax, 01301h
 mov bx, 000ch
 mov dl, 0
 int 10h
 ret
BootMessage:  db "Hello, OS world!"
times  510-($-$$) db 0
dw  0xaa55


通Defender 偵測的 boot.asm 範例

測試 ghaiklor 的 Simple boot sector with Hello World 範例編譯沒有問題
source: https://gist.github.com/ghaiklor/89e243a3463569480d01188c9e55e077





執行編譯OK的Hello World 

Windows 10 pro with qemu / make tools installed by choco


小結

比較程式的差異,通Defender 偵測的 boot.asm 範例,是比較好的寫法,有做處理停止中斷及結束。
看到偵測有病毒檔案訊息,先不用緊張,確認問題點,調整一下寫法,就可避開 Defender 誤偵測,繼續快樂的作業系統程式開發與研究囉!

2019年1月21日 星期一

如何在Mac上將微軟測試用虛擬機Image的zip 檔案解壓縮到特定目錄?

最近工作上,暫時會用到Windows 系統,
配合我的工作機所使用Parallels 虛擬化的軟體,
紀錄一下,在 MAC 上解壓縮微軟測試用虛擬機Image的zip 檔案到特定路徑並設定的方法

Step 1: 從微軟網站上下載虛擬機


下載相關檔案:選擇你要使用的虛擬機版本,及對應的虛擬化平台。



Step 2: 開啟 Terminal Console (我使用的是 iTerm2.app)



Step 3: 確認 unzip 指令在 Mac OS X (10.14.2 ) 的使用方法

在 terminal console 中輸入 man unzip, 可得知參數 -d exdir


Step 4: 解壓縮

以微軟30天測試用 Windows 7 Enterprise 虛擬機器 Parallels 檔案為例:

➜  unzip IE11.Win7.Parallels.zip -d ~/vmspace/win7/
Archive:  IE11.Win7.Parallels.zip
  inflating: /Users/tester/vmspace/win7/.DS_Store
   creating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/
  inflating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/config.pvs
  inflating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/floppy.fdd
   creating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/harddisk1.hdd/
  inflating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/harddisk1.hdd/DiskDescriptor.xml
 extracting: /Users/tester/vmspace/win7/IE11 - Win7.pvm/harddisk1.hdd/harddisk1.hdd
  inflating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/harddisk1.hdd/harddisk1.hdd.0.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds
  inflating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/harddisk1.hdd/harddisk1.hdd.drh
  inflating: /Users/tester/vmspace/win7/IE11 - Win7.pvm/VmInfo.pvi
➜ 

Step 5: 執行 pvm

即可在 /Users/tester/vmspace/win7 中找到 IE11 - Win7.pvm,點擊 pvm 檔案執行



Step 6: 執行後會看到對應的 IE11-Win7 的虛擬桌面

Step 7:安裝 Parallels Tools

用以安裝虛擬機平台相關驅動程式及擴充功能,安裝完成後,虛擬機需要重新啟動。


Step 8: 開始使用測試虛擬機



請注意:虛擬機預設定密碼為 Passw0rd! 

小結

虛擬化平台對於軟體在不同系統開發測試是必要工具,可減少重覆安裝系統的時間,搭配CI (持續整合),更能加速開發進度喔!