加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 16:28:39 所属栏目:安全 来源:网络整理
导读:#!/bin/bash########################author:Bovin########################showallhostinfosofserverList.txtif[[-f$HOME/.serverList.txt]]thenhostNum=`cat$HOME/.serverList.txt|wc-l`elseecho"No.serverList.txtin$HOMEdir,pleasecreateitandaddserveri
#!/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@%sn",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"yesr";exp_continue}
"*?assword:"{send"$passwdr"}
}
interact
"

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读