场景
当我们维护一些服务器的时候,经常会面临密码呀端口账号呀啥的问题,使得维护时麻烦不小,再加上有时候网络不稳定,SA的脾气就更大了。
解决方案
简单的几个脚本
goto > 用户交互入口
denglu > 采用goto传过来的参数,使用expect命令进行模拟登陆
list > 服务器密码资源列表
goto
#!/usr/bin/env python
#coding: utf8
import os,sys,re
import subprocess
import datetime
host = sys.argv[1]
if host == "list":
print "进入"
for line in open("./list"):
result = re.findall("(\S+)",line)
#print len(result)
if len(result) >=5:
print "%s %s"%(result[0],result[5])
sys.exit()
matchedline = []
for line in open("/Users/jacky/bin/list"):
result = re.findall(host,line)
if len(result) == 1:
matchedline.append(line)
if len(matchedline) == 1:
secretline=matchedline[0]
print "Found the machted line."
value = re.findall("\S+",secretline)
if len(value) >= 5:
print "... Mached Line Found!!!"
# print value
else:
print "Please check your config file list."
exit(1)
#print value
host = value[0]
passwd = value[1]
port = value[2]
user = value[3]
prompt = value[4]
if len(value) >= 6:
description = value[5]
#print host
#print passwd
#print port
#print user
print "Welcome Login --> %s"%(description)
#os.system("/Users/jacky/bin/denglu2 %s \'%s\' %s %s \"%s\""%(host, passwd,port,user,prompt))
os.system("/Users/jacky/bin/denglu2 %s \'%s\' %s %s \"%s\""%(host, passwd,port,user,prompt))
else:
print "No Not Find Matched. Exit............."
denglu2
#!/usr/bin/expect -f
log_file /tmp/1.txt
# This is very important.
set HOST [lindex $argv 0]
set serverpassword [lindex $argv 1]
set port [lindex $argv 2]
set user [lindex $argv 3]
set prompt [lindex $argv 4]
set timeout 2
#send_user "$serverpassword"
#exit
# connect via ssh
spawn ssh -o StrictHostKeyChecking=no $user@$HOST -p $port
#######################
expect {
-re "yes/no.*" {
exp_send "yes\r"
exp_continue
}
}
expect {
-re "password:.*" {
exp_send "$serverpassword"
exp_send "\r"
}
-re "$prompt.*" {
interact
exit
}
-re "password:.*" {
exp_send "$serverpassword"
exp_send "\r"
}
}
expect {
-re "$prompt.*" {
interact
exit
}
}
list
格式如下
IP地址 密码 端口 账户 (>或者$或者#提示符) 描述
使用
goto 唯一定位到条目(地址呀 描述呀 账号呀啥的)

本文介绍了一种通过简单脚本实现服务器快速登录的方法,解决了维护过程中常见的密码、端口及账号等问题,提高了网络不稳定情况下的工作效率。
112

被折叠的 条评论
为什么被折叠?



