expect shell 自动越过跳板机登陆服务器
set password "123"
set serverHost(0) "tx3-testad01.bj"
set serverHost(1) "tx3-testad01.bj"
set serverHost(2) "tx3-server-ad02.bj"
set serverHost(3) "tx3-server-ad03.bj"
set serverHost(4) "tx4-server-ad04.bj"
set serverHost(5) "tx3-server-ad05.bj"
set serverHost(6) "tx4-server-ad06.bj"
set serverHost(7) "tx3-server-ad07.bj"
spawn ssh wuchengjin@relay.inke.cn
for {set i 0} {$i < 10} {incr i} {
expect {
"*assword:" {
send "$password\r"
exp_continue
}
"*code:*" {
expect_user -re "(.*)\n"
set code $expect_out(1,string)
puts "your code is $code\n"
send "$code\r"
exp_continue
}
"*@relay:~*" {
puts "\nplease select your server:"
set serverHostCount [array size serverHost]
for {set k 0} {$k < $serverHostCount} {incr k} {
puts $k,$serverHost($k)
}
expect_user -re "(.*)\n"
set serverSelected $expect_out(1,string)
puts "your select server is $serverSelected:$serverHost($serverSelected)\n"
send "ssh $serverHost($serverSelected)\r"
break
}
}
}
interact
expect shell 自动越过跳板机登陆服务器
import pexpect
import sys
import math
import signal
import fcntl
import termios
import struct
def sigwinch_passthrough (sig, data):
winsize = getwinsize()
global child
child.setwinsize(winsize[0],winsize[1])
def getwinsize():
"""This returns the window size of the child tty.
The return value is a tuple of (rows, cols).
"""
if 'TIOCGWINSZ' in dir(termios):
TIOCGWINSZ = termios.TIOCGWINSZ
else:
TIOCGWINSZ = 1074295912L
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
relayPassword = "*****"
serverHostArray = [
{"host":"bjyz-888.epc.baidu.com","user":"work","password":"*****"},
{"host":"bjhw-feed-8888.bjhw","user":"wuchengjin","password":""},
{"host":"njjs-feed-8888.njjs","user":"wuchengjin","password":""},
{"host":"szth-feed-8888.szth","user":"wuchengjin","password":""},
{"host":"gzhl-feed-8888.gzhl","user":"wuchengjin","password":""},
{"host":"bjhw-gh-8888.bjhw","user":"work","password":"888"},
{"host":"njjs-gh-8888.njjs","user":"work","password":"888"},
]
serverIndex = 0
if __name__ == '__main__':
child = pexpect.spawn('ssh relay.baidu-int.com')
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
winsize = getwinsize();
child.setwinsize(winsize[0], winsize[1])
while True:
expectIndex = child.expect([
"wuchengjin@relay.baidu-int.com's password:",
"password:",
"-bash-baidu-ssl"
],)
if expectIndex == 0:
child.sendline(relayPassword)
elif expectIndex == 1:
child.sendline(serverHostArray[serverIndex]["password"])
break
elif expectIndex == 2:
serverPasswordFlag = True;
length = len(serverHostArray)
lines = math.ceil(float(length) / 3)
for line in range(int(lines)):
for column in range(3):
mat = "{:<5}\t{:<40}\t"
index = int(lines) * column + line
if index < length :
print mat.format(index, serverHostArray[index]["host"]),
print ""
indexStr = raw_input('please select your server:')
if len(indexStr) > 10:
child.sendline('ssh wuchengjin@' + indexStr)
break;
serverIndex = int(indexStr)
print serverIndex, serverHostArray[serverIndex]["host"]
child.sendline('ssh %s@%s' % (serverHostArray[serverIndex]["user"], serverHostArray[serverIndex]["host"]))
if len(serverHostArray[serverIndex]["password"]) == 0:
break
print '登录成功';
child.interact()
print "程序退出"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)