크로스보드 서버 셸 스크립트 로그인
29429 단어 linux 명령 및 사용 기교
expect 셸 자동 스프링보드 넘어 서버 로그인
#!/usr/bin/expect -f
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 [email protected]
for {set i 0} {$i < 10} {incr i} {
expect {
"*assword:" {
send "$password\r"
exp_continue
}
"*code:*" {
expect_user -re "(.*)
"
set code $expect_out(1,string)
puts "your code is $code
"
send "$code\r"
exp_continue
}
"*@relay:~*" {
puts "
please select your server:"
set serverHostCount [array size serverHost]
for {set k 0} {$k < $serverHostCount} {incr k} {
puts $k,$serverHost($k)
}
expect_user -re "(.*)
"
set serverSelected $expect_out(1,string)
puts "your select server is $serverSelected:$serverHost($serverSelected)
"
send "ssh $serverHost($serverSelected)\r"
break
}
}
}
interact
expect 셸 자동 스프링보드 넘어 서버 로그인
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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 # Assume
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
if __name__ == '__main__':
password = "*******"
serverHostArray = [
"algm-f-gmu-testing01.bj",
"algm-f-gmu-testing02.bj",
"tx4-inno-cr-test02.bj"
]
# spawn
child = pexpect.spawn('ssh [email protected]')
# expect ,
# pexpect
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
winsize = getwinsize();
child.setwinsize(winsize[0], winsize[1])
while True:
expectIndex = child.expect([
'assword:',
'code:',
'n@relay:~',
])
if expectIndex == 0:
child.sendline(password)
print ""
elif expectIndex == 1:
print "please enter Verification code:"
code = raw_input()
child.sendline(code)
elif expectIndex == 2:
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]),
print ""
indexStr = raw_input('please select your server:')
index = int(indexStr)
print index, serverHostArray[index]
child.sendline('ssh ' + serverHostArray[index])
break
child.interact()
print " "
expect 셸 자동 건너뛰기 서버 로그인 ②
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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 # Assume
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
return struct.unpack('HHHH', x)[0:2]
if __name__ == '__main__':
password = "xxxxxxxxx"
serverHostArray = [
"bjyz-xxxxxxxxxx.epc.cainiao.com",
]
# spawn
child = pexpect.spawn('ssh relay.cainiao-int.com')
#child.logfile = sys.stdout
# expect ,
# pexpect
signal.signal(signal.SIGWINCH, sigwinch_passthrough)
winsize = getwinsize();
child.setwinsize(winsize[0], winsize[1])
serverPasswordFlag = False
while True:
expectIndex = child.expect([
"password:",
'-bash-cainiao-ssl'
])
if expectIndex == 0:
child.sendline(password)
if serverPasswordFlag :
print ' ';
break;
elif expectIndex == 1:
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]),
print ""
indexStr = raw_input('please select your server:')
# host
if len(indexStr) > 10:
child.sendline('ssh wuchengjin@' + indexStr)
print ' ';
break;
index = int(indexStr)
print index, serverHostArray[index]
#
if index == 0 :
child.sendline('ssh work@' + serverHostArray[index])
else:
child.sendline('ssh wuchengjin@' + serverHostArray[index])
print ' ';
break;
else:
print 'break'
break
child.interact()
print " "