2020年8月5日 星期三

使用sshpass 傳送ssh密碼

CentOS / RHEL /Ubuntu 幾乎Linux環境都適用

工作上須對多台服務器來做設定檔修改等等,ssh對多台連線避免出現人工key密碼情形,當然此方法也可以拿來使用ssh-copy-id交換多台主機ssh public key
環境使用以Centos8.2來做示範首先來安裝epel-release 與sshpass 套件
[student@localhost 桌面]$ sudo dnf  -y install epel-release && sudo dnf -y install sshpass

Extra Packages for Enterprise Linux Modular 8 -  39 kB/s |  82 kB     00:02    
Extra Packages for Enterprise Linux 8 - x86_64  611 kB/s | 7.5 MB     00:12    
依賴關係解析完畢。
================================================================================
 Package           Architecture     Version                Repository      Size
================================================================================
安裝:
 sshpass           x86_64           1.06-9.el8             epel            27 k



首次登入ssh連線的話會show出一個 "Are you sure you want to continue connecting (yes/no/[fingerprint])?" 會造成sshpass使用困難
解決辦法:ssh -o StrictHostKeyChecking=no

[student@localhost 桌面]$ ssh 192.168.0.11

The authenticity of host '192.168.0.11 (192.168.0.11)' can't be established.
ECDSA key fingerprint is SHA256:s1MZg2bsOHYuwR6WfTdE1Z2sDyLBX7LJmDSIL/+kiqk.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

1. 使用明碼的方式來登入遠端主機

sshpass -p password ssh -o StrictHostKeyChecking=no user@ip

-p : 後面接的是使用者密碼 "student"

[student@localhost 桌面]$ sshpass -p student ssh -o StrictHostKeyChecking=no student@192.168.0.11


Warning: Permanently added '192.168.0.11' (ECDSA) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Wed Aug  5 04:55:54 2020


2. 宣告變數SSHPASS來登入遠端主機

-e : 帶入$SSHPASS環境變數 "student"

[student@localhost ~]$ export SSHPASS=student

[student@localhost ~]$ echo $SSHPASS
student

[student@localhost ~]$ sshpass -e ssh student@servera

Activate the web console with: systemctl enable --now cockpit.socketLast login: Wed Aug  5 06:51:14 2020 from 192.168.0.100

[student@servera ~]$ 


3. 從file 載入使用者密碼登入

-f : 從檔案載入使用者密碼 "student"

[student@servera ~]$ echo "student" >> passwd.txt

[student@servera ~]$ cat passwd.txt

student


[student@localhost ~]$ sshpass -f passwd.txt  ssh student@servera

Activate the web console with: systemctl enable --now cockpit.socket

Last login: Wed Aug  5 06:57:32 2020 from 192.168.0.100


4. For Loop迴圈搭配使用sshpass

如果第一次登入記得加上 "ssh -o StrictHostKeyChecking=no"

[student@localhost ~]$ for i in {a..c} ; do sshpass -f passwd.txt ssh -o StrictHostKeyChecking=no student@server$i 'hostname' ;done

servera.example.comserverb.example.comserverc.example.com

5. 編寫簡單腳本來對遠端主機做控制

[student@localhost ~]$ cat server_list.cfg 

servera
serverb
serverc

[student@localhost ~]$ cat passwd.sh

#!/bin/bash

export SSHPASS=redhat
ips=`cat server_list.cfg`

for ip in $ips

do

echo ==========$ip===========

sshpass -e  ssh root@$ip "hostname"

[ $? -eq 0 ] && echo -e "\033[32m ==$ip==password is ok... \033[0m" || echo -e "\033[31m ==$ip==Password is error!!! \033[0m"

done


2020年6月14日 星期日

Parted惡補

parted /dev/vdc mkpart 0% 100% --->如果全部空間都要用的話,則用100%就不會切到剩一點點
parted /dev/vdc mkpart 2048s100%

從0%的對齊大小在512 sector的最小對齊單位是17.4Kb
2048s是什麼意思呢?

意思是假設硬碟最小的physical block size 是512 sector,在一個20G的硬碟
2048s就是 1MB對齊的意思   1MB = 1024KB =  1048576 byte/每個扇區sector是512得出2048s
假設   是 4MB對齊   4MB = 4094KB =  4194304 byte /512 得到8192s
假設   是 8k 對齊   8KB = 8192 byte /512  16s --->可以強制過關,但最少parted"對齊"的大小是17.4kb在@sector是512大小時,所以parted希望是2048s的倍數

2020年6月11日 星期四

CentOS8 新增注音輸入法

yum install ibus-libpinyin.x86_64
dnf install ibus-libpinyin.x86_64
2種寫法都可以
然後去設定>地區跟語言>點+增加輸入法>搜尋chi>點進other
>Chinese(New Zhuyin)
就有注音輸入法能使用

參考網址
https://www.azureunali.com/linux-how-to-type-chineses-words-on-centos-8-steam/

2020年5月29日 星期五

使用指令"Script" 記錄終端機輸入&輸出

工作上種種情況需要留下terminal指令紀錄除了"history"以外,也可以使用"Script"來留底,需要進階的操作可參考頁尾連結

1. 在終端機上Key in "Script"指令表示開始記錄


2. 在終端機結束紀錄時Key in " exit "指令

3. 結束以後找到 "typescript" 檔案,使用 "cat" "head" "tail" 等指令檢視內容即可

Refer link
https://blog.gtwang.org/linux/screen-command-examples-to-manage-linux-terminals/