自动登录ssh或telnet脚本

闲来无事,正好更新下以前写的脚本,自动登录ssh或telnet。
而且也方便记录密码。Git-oschinaGitHub

使用connect.sh自动连接ssh或者telnet

其中配置文件conn.profile为json格式

配置样例:

{
   "test1": {
       "host": "192.168.0.1",
       "port": 22,
       "user": "root",
       "pass": "123456",
       "connecttype": "ssh",
       "sshtype": "pass",
       "language": "None"
   },
   "test2": {
       "host": "192.168.0.1",
       "port": 22,
       "user": "root",
       "pass": "123456",
       "connecttype": "ssh",
       "sshtype": "~/.ssh/test_rsa",
       "language": "en_US.UTF-8"
   },
   "test3": {
       "host": "192.168.0.1",
       "port": 23,
       "user": "root",
       "pass": "123456",
       "connecttype": "telnet",
       "language": "en_US.UTF-8"
   }
}

可以自定义设置别名

echo ‘aliases=”$HOME/Documents/ssh/connect.sh”‘ >> ~/.bashrc

之后执行

connect show test1

/test1/host               192.168.0.1
/test1/port               22
/test1/user               root
/test1/pass               123456
/test1/connecttype        ssh
/test1/sshtype            pass
/test1/language           en_US.UTF-8

connect test1

Last login: Mon Aug  8 23:48:46 2016 from gateway
[root@clusterA31 ~]#

附带脚本文件

#!/bin/sh

[ -z "$CONNJSON" ] && _BJSON=$HOME/Documents/ssh/json.sh || _BJSON=$CONNJSON
[ -z "$CONNCFG" ] && _C_CFG=$HOME/Documents/ssh/conn.profile || _C_CFG=$CONNCFG
if [ ! -f $_C_CFG ]; then
    echo "not fount profile file $_C_CFG"
    exit 0
fi

source $_BJSON

function connect() {
    name=$1
    config=`json < $_C_CFG | grep $name`
    row=`json < $_C_CFG | grep $name | wc -l`
    if [ $row -gt 7 ]; then
        echo "fount to many configs\n$config\n"
        exit 0
    fi

    connhost=`json < $_C_CFG | grep $name | grep "host" | grep -v grep | awk '{print \$3}'`
    if [ -z $connhost ]; then
        echo error:  没有那个IP
        exit 0
    fi
    connport=`json < $_C_CFG | grep $name | grep "port" | grep -v grep | awk '{print \$3}'`
    connuser=`json < $_C_CFG | grep $name | grep "user" | grep -v grep | awk '{print \$3}'`
    connpass=`json < $_C_CFG | grep $name | grep "pass" | grep -v grep | awk '{print \$3}'`
    connlang=`json < $_C_CFG | grep $name | grep "lang" | grep -v grep | awk '{print \$3}'`
    conntype=`json < $_C_CFG | grep $name | grep "conn" | grep -v grep | awk '{print \$3}'`
    conntssh=`json < $_C_CFG | grep $name | grep "ssht" | grep -v grep | awk '{print \$3}'`

    [ -z "$connport" ] && connport=22
    [ -z "$connuser" ] && connuser='root'
    [ -z "$connpass" ] && connpass='123456'
    [ -z "$connlang" ] && connlang='None'
    [ -z "$conntype" ] && conntype='ssh'
    [ -z "$conntssh" ] && conntssh='pass'

    if [ "$connlang" != "None" ]; then
        locallang=$LANG
        locallcall=$LC_ALL
        export LANG=$connlang
        export LC_ALL=$connlang
    fi

    if [ "$conntype" == "ssh" ]; then
        connstr="spawn ssh -p${connport} ${connuser}@${connhost}
                 expect {
                        \"*(yes/no)*\" { send \"yes\n\"; exp_continue }
                    -re \"assword:|assw\" { send \"${connpass}\n\" }
                 }"
        if [ "$conntssh" != "pass" ]; then
            connstr="spawn ssh -p${connport} ${connuser}@${connhost} -i${conntssh}
                     expect {
                            \"*(yes/no)*\" { send \"yes\n\" }
                        -re \">|]|$|#\" { send \"\n\" }
                     }"
        fi
        expect -c "
                set timeout 10000
                stty -echo
                ${connstr}

                if { \"${connlang}\" != \"None\" } {
                    expect {
                        -re \">|]|$|#\" { send \"export LANG=${connlang} && export LC_ALL=${connlang}\n\" }
                    }
                }
                interact 
                  "
    elif [ "$conntype" == "telnet" ]; then
        echo $connlang
        expect -c "
                set timeout 10000
                stty -echo
                spawn telnet ${connhost} ${connport}
                expect {
                        \"*(yes/no)*\" { send \"yes\n\"; exp_continue }
                        \"*ogin*\" { send \"${connuser}\n\"; exp_continue }
                    -re \"assword:|assw\" { send \"${connpass}\n\" }
                }

                if { \"${connlang}\" != \"None\" } {
                    expect {
                        -re \">|]|$|#\" { send \"export LANG=${connlang} && export LC_ALL=${connlang}\n\" }
                    }
                }
                interact 
                  "
    fi

    if [ "$connlang" != "None" ]; then
        locallang=$LANG
        locallcall=$LC_ALL
        export LANG=$locallang
        export LC_ALL=$locallcall
    fi
}

function showconn() {
    json < $_C_CFG | grep $1 | awk '{printf("%-25s\t%s\n", $1, $3)}'
}

case "$1" in 
    show)
        showconn $2
        ;;
    help)
        echo "$0 onename\n$0 show onename" >&2
        ;;
    *)
        connect $1
        ;;
esac

exit 0
文章目录
  1. 使用connect.sh自动连接ssh或者telnet
    1. 配置样例:
    2. 可以自定义设置别名
    3. 之后执行

最近访客