zoukankan      html  css  js  c++  java
  • mac 利用 sshpass 自动登录

    mac 利用 sshpass  + 配置文件 实现自动登录 

     使用方式  https://github.com/vipzhicheng/go  参见此项目

    其实原理也就是 脚本 读取配置文件 匹配 参数或者 ip 调用 ssh 命令  简单 mark下  

     按照链接方式配置好之后 

    shell 中 执行   go (参数) 

    例如 我配置


    # IP USER:PASS Label
    120.27.96.210(你的 ip)  你的用户名:你的密码  label:210

    执行 go 210 

    直接 ssh 登录到 目标 ip  

    #!/bin/bash

    # Define config directory and config path
    GO_HOME=~
    AUTHFILE=$GO_HOME/.ssh/pass/.go.conf

    # Parse options
    while getopts gh ARGS
    do
    case $ARGS in
    g)
    extra_options="-D7070"
    shift
    ;;
    h)
    echo "usage: go [-gh] foo bar"
    exit 0;
    ;;
    *)
    echo "Unknow option: $ARGS"
    echo "usage: go [-gh] foo bar"
    exit 1;
    ;;
    esac
    done

    # Config search function
    # Support AND logic, separated by blank char
    GREP()
    {

    if [ -z "$*" ]; then
    var="^[^#].*"
    tmp_var=`cat $AUTHFILE | grep -i $var`
    echo "$tmp_var"
    return
    fi

    index=1;
    for var in $@
    do
    if [[ "$var" =~ ^- ]];then
    index=$((index+1));
    continue
    fi

    if [ "$var" = "${!index}" ];then
    var="^[^#].*$var.*"
    tmp_var=`grep -i $var $AUTHFILE`
    else
    var="^[^#].*$var.*"
    tmp_var=`echo "$tmp_var" | grep -i $var`
    fi
    done

    echo "$tmp_var"
    }

    # Get choice
    GET_CHAR()
    {
    read choice
    echo $choice;
    }

    # Support input keywords by arguments or read from command line input
    if [ -z $1 ];then
    echo -n "Please input the server IP or Label: "
    read target
    else
    target=$@
    fi

    # Parse config search result
    count=`GREP $target | wc -l`
    targetfullname=`GREP $target | awk '{print $1}' | awk -F ':' '{print $1}'`
    port=`GREP $target | awk '{print $1}' | awk -F ':' '{if ($2 > "") {print $2} else {print 22}}'`
    passwd=`GREP $target | awk '{print $2}' | awk -F ':' '{if ($2 > "") {print $2} else {print "-"}}'`
    sshkey=`GREP $target | awk '{print $2}' | awk -F ':' '{if ($3 > "") {print $3} else {print "-"}}'`
    user=`GREP $target | awk '{print $2}' | awk -F ':' '{print $1}'`
    label=`GREP $target | awk '{print $3}'`

    # Process the case of more than one items in search results.
    if [ $count -gt 1 ];then
    echo -e 'Found follow servers: (33[0;31mWhich one do you want to connect?33[0m)'

    arrtarget=($targetfullname)
    arruser=($user)
    arrpasswd=($passwd)
    arrlabel=($label)
    arrport=($port)
    arrsshkey=($sshkey)

    length=${#arrtarget[@]}

    for ((i=0; i<$length; i++))
    do
    echo -e '[33[4;34m'$(($i+1))'33[0m]' "${arruser[$i]}@${arrtarget[$i]}:${arrport[$i]} ${arrlabel[$i]}"
    done

    # Choose one from search results
    echo -n "Please choose by ID: "
    choice=`GET_CHAR`
    echo ""

    echo $choice;

    if [[ "$choice" =~ ^[0-9]+$ ]]; then
    echo '';
    else
    exit 1;
    fi

    targetfullname=${arrtarget[$(($choice-1))]}
    passwd=${arrpasswd[$(($choice-1))]}
    user=${arruser[$(($choice-1))]}
    label=${arrencoding[$(($choice-1))]}
    port=${arrport[$(($choice-1))]}
    sshkey=${arrsshkey[$(($choice-1))]}
    fi

    # Bad cases
    if [ -z $targetfullname ] || [ -z $user ];then
    echo "No matching server~";
    exit 1;
    fi
    target=$targetfullname

    # Any key value should not be empty
    if [ -z $port ]; then
    port=22
    fi

    if [ -z $passwd ]; then
    passwd=-
    fi

    if [ -z $extra_options ]; then
    extra_options=-
    fi

    if [ -z $sshkey ]; then
    sshkey=-
    fi

    echo "Logging into ${user}@${target} ${label}..."

    sshpass -p $passwd -P $port ssh $user@$target //利用 sshpass 不会出现第一个命令 会卡住现象 expect命令会出现 原因不详

    #ssh-expect $user $target $passwd $port $extra_options $sshkey

    参考 https://github.com/vipzhicheng/go  (主要采用)
           https://github.com/whorusq/ssh-autologin/blob/master/goto.sh  可参考

          https://github.com/Kuchenm0nster/ssh-config-manager/blob/master/ssh-manager.sh 可优化

  • 相关阅读:
    并发编程2(并发编程1已记录完毕,可去前面文章翻找)
    服务器启动django项目
    大四实习期间公司遇到的一些知识点
    列表推导式、生成器表达式
    brewhome基本使用
    python float的四舍五入
    爬取狮城bbs困扰了我一天的Python基础题
    python pip安装模块失败的原因
    stringutil stringutils
    echars的使用
  • 原文地址:https://www.cnblogs.com/xmanblue/p/7572170.html
Copyright © 2011-2022 走看看