微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过ssh实现登录服务器脚本

#!/bin/bash
#######################
#author:Bovin
#######################
#showallhostinfosofserverList.txt
if[[-f$HOME/.serverList.txt]]
then
hostNum=`cat$HOME/.serverList.txt|wc-l`
else
echo"No.serverList.txtin$HOMEdir,pleasecreateitandaddserverinfos."
exit
fi
while[True]
do
echo-e"+++++++++++HostList++++++++++++++++"
awk-F'''{printf("%3d->%s@%s\n",NR,$1,$2)}'$HOME/.serverList.txt
echo-e"++++++++++++++++++++++++++++++++++++++"
echo-e"EnterhostIDatfirstcolumn."
echo-e"EnterqorQtoquit."
readhostID
if[["$hostID"=='q']]||[["$hostID"=='Q']]
then
exit
elif[[$hostID-lt1]]||[[$hostID-gt$hostNum]]
then
echo"WronghostIdisselected,Only$hostNumhostsarelisted,pleasecheck."
continue
else
break
fi
done
user=""
host=""
passwd=""
eval$(awk-vhostID=$hostID-F'''{if(NR==hostID){printf("user=%s;host=%s;passwd=%s;",$2,$3);}}'$HOME/.serverList.txt)
#echo$user,$host,$passwd
echo"loginin$user@$host"
expect-c"
settimeout30
spawnssh$user@$host
expect{
\"*yes/no\"{send\"yes\r\";exp_continue}
\"*?assword:\"{send\"$passwd\r\"}
}
interact
"

说明:此脚本读取.serverList文件,.serverList文件存有服务器信息。内容如下

qindy10.24.34.69qindy
root10.24.181.140passWord
wrf10.24.185.18wrf

第一列:服务器名称;第二列:服务器ip地址;第三列:服务器密码。

原文地址:https://www.jb51.cc/bash/391288.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐