自动登录ssh或telnet脚本

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

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

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

配置样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"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

1
2
3
4
5
6
7
/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

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

附带脚本文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/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. 之后执行