MACBOOK 通过iterm2连接堡垒机跳转服务器
创始人
2025-01-20 05:02:42
0
  • 本公司是通过齐治堡垒机连接远程服务器的环境,因为连接过程中需要自动输入密码和选择主机,所以要使用expect工具,编写expect脚本
  • remote.exp 
#!/usr/bin/expect  if { $argc != 7 } {     send_user "usage: expect $argv0 \[JUMP_HOST\] \[JUMP_PORT\] \[JUMP_USER\] \[JUMP_PASSWORD\] \[HOST\] \[USER\] \[PASSWORD\]\n\n\t"     send_user "*JUMP开头\t是堡垒机的登录信息\n\t"     send_user "*非JUMP开头\t是连接目标主机的登录信息."     exit }  set timeout 5  set jump_host [lindex $argv 0] set jump_port [lindex $argv 1] set jump_user [lindex $argv 2] set jump_pswd [lindex $argv 3] set host [lindex $argv 4] set user [lindex $argv 5] set pswd [lindex $argv 6]  # 连接堡垒机 spawn ssh -p$jump_port $jump_user@$jump_host # 登录堡垒机 expect {    "*(yes/no*)?" {        send "yes\r"        expect "*assword:"        send "$jump_pswd\r"    }     # 接受到password输入密码     "*assword:" {         send "$jump_pswd\r"     } } # 选择登录主机 expect {     "*:" {         send "$host\r"     } } # 选择登录用户 1 是 any expect {      "*:" {         send "1\r"     } } # 输入主机用户 expect {     "*login:" {         send "$user\r"     } } # 输入主机密码 expect {     "*assword:" {         send "$pswd\r"     } }  interact
  • 配置iterm2 profile
    • Send text at start: /Users/aaa/scripts/remote.exp 堡垒机ip 堡垒机ssh端口 堡垒机ssh用户 堡垒机ssh密码 跳转主机ip 跳转主机ssh用户 跳转主机ssh密码

  • 配置lrzsz
    • mac上传脚本,根据自己rz sz命令路径修改
  • recv
    cat /usr/local/bin/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 domain  osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm if [[ $NAME = "iTerm" ]]; then     FILE=`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)&\"\")"` else     FILE=`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)&\"\")"` fi  if [[ $FILE = "" ]]; then     echo Cancelled.     # Send ZModem cancel     echo -e \\x18\\x18\\x18\\x18\\x18     sleep 1     echo     echo \# Cancelled transfer else     cd "$FILE"     /opt/homebrew/bin/rz -E -e -b     sleep 1     echo     echo     echo \# Sent \-\> $FILE fi
  • send
    cat /usr/local/bin/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 domain  osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm if [[ $NAME = "iTerm" ]]; then     FILE=`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)&\"\")"` else     FILE=`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 = "" ]]; then     echo Cancelled.     # Send ZModem cancel     echo -e \\x18\\x18\\x18\\x18\\x18     sleep 1     echo     echo \# Cancelled transfer else     /opt/homebrew/bin/sz "$FILE" -e -b     sleep 1     echo     echo \# Received $FILE fi

  • 配置Triggers

  • Regular expression: rz waiting to receive.\*\*B0100             Action: Run Silent Coprocess         Parameters: /usr/local/bin/iterm2-send-zmodem.sh            Instant: checked  Regular expression: \*\*B00000000000000             Action: Run Silent Coprocess         Parameters: /usr/local/bin/iterm2-recv-zmodem.sh            Instant: checked

    * 如果是通过expect方式登录的服务器,需要做一下配置,写一个shell脚本加入               export LC_CTYPE=en_US

  • #!/bin/bash # $1 是expect脚本,其他的参数就是登录堡垒机和服务器的参数  if [ $# -ne 8 ];then     echo -e  "usage: $0 [EXPECT_SCRIPT_PATH] [JUMP_HOST] [JUMP_PORT] [JUMP_USER] [JUMP_PASSWORD] [HOST] [USER] [PASSWORD]\n\n\t"     echo -e "*JUMP开头\t是堡垒机的登录信息\n\t"     echo -e "*非JUMP开头\t是连接目标主机的登录信息."     exit fi  export LC_CTYPE=en_US  /usr/bin/expect $1 $2 $3 $4 $5 $6 $7 $8     # 运行示例  # bash /Users/test_user/scripts/login.sh /Users/test_user/scripts/remote.exp 172.88.88.88 22 test_user test_user_password 172.77.77.77 root 'root_password'  # 如果使用了这种方式的话,iterm2 profile 也需要对应的修改一下

相关内容

热门资讯

一分钟内幕!科乐吉林麻将系统发... 一分钟内幕!科乐吉林麻将系统发牌规律,福建大玩家确实真的是有挂,技巧教程(有挂ai代打);所有人都在...
一分钟揭秘!微扑克辅助软件(透... 一分钟揭秘!微扑克辅助软件(透视辅助)确实是有挂(2024已更新)(哔哩哔哩);1、用户打开应用后不...
五分钟发现!广东雀神麻雀怎么赢... 五分钟发现!广东雀神麻雀怎么赢,朋朋棋牌都是是真的有挂,高科技教程(有挂方法)1、广东雀神麻雀怎么赢...
每日必看!人皇大厅吗(透明挂)... 每日必看!人皇大厅吗(透明挂)好像存在有挂(2026已更新)(哔哩哔哩);人皇大厅吗辅助器中分为三种...
重大科普!新华棋牌有挂吗(透视... 重大科普!新华棋牌有挂吗(透视)一直是有挂(2021已更新)(哔哩哔哩)1、完成新华棋牌有挂吗的残局...
二分钟内幕!微信小程序途游辅助... 二分钟内幕!微信小程序途游辅助器,掌中乐游戏中心其实存在有挂,微扑克教程(有挂规律)二分钟内幕!微信...
科技揭秘!jj斗地主系统控牌吗... 科技揭秘!jj斗地主系统控牌吗(透视)本来真的是有挂(2025已更新)(哔哩哔哩)1、科技揭秘!jj...
1分钟普及!哈灵麻将攻略小,微... 1分钟普及!哈灵麻将攻略小,微信小程序十三张好像存在有挂,规律教程(有挂技巧)哈灵麻将攻略小是一种具...
9分钟教程!科乐麻将有挂吗,传... 9分钟教程!科乐麻将有挂吗,传送屋高防版辅助(总是存在有挂)1、完成传送屋高防版辅助透视辅助安装,帮...
每日必看教程!兴动游戏辅助器下... 每日必看教程!兴动游戏辅助器下载(辅助)真是真的有挂(2025已更新)(哔哩哔哩)1、打开软件启动之...