通过ssh实现登录服务器脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过ssh实现登录服务器脚本相关的知识,希望对你有一定的参考价值。
版本v1
#!/bin/bash ####################### #author: Bovin ####################### #show all host infos of serverList.txt if [[ -f $HOME/.serverList.txt ]] then hostNum=`cat $HOME/.serverList.txt | wc -l` else echo "No .serverList.txt in $HOME dir, please create it and add server infos." exit fi while [ True ] do echo -e "+++++++++++ Host List ++++++++++++++++" awk -F‘ ‘ ‘{printf("%3d -> %[email protected]%s\n", NR,$1,$2)}‘ $HOME/.serverList.txt echo -e "++++++++++++++++++++++++++++++++++++++" echo -e "Enter hostID at first column." echo -e "Enter q or Q to quit." read hostID if [[ "$hostID" == ‘q‘ ]] || [[ "$hostID" == ‘Q‘ ]] then exit elif [[ $hostID -lt 1 ]] || [[ $hostID -gt $hostNum ]] then echo "Wrong hostID is selected, Only $hostNum hosts are listed, please check." continue else break fi done user="" host="" passwd="" eval $(awk -v hostID=$hostID -F‘ ‘ ‘{if (NR==hostID) {printf("user=%s;host=%s;passwd=%s;",$1,$2,$3);}}‘ $HOME/.serverList.txt) #echo $user, $host, $passwd echo "login in [email protected]$host" expect -c " set timeout 30 spawn ssh [email protected]$host expect { \"*yes/no\" { send \"yes\r\"; exp_continue } \"*?assword:\" { send \"$passwd\r\" } } interact "
说明:此脚本读取.serverList文件,.serverList文件存有服务器信息。内容如下:
qindy 10.24.34.69 qindy root 10.24.181.140 passWord wrf 10.24.185.18 wrf
第一列:系统用户名称;第二列:服务器ip地址;第三列:服务器密码。
版本v2
#!/bin/bash ####################### #author: Bovin ####################### #show all host infos of serverList.txt if [[ -f /root/exec/iplist.txt ]] then hostNum=`cat /root/exec/iplist.txt | wc -l` else echo "No iplist in dir, please create it and add server infos." exit fi while [ True ] do echo -e "+++++++++++ Host List ++++++++++++++++" awk -F‘ ‘ ‘BEGIN {print "ID\tServerName\[email protected]"}{printf("%2d -> %s %[email protected]%s\n", NR,$1,$2 ,$3)}‘ /root/exec/iplist.txt echo -e "++++++++++++++++++++++++++++++++++++++" echo -e "Enter hostID at first column." echo -e "Enter q or Q to quit." read hostID if [[ "$hostID" == ‘q‘ ]] || [[ "$hostID" == ‘Q‘ ]] then exit elif [[ $hostID -lt 1 ]] || [[ $hostID -gt $hostNum ]] then echo "Wrong hostID is selected, Only $hostNum hosts are listed, please check." continue else break fi done servername="" user="" host="" passwd="" eval $(awk -v hostID=$hostID -F‘ ‘ ‘{if (NR==hostID) {printf("servername=%s;user=%s;host=%s;pas swd=%s;",$1,$2,$3,$4);}}‘ /root/exec/iplist.txt) #echo $user, $host, $passwd echo "logining $servername by [email protected]$host" expect -c " set timeout 30 spawn ssh [email protected]$host expect { \"*yes/no\" { send \"yes\r\"; exp_continue } \"*?assword:\" { send \"$passwd\r\" } } interact "
说明:此脚本读取iplist.txt文件,iplist.txt文件存有服务器信息。内容如下:
centos-test1 root 172.16.172.151 yunjikeji centos-test2 root 172.16.172.152 yunjikeji centos-test3 root 172.16.172.153 yunjikeji
第一列:服务器名称;第二列:系统用户名;第三列:服务器ip地址;第四列:服务器密码。
本文出自 “Hello,World!” 博客,转载请与作者联系!
以上是关于通过ssh实现登录服务器脚本的主要内容,如果未能解决你的问题,请参考以下文章
通过 Python 脚本拒绝所有用户基于密码的 SSH 登录