Saturday, April 28, 2018

用 cowsay 讓終端機充滿智慧

0. 準備工作
apt-get install fortune cowsay cconv

1. 程式碼 Cowsay.sh
#!/usr/bin/env bash

PATH=$PATH

SLEEP=6

declare -A CS

MAXEM=0
for e in `ls /usr/share/cowsay/cows/*.cow | sed 's/\.cow//' | awk 'BEGIN{FS="/"};{print $NF}'`
do
        CS[$MAXEM]="$e"
        MAXEM=$((MAXEM+1))
done

while [ 1 ];
do
        clear
        S=$(( RANDOM % 52 ))
        S=$((S+SLEEP))
        F="$S"
        FACE=""
        if [ $F -gt $MAXEM ]; then
                F=$(( F % MAXEM ))
                FACE=${CS[$F]}
        else
                FACE=${CS[$F]}
        fi
        date +"%Y/%m/%d %H:%M:%S"
        /usr/games/fortune | cconv -f UTF8-CN -t UTF8-TW | sed -r "s/\x1B\[[0-9;]*[mK]//g" | /usr/games/cowsay -f $FACE
        sleep $S
done

2. 執行
chmod a+x Cowsay.sh
./Cowsay.sh
bash Cowsay.sh

阿宅桌面時鐘 ASCII 版

請先安裝 toilet
# apt-get install toilet

然後執行
watch -n 1 "date +\"%Y-%m-%d %H:%M:%S\" | toilet"


Ubuntu 12 升級 16 意外直達 18 感想

話說之前將一台迷你筆電從 12 經過 14 升級 16 一切順利.
就來將自己的一號機筆電從好多年的 12 升級 16. 這台唯一的升級難度是有使用加密使用者目錄, 如果不小心就會毀掉使用者目錄, 這也是之前猶豫不升級版本的主因.

首先使用外接磁碟備份重要資料.

接著就使用 do-release-upgrade 從 12 升級 14. 一切順利登入而且正確輸入密碼解開使用者目錄. 一鼓作氣繼續升級到 16.

沒想到災難開始, 一路開始抱怨一堆套件不能安裝. 硬著頭皮繼續下去, 到最後還出現完成升級但是無法安裝一堆套件. 直接結束後重新開機就直接 kernel panic.

這時發揮堅持的精神, 放棄新安裝的核心改用舊版開機, 很好, 可以看到終端機登入畫面.
登入後發現無法自動連接網路, 這難不了我, 使用 ifconfig 大法手動來.
網路終於通了, 繼續堅持再執行 do-release-upgrade 還是一樣慘兮兮. 甚至想到之前將雲端VM從 14 升級 16 只改了 apt source 的方法, 然後 apt-get update; apt-get upgrade 後重新開機還是一樣.
這下傻了, 不會要重新安裝系統吧?!

還好此時靈光乍現, 不給升級就改用 update-manager -d. 可是現在是終端機界面哪來的 GUI WM. 死馬當活馬醫, 使用 startx 上場, 雖然 menu bar 通通失蹤, 可是有桌面啊! 立馬按下右鍵開個 GUI 下的終端機執行 update-manager -d. 看到有新版可以更新, 太讚了. 直上 18.04 LTS.
一切順利完成. 重新開機後看到登入畫面一整個感動. 而且大部分安裝的套件都沒消失, 只有 VirtualBox 等已在二號機安裝完成的套件不見蹤影. 更重要的是加密使用者目錄沒消失.

就這樣消耗兩個晚上, 最後想起來之前順利完成是使用更新管理員.
請愛用 update-manager -d

安裝簡易的電報送訊息機器人 Telegram bot

0. 安裝 telegram 設定帳號 UACCT

1. 加入 @BotFather
BotFather is the one bot to rule them all. Use it to create new bot accounts and manage your existing bots.

About Telegram bots:
https://core.telegram.org/bots
Bot API manual:
https://core.telegram.org/bots/api

Contact @BotSupport if you have questions about the Bot API.


UACCT:
/start

BotFather:
I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual.

You can control me by sending these commands:

/newbot - create a new bot
/mybots - edit your bots [beta]
/mygames - edit your games [beta]
...

2. 新增 bot
UACCT:
/newbot

BotFather:
Alright, a new bot. How are we going to call it? Please choose a name for your bot.

UACCT:
p****y

BotFather:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

UACCT:
botusername_bot

BotFather:
Done! Congratulations on your new bot. You will find it at t.me/pipboy88_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
5*******2:A*********************************w

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

3. 測試 API
3.1. 查詢我是誰
https://api.telegram.org/bot5*******2:A*********************************w/getMe
回應
{"ok":true,"result":{"id":5*******2,"is_bot":true,"first_name":"p****y","username":"botusername_bot"}}

3.2. 加入 bot 聊天並取得聊天室編號
https://api.telegram.org/bot5*******2:A*********************************w/getUpdates
{
  "ok": true,
  "result": [
    {
      "update_id": 5*******0,
      "message": {
        "message_id": 41,
        "from": {
          "id": CHAT_ID,
          "is_bot": false,
          "first_name": "UACCT_DESCRIPTION",
          "username": "UACCT",
          "language_code": "zh-TW"
        },
        "chat": {
          "id": CHAT_ID,
          "first_name": "UACCT_DESCRIPTION",
          "username": "UACCT",
          "type": "private"
        },
        "date": 1524858545,
        "text": "hbbvoebbvebwber"
      }
    }
  ]
}

3.3. 送出第一則訊息
https://api.telegram.org/bot5*******2:A*********************************w/sendMessage?chat_id=CHAT_ID&text=Hello+World

3.4. 送訊息 script
3.4.1. 先安裝 packages
apt-get install jq cconv fortune fortune-zh

3.4.2. 送訊息程式 tgsendmsg.sh
#!/usr/bin/env bash

PATH=$PATH

TS=`date +"%s.%N"`
LOGPATH="/PATH/to/tglog"
LOG="$LOGPATH/LogFilename.log"

BOTTK="5*******2:A*********************************w"
CHATID="CHAT_ID"

mkdir -p $LOGPATH
touch $LOG
chown -R 0755 $LOGPATH

if [ -n "$1" ]; then
        MSG="$1"
        shift
else
        echo "Usage: $0 \"Message\""
        exit 1
fi

TM=`curl --progress-bar --data-urlencode "chat_id=$CHATID" --data-urlencode "text=$MSG" "https://api.telegram.org/bot$BOTTK/sendMessage"`
echo "$TM" | jq
echo "$TS $TM" >> $LOG

3.4.3. 呼叫送出訊息 MyMSG.sh
#!/usr/bin/env bash

PATH=$PATH

Z="Asia/Taipei"

HR=`TZ=":$Z" date +"%H"`
HR=`echo "$HR" | sed 's/^0//'`
echo $HR

if [ $HR -eq 6 ]; then
        /root/tgsendmsg.sh "Good Morning 早安"
        FTZH=`/usr/games/fortune-zh | cconv -f UTF8-CN -t UTF8-TW`
        MSG=""
        for d in $FTZH
        do
                MSG="$MSG $d"
        done
        MSG=`echo "$MSG" | sed -r "s/\x1B\[[0-9;]*[mK]//g"`
        /root/tgsendmsg.sh "$MSG"
fi

if [ $HR -eq 12 ]; then
        /root/tgsendmsg.sh "Good Afternoon 午安"
        FT=`/usr/games/fortune | cconv -f UTF8-CN -t UTF8-TW`
        FT=`echo "$FT" | sed -r "s/\x1B\[[0-9;]*[mK]//g"`
        /root/tgsendmsg.sh "$FT"
fi

if [ $HR -eq 18 ]; then
        /root/tgsendmsg.sh "Good Night 晚安"
        FT=`/usr/games/fortune | cconv -f UTF8-CN -t UTF8-TW`
        FT=`echo "$FT" | sed -r "s/\x1B\[[0-9;]*[mK]//g"`
        /root/tgsendmsg.sh "$FT"
fi

接下來就發揮想像力把能串接的掛上去, 也能多開幾隻 bot 送不同類型的訊息.