您的位置:首页 > 运维架构 > Shell

使用shell脚本自定义实现选择登录ssh

2016-08-18 19:13 676 查看
在系统bin目录中建立两个脚本分别是pssh tssh

pssh

#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set port [lindex $argv 1 ]
set password [lindex $argv 2 ]
set uname [lindex $argv 3 ]
set timeout 10
spawn ssh root@$ip -p$port
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\rclear\r\r\r请小心使用,您现在登录的服务器IP是:$ip,服务器名称是:$uname" }
}
interact


tssh

#!/bin/sh
echo "以下是你的所有服务器列表"
echo "1.A服务器 192.168.100.11"
echo "2.B服务器 192.168.100.12"
echo "3.C服务器 192.168.100.13"
echo "回车键退出"
echo "请输入对应数字,选择你要登录的服务器:\c"
read input
test $input = 0

case $input in
1)pssh 192.168.100.11 22 123456 A服务器;;
2)pssh 192.168.100.12 22 admin B服务器;;
3)pssh 192.168.100.13 22 root C服务器;;
esac


记得将给脚本加上可执行权限

然后在终端上执行tssh出现如下结果

以下是你的所有服务器列表
1.A服务器 192.168.100.11
2.B服务器 192.168.100.12
3.C服务器 192.168.100.13
回车键退出
请输入对应数字,选择你要登录的服务器:1
spawn ssh root@192.168.100.11 -p22
CentOS release 6.8 (Final)
Kernel \r on an \m
root@192.168.100.11's password:
Last login: Thu Aug 18 18:34:58 2016 from 192.168.100.2

Welcome to aliyun Elastic Compute Service!

[root@Official ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: