尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Shell脚本实现自动登录服务器

Shell脚本实现自动登录服务器 1.登录脚本login_server.sh#!/bin/bash# ReferenceLinkhttps://yq.aliyun.com/articles/516347#show all host infos of serverList.txtif[[-f./serverList.txt]]thenhostNumcat./serverList.txt|wc-lelseechoNo .serverList.txt in ./ dir, please create it and add server infos.exitfiwhile[True]doecho-e Host List awk-F {printf(%3d - %s%s\n, NR,$1,$2)}./serverList.txtecho-eecho-eEnter hostID at first column.echo-eEnter q or Q to quit.readhostIDif[[$hostIDq]]||[[$hostIDQ]]thenexitelif[[$hostID-lt1]]||[[$hostID-gt$hostNum]]thenechoWrong hostID is selected, Only$hostNumhosts are listed, please check.continueelsebreakfidoneuserhostpasswdeval$(awk-vhostID$hostID -F {if (NRhostID) {printf(user%s;host%s;passwd%s;,$1,$2,$3);}}./serverList.txt)#echo $user, $host, $passwdechologin in$user$hostexpect-c set timeout 30 spawn ssh$user$hostexpect {\*yes/no\{ send\yes\r\; exp_continue }\*?assword:\{ send\$passwd\r\} } interact 2.服务器列表serverList.txt第一列系统用户名称第二列服务器ip地址第三列服务器密码。test1 192.168.1.222 123456 bt 192.168.1.101 123456 st 192.168.1.103 123456
返回列表