通过ssh实现登录服务器脚本
2020-07-09 07:37
507 查看
钉钉、微博极速扩容黑科技,点击观看阿里云弹性计算年度发布会!>>>
版本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 -> %s@%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 $user@$host" expect -c " set timeout 30 spawn ssh $user@$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\tUser@ServerIp"}{printf("%2d -> %s %s@%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 $user@$host" expect -c " set timeout 30 spawn ssh $user@$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地址;第四列:服务器密码。
ff9相关文章推荐
- 通过ssh实现登录服务器脚本
- 服务器间通过ssh使用密钥对实现无密码登录
- Centos-SSH多个服务器通过互信实现彼此之间免密登录
- iTerm2 结合 Linux expect 实现 ssh 自动登陆, 通过跳板机登录服务器内网功能
- 服务器间通过ssh使用密钥对实现无密码登录solaris,redhat,linux
- ssh,expect脚本实现远程登录,拷贝命令scp,rsync服务器的搭建
- 用了一天的时间,linux下expect实现ssh自动登录服务器记,鄙视下网上各种抄来抄去残段子
- 使用shell脚本自定义实现选择登录ssh
- 通过SSH实现Cisco路由器登录
- 用python的控件wx实现了windows通过ssh登录linux(学习一)
- 通过SSH证书实现Putty免密码登录CentOS
- 详解vsftp服务器通过匿名用户、本地用户、虚拟用户三种登录方式实现文件传输
- 【效率工具】SSH一键登录脚本(可一键从跳板机登录线上服务器)
- 用expect实现ssh自动登录对服务器进行批量管理
- 通过SSH证书实现Putty免密码登录CentOS
- 用expect实现ssh自动登录服务器并进行批量管理的实现方法
- ngrok内网穿透,ngrok服务器搭建,实现外网访问内网服务;树莓派外网访问内网的web服务及ssh外网登录
- 使用jsch,ssh协议远程操作服务器,登录,执行命令,上传脚本,调用无参,带参脚本等
- 通过mysqldump和SCP实现mysql定时自动备份到另外一台服务器脚本
- 通过ssh远程ipython notebook登录使用服务器