美文网首页
脚本链接服务器

脚本链接服务器

作者: swoft_ | 来源:发表于2019-10-13 23:59 被阅读0次
  1. 简单链接(A->B)

假设你要连接的服务器地址为123.123.123.123,端口号为8888,用户名为root,密码为mimamima
编写shell文件"login_server.sh",并放置于目录/usr/local/bin/下(该目录可任意指定),文件内容如下:

spawn ssh -p 8888 root@123.123.123.123
expect "password:"
send "mimamima\r"
interact
expect eof

打开 iTem2 - Profiles - Open Profiles... - Edit Profiles...
Name栏任意填写一个名称
在Command栏选则Command并输入:
expect /usr/local/bin/login_server.sh
关闭即保存
之后就可以通过 Profiles 下的列表快速登陆服务器了
转载于:https://www.cnblogs.com/surfzjy/p/8681362.html

  1. 复杂链接(A -> 跳板机 -> B -> xx -> xx)

主要命令:expect

#!/bin/bash
# 登录信息
relay_host=".com" # relay机器的用户和地址
passwd="xxx" # relay用户的密码
secret="xxxxxxx" # 计算动态码的密钥串
user_host="rd@192.168.10.119"  #xxx2机器
passwd_relay="xxxx"
c_mysql="mysql -h192.168.5.58 -P6041 -uzyb_rd_r -ppdrucy2Jw9pu4Su"
# 本脚本运行的绝对地址
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

# 获得动态码 例如 014521-07
# ComputeCode是一个可执行文件,传入secret来计算动态码
# 详情请见博客: http://blog.csdn.net/qq_27068845/article/details/79636033
code_second=`$DIR/ComputeCode $secret`
# 验证码 截取014521-07中前6位
code=${code_second:0:6}
# 剩余秒数 此验证码剩余可用时间(秒)
second=${code_second:7:2}


# 打印正常文字,
# echo "动态码:$code , 剩余可用秒数: $second s"
# 输出 蓝色字, 谷歌的二次认证是一次性密码,30秒内只能使用一次
echo "\033[34m 动态码:$code , 剩余可用秒数: $second s \033[0m"

# 上面的目的是为了算出动态密码,下面才是主要交互。如果不需要谷歌二次认证,可以略过
# expect调用login脚本
# expect $DIR/input.sh $relay_host $code $passwd $user_host
expect -c "
    set timeout 10;
    spawn ssh $relay_host;
    expect {
        \"Verification code:\" { send \"$code\n\"; exp_continue}
        \"Password:\" { send \"$passwd\n\"; exp_continue}
        \"*relay*\" { send \"user_host \n\";}
    };
    interact
"

参考:https://blog.csdn.net/qq_27068845/article/details/79636033

相关文章

网友评论

      本文标题:脚本链接服务器

      本文链接:https://www.haomeiwen.com/subject/ghhlmctx.html