說明:使用了macOS后發現,win系統能使用的xshell、xftp等連接服務器及其文件傳輸等軟件均不能使用了,沒有兼容的版本。所以我們剛切換到mac系統該如何去適應呢。
一、連接遠程服務器
macOS中前文也說道我們使用的是iterm2進行終端控制的,所以連接遠程服務器可以通過ssh來連接
方式一:ssh 用戶名@公網ip的方式進行連接
缺點:每次都需要輸入命令且輸入密碼來進行登陸,比較繁瑣
疑問:有沒有一種只輸入一次密碼就能每次簡單登陸的方法呢?
方式二:通過腳本進行登陸
- 找一個地方創建腳本
- 編輯腳本,內容如下:
#!/usr/bin/expect
set timeout 30
spawn ssh -p [lindex $argv 0] [lindex $argv 1]@[lindex $argv 2]
expect {"(yes/no)?"{send "yes\n";exp_continue}"password:"{send "[lindex $argv 3]\n"}
}
interact# 變量說明
# [lindex $argv 0]:端口號
# [lindex $argv 1]:服務器用戶名
# [lindex $argv 2]:服務器IP地址
# [lindex $argv 3]:服務器密碼
- 編輯完成給予腳本執行權限
# 文件名字可以隨便取
chmod 777 ssh_server.sh
- 打開iTerm2,打開Preferences配置界面,Profiles -> general,左下角點擊+號,新建profile,參考下面圖片在對應位置輸入內容即可。
Name:根據需求輸入,通常選擇標識性較強的內容便于區分,例如服務器的IP地址
Command:這里選擇login Shell
Send text at start :填寫格式形如A B C D E這樣,每一個部分之間用空格隔開,根據自己實際情況填寫,下面是對每一部分內容的解釋
A代表咱們上面寫的本機保存sh腳本的路徑:/Users/iterm/ssh_server.shB代表服務器端口號一般遠程連接端口為:22C代表服務器用戶名一般為:rootD代表服務器IP:根據服務器對外暴露的公網IP填寫E代表服務器密碼:根據自己實際的服務器密碼填寫
- 設置好之后打開iTerm2,點擊profiles,點擊前面自己新增的連接遠程服務器的profile的名字。首次連接需要輸入一次服務器密碼,之后再連接就免密碼登陸了。
二、文件傳輸
方式一:通過app去實現文件傳輸
- transmit(mac系統最推薦的,沒有之一) 官網收費有點貴,可以通過萬能的某寶
- filezilla
- 等等
方式二:通過lrzsz
- 給mac以及服務器都裝上lrzsz
# mac安裝通過homebrew
brew install lrzsz
# Linux系統安裝通過yum
yum -y install lrzsz
- mac下載iterm2-zmodem
wget https://raw.github.com/mmastrac/iterm2-zmodem/master/iterm2-send-zmodem.shwget https://raw.github.com/mmastrac/iterm2-zmodem/master/iterm2-recv-zmodem.sh
# 如果wget notfound
brew install wget# 如果網絡不佳直接去github下載
# https://github.com/robberphex/iTerm2-zmodem
- 下載下來的兩個文件iterm2-recv-zmodem.sh和iterm2-send-zmodem.sh需要修改一下rz和sz的路徑因為文件對應的還是原來intel芯片下的mac通過homebrew下載的路徑,m芯片homebrew安裝路徑變了
iterm2-send-zmodem.sh
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domainosascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; thenFILE=$(osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
elseFILE=$(osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
fi
if [[ $FILE = "" ]]; thenecho Cancelled.# Send ZModem cancelecho -e \\x18\\x18\\x18\\x18\\x18sleep 1echoecho \# Cancelled transfer
else# 此處需要修改# usr/local/bin/sz "$FILE" --escape --binary --bufsize 4096/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/sz "$FILE" --escape --binary --bufsize 4096sleep 1echoecho \# Received "$FILE"
fi
iterm2-recv-zmodem.sh
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domainosascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; thenFILE=$(osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
elseFILE=$(osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")")
fiif [[ $FILE = "" ]]; thenecho Cancelled.# Send ZModem cancelecho -e \\x18\\x18\\x18\\x18\\x18sleep 1echoecho \# Cancelled transfer
elsecd "$FILE"# /usr/local/bin/rz --rename --escape --binary --bufsize 4096 /opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/rz --rename --escape --binary --bufsize 4096sleep 1echoechoecho \# Sent \-\> $FILE
fi
- 查看路徑方法
brew list lrzsz
# 以下是輸出結果
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lrb
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lrx
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lrz
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lsb
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lsx
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/lsz
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/rz
/opt/homebrew/Cellar/lrzsz/0.12.20_1/bin/sz
/opt/homebrew/Cellar/lrzsz/0.12.20_1/share/man/ (2 files)
- 賦予兩個腳本的執行權限
chmod 777 /usr/local/bin/iterm2-*
- 設置iterm2的觸發器
# 設置內容Regular expression: rz waiting to receive.\*\*B0100Action: Run Silent CoprocessParameters: /usr/local/bin/iterm2-send-zmodem.shInstant: checkedRegular expression: \*\*B00000000000000Action: Run Silent CoprocessParameters: /usr/local/bin/iterm2-recv-zmodem.shInstant: checked
- 重啟iTerm 2 ,連接上服務器,就能通過rz、sz來上傳下載文件了
注意:如果是用expect腳本登錄的,無法使用lszrz。上述的腳本連接就是使用的cxpect
解決方式待更新!!!!