;サーバを自動設定ツール
;作成者:金忠傑
; copyleft by jinzhongjie(xjinzhongjie@hotmail.com).any one who want change the code plese tell wirter.
; Everyone is permitted to copy and distribute verbatim copies
; of this license document, but changing it is not allowed.
;使える環境
;host --  踏み台  -------------- 踏み台
;　　　　　　|----- サーバ1        |----- サーバ1
;　　　　　　|----- サーバ2          |----- サーバ2
;　　　　　　|----- ... ...          |----- ... ...
;          ... ...
;            |---VM master
;                    |--- VM サーバ1
;                    |--- VM サーバ2
;                ... ...
;----------------------------------------------------------------------------------------
; command list 
;-------------------------------
; download,remoto path,[local path(or filename)]
; upload,local path(or file name),remoto path
; cmd,
; ans,wait word,answer
; id,user id,password
; sleep,time(s)
; ssh,user id,[password],servername(or ip)
; check,wait word,time(s),cmd    ## wait word for cmd 
; svn,check out from, check out to,svn user,svn password
; log,log file
;----------------------------------------------------------------------------------------
;

language=0

if language==0 then
	exp_lang = 'export LANG=en_US.UTF-8'
	login_kvm = 'Escape character is'
	start_vm = 'started'
elseif language==1 then
	exp_lang = 'export LANG=ja_JP.UTF-8'
	login_kvm = 'エスケープ文字は'
	start_vm = '起動されました'
endif


goto main_start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;手動で設定　部分
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:ladder_setting
	ladder_hostname_tbl[0]=""
	ladder_hostaddr_tbl[0]=""
	ladder_username_tbl[0]=""
	ladder_password_tbl[0]=""
	ladder_hostaddr_inner_tbl[0]=""
	;;  ::1::   geon-sub
	ladder_hostname_tbl[1]="geon-sub"
	ladder_username_tbl[1]="hayabusa"
	ladder_password_tbl[1]="hayabusa"
	ladder_hostaddr_tbl[1]="10.22.98.208"
	ladder_hostaddr_inner_tbl[1]="192.168.100.1"

	;;  ::2::  bluesonic
	ladder_hostname_tbl[2]="bluesonic"
	ladder_username_tbl[2]="hayabusa"
	ladder_password_tbl[2]="hayabusa"
	ladder_hostaddr_tbl[2]="10.22.98.170"
	;;ladder_hostaddr_inner_tbl[2]="10.22.98.170"
	ladder_hostaddr_inner_tbl[2]="192.168.0.1"
    
	;;  ::3:: snv-springboard:192.168.100.1
	ladder_hostname_tbl[3]="snv-springboard"
	ladder_username_tbl[3]="root"
	ladder_password_tbl[3]="snv123"
	ladder_hostaddr_tbl[3]="10.22.52.113"
	ladder_hostaddr_inner_tbl[3]="192.168.100.1"

	;;  ::3:: snv-springboard:192.168.101.1
	ladder_hostname_tbl[4]="snv-springboard"
	ladder_username_tbl[4]="root"
	ladder_password_tbl[4]="snv123"
	ladder_hostaddr_tbl[4]="10.22.52.113"
	ladder_hostaddr_inner_tbl[4]="192.168.101.1"
    
	;;  ::5:: snv-springboard:192.168.104.1
	ladder_hostname_tbl[5]="snv1fgw"
	ladder_username_tbl[5]="snv"
	ladder_password_tbl[5]="snv123"
	ladder_hostaddr_tbl[5]="10.22.98.210"
	ladder_hostaddr_inner_tbl[5]="192.168.104.1"
	
	;;--------------------------------------------------
	ladder_hostname_tbl[8]="CentOs65x64_server"
	ladder_username_tbl[8]="jin"
	ladder_password_tbl[8]="jin123"
	ladder_hostaddr_tbl[8]="CentOs65x64_server"
	ladder_hostaddr_inner_tbl[8]="192.168.10.1"
	
	;;--------------------------------------------------
	ladder_hostname_tbl[9]="CentOs68x64_server"
	ladder_username_tbl[9]="jin"
	ladder_password_tbl[9]="jin123"
	ladder_hostaddr_tbl[9]="CentOs68x64_server"
	ladder_hostaddr_inner_tbl[9]="192.168.1.254"

	;;--------------------------------------------------
	ladder_hostname_tbl[9]="raspberrypi1"
	ladder_username_tbl[9]="pi"
	ladder_password_tbl[9]="jin123"
	ladder_hostaddr_tbl[9]="raspberrypi"
	ladder_hostaddr_inner_tbl[9]="192.168.1.251"
	
	
	ladder_hostname_tbl[14]="hayabusa.ss.ts.fujitsu.co.jp"
	ladder_username_tbl[14]="hayabusa"
	ladder_password_tbl[14]="hayabusa"
	ladder_hostaddr_tbl[14]="hayabusa.ss.ts.fujitsu.co.jp"
	ladder_hostaddr_inner_tbl[14]="0.0.0.0"
	
return


}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 日時から保存ファイル名を組み立てる
/*
msg2 = "VM_setting"
getdate timestamp "_%Y%m%d_%H%M%S_command.log"
strconcat msg2 timestamp
;strconcat logfile msg2
fileopen flog_handle msg2 0
*/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ログの記録開始
/*
logopen logfile 0 0
*/

;;/////////////////////////////////////////////////////////////
:main_start
log_flag=0
comment_flag=0

login_id=""
login_password=""

vm_master_old=""
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;=========================================================================
line2_index=0
line2_max=500
line2_count=0
strdim	line2	line2_max

;ip for vm host
;=========================================================================
ladder_index=0
ladder_count_max=15
ladder_count=0
strdim ladder_username_tbl	ladder_count_max
strdim ladder_password_tbl	ladder_count_max
strdim ladder_hostaddr_tbl	ladder_count_max
strdim ladder_hostname_tbl	ladder_count_max
strdim ladder_hostaddr_inner_tbl	ladder_count_max
intdim ladder_memory_tbl	ladder_count_max
;=========================================================================

;string  Variable
;=========================================================================
variable_index=0
variable_count_max=100
variable_count=0
strdim variable_name_tbl	variable_count_max
strdim variable_string_tbl	variable_count_max
;=========================================================================


call ladder_setting

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;=========================================================================

field_index=0
field_count=20
strdim field field_count

;ip for vm host 
vm_host_index=0
vm_host_count=100
strdim vm_host_name_tbl vm_host_count
strdim vm_host_ip_tbl vm_host_count

;;for upload
uploaded_index=0
uploaded_count=100
strdim uploaded_filename_tbl uploaded_count

;;for download
downloaded_index=0
downloaded_count=100
strdim downloaded_filename_tbl downloaded_count

;;for su
su_index=0
su_count=10
strdim su_id_tbl su_count
strdim su_pwd_tbl su_count

CMD_NULL=0
CMD_LOG=1
CMD_LADDER=2
CMD_LADDER_END=3
CMD_VAR=4
CMD_VMMASTER=5
CMD_FUNCTION=6
CMD_CALL=7
CMD_SECTION_END=8
CMD_GUEST=9
CMD_ID_PSWD=10
CMD_CMD=11
CMD_UPLOAD=12
CMD_DOWNLOAD=13
CMD_SLEEP=14
CMD_SU_LOGIN=15
CMD_SSH_LOGIN=16
CMD_ANS=17
CMD_CHECK=18
CMD_SVN=19

command_num=0
notexe_flag=0

cmd_count=0
cmd_max=100
intdim command_num_tbl cmd_max
strdim parameter1_tbl cmd_max
strdim parameter2_tbl cmd_max
strdim parameter3_tbl cmd_max
strdim parameter4_tbl cmd_max
strdim parameter5_tbl cmd_max
intdim command_type_tbl cmd_max

stack_pos=0
stack_size=10
intdim stack_tbl stack_size

vm_username=""
vm_password=""
vm_guestname=""




/*
STA_NULL=0
STA_LADDER=1
STA_VM_MASTER=2
STA_VM_GUEST=3
STA_SERVER_NAME=4
STA_SERVER_IP=5
*/
STA_VM_NULL=0
STA_VM_MASTER=1
STA_VM_GUEST=2




;;現在の状態(経由踏み台とサバ台数)
sta_num=0

;;VM　server　から
sta_vm=STA_VM_NULL

;;踏み台経由時、サーバの順番。
;;踏み台がない時は1；踏み台が1の時は2。
sta_dest=0
vm_flag=0
vm_conn_flag=0

;;virsh で操作するかを判断
vm_use_flag=0


connect_flag=0


server=""

last_path=""
do_flag=0
;;su_flag=0
login_count=0
timeout = 3000

;=========================================================================


; ファイルオープン
file_open_flag=1
fileopen fhandle 'server-tool.txt' 0
if fhandle=-1 then
	message="他のeditorで排他モードでserver-list-tool.txtを開いてないかを確認ください"
	messagebox  message 'VM-list.txt読み込みエラー'
	exit
endif
comment_flag=0
cmd_count=0
while 1
	call read_setting
endwhile
:fclose
;;messagebox  sta_num 'next'
fileclose fhandle

;;messagebox  sta_num 'next'
sta_dest=0
call do_task
call do_clear_remain

;sendln 'exit'
;Wait '$'
exit

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;do_clear_remain
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:do_clear_remain
;;messagebox  sta_num 'do_clear_remain sta_num'
;;messagebox  sta_dest 'do_clear_remain sta_dest'

if sta_num > ladder_count then
	call disconnect_ladder
endif


if sta_num < ladder_count then
	until sta_num == ladder_count
		ladder_index=ladder_memory_tbl[sta_num+1]
		call connect_ladder
	enduntil
endif

;;messagebox  sta_num 'after fclose'
do while sta_num>1
	call do_clear
	call disconnect_ladder
loop
call do_clear
uploaded_index=0
downloaded_index=0

return

:do_clear
if uploaded_index >0 then
	for i 0 uploaded_index-1
		sprintf  'rm -f /tmp/%s'  uploaded_filename_tbl[i]
		sendln inputstr
		wait server_prompt
	next
endif

if downloaded_index >0 then
	for i 0 downloaded_index-1
		sprintf  'rm -f /tmp/%s'  downloaded_filename_tbl[i]
		sendln inputstr
		wait server_prompt
	next
endif
return
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;do_task
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:do_task
	;;messagebox  cmd_count  '-----cmd_count----'
	if cmd_count>0 then
		;;for ip 0 cmd_count-1
		ip=0
		do while ip < cmd_count
			;;messagebox  command_num_tbl[ip] 'do_commands command_num_tbl'
			if ip < cmd_count-1 then
				next_cmd=command_num_tbl[ip+1]
			else
				next_cmd=CMD_NULL
			endif
			command_num=command_num_tbl[ip]

			if command_num==CMD_SECTION_END then
				ii=stack_tbl[stack_pos]
				stack_pos=stack_pos-1
				command_num=command_num_tbl[ii]
				;;messagebox  ii  '-----func:ii----'
				;;messagebox  command_num  '-----func:command_num----'

				if command_num==CMD_FUNCTION  then
					notexe_flag=0
				elseif command_num==CMD_CALL then
					ip=ii
				elseif command_num==CMD_VMMASTER then
					vm_flag=0
					vm_master=''
					call disconnect_vm
				endif
				goto after_do_command
			endif
			if notexe_flag==1 then
				goto after_do_command
			endif

			for_replace=parameter1_tbl[ip]
			call get_variable
			parameter1=for_replace
			for_replace=parameter2_tbl[ip]
			call get_variable
			parameter2=for_replace
			for_replace=parameter3_tbl[ip]
			call get_variable
			parameter3=for_replace
			for_replace=parameter4_tbl[ip]
			call get_variable
			parameter4=for_replace
			for_replace=parameter5_tbl[ip]
			call get_variable
			parameter5=for_replace
			
			command_type=command_type_tbl[ip]
			;;messagebox  ip    '-----ip----'
			;;messagebox  command_num    '-----command_num_tbl----'
			;;call debug
			
			if command_num==CMD_LADDER then
				line = parameter1
				call ladder_memory_read
				login_count=1
			elseif command_num==CMD_LOG then
				log_flag=1
				logfile=parameter1
			elseif command_num==CMD_LADDER_END then
				call ladder_memory_clear
				connect_flag=0
				while sta_num > 0
					call disconnect_ladder
				endwhile
			elseif command_num==CMD_VAR then
				variable_name=parameter1
				variable_str=parameter2
				call set_variable
			elseif command_num==CMD_VMMASTER then
				vm_flag=1
				vm_master=parameter1
				stack_pos=stack_pos+1
				stack_tbl[stack_pos]=ip
			elseif command_num==CMD_CALL then
				stack_pos=stack_pos+1
				;;messagebox  ip    '-----ip----'
				;;messagebox  stack_pos    '-----stack_pos----'
				stack_tbl[stack_pos]=ip
				ip=command_type
			elseif command_num==CMD_FUNCTION then
				stack_pos=stack_pos+1
				stack_tbl[stack_pos]=ip
				notexe_flag=1
			elseif command_num==CMD_SECTION_END then
				ii=stack_tbl[stack_pos]
				stack_pos=stack_pos-1
				command_num=command_num_tbl[ii]
				if command_num==CMD_VMMASTER  then
					vm_flag=0
					vm_master=''
					call disconnect_vm
				endif
			elseif command_num==CMD_GUEST then
				;messagebox  sta_num    '-----CMD_GUEST----'
				if vm_flag==0 then
					strcompare ladder_hostname_tbl[sta_dest] parameter1
					if sta_num>0 && sta_num == sta_dest && result != 0 then
						call disconnect_server
					endif
					server_pre=parameter1
					sta_pre=ladder_count+1
					sta_dest=sta_pre
					call ladder_host_setting
				else
					strcompare vm_guestname parameter1
					if sta_vm==STA_VM_GUEST && result != 0 then
						call disconnect_server
					endif
					vm_guestname=parameter1
				endif
				login_count=0
			elseif command_num==CMD_ID_PSWD then
				if login_count==0 then
					if vm_flag==0 then
						ladder_index=ladder_memory_tbl[sta_dest]
						
						ladder_username_tbl[ladder_index]=parameter1
						ladder_password_tbl[ladder_index]=parameter2
					else
						vm_username=parameter1
						vm_password=parameter2
					endif
				else
					call connect_server
					;;messagebox  parameter1    '-----do_su_login----'
					call do_su_login
				endif
				login_count=login_count+1
			elseif command_num==CMD_CMD then
				call connect_server
				call do_cmd
			elseif command_num==CMD_ANS then
				call do_ans
			elseif command_num==CMD_CHECK then
				call do_check
			elseif command_num==CMD_SVN then
				call do_svn
			elseif command_num==CMD_SU_LOGIN then
				call connect_server
				call do_su_login
			elseif command_num==CMD_SSH_LOGIN then
				call connect_server
				call do_ssh_login
			elseif command_num==CMD_UPLOAD then
				;;call disconnect_server
				call do_upload
			elseif command_num==CMD_DOWNLOAD then
				;;call connect_server
				call do_download
			elseif command_num==CMD_SLEEP then
				;;call connect_server
				str2int value parameter1
				pause value
			elseif command_num==CMD_NULL then
			endif
:after_do_command
			;;messagebox  ip    '-----after_do_command:ip----'
			ip=ip+1
		;;next
		loop
	endif


return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;vm_login
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:vm_login
;messagebox  sta_dest    '-----vm_login----'
;messagebox  ladder_username_tbl[sta_dest]    '-----vm_login----'
tmp=ladder_username_tbl[0]
sendln tmp
Wait 'Password:'
tmp=ladder_password_tbl[0]
sendln tmp
Wait server_prompt

sendln

return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;start console
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:console
if sta_vm==STA_VM_NULL && sta_num == sta_dest then
	ladder_index=ladder_memory_tbl[sta_num]
	vm_master=ladder_hostname_tbl[ladder_index]
	sendln 'virsh list > vm_list.txt'
	Wait vm_master

	sendln 'aa="NOT";bb="EXIST"'
	Wait vm_master


	sprintf  'if [[ "$(cat vm_list.txt | grep %s)" = "" ]];then echo $aa;else  echo $bb;fi' vm_guestname
	msg=inputstr

	;プロセス実行されているかを確認

	sendln msg
	Wait  'EXIST' 'NOT'
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln
	else

:start_ut
		;;start ike-ut
		msg = 'virsh start '
		strconcat msg vm_guestname
		sendln msg
		Wait vm_master
		Wait start_vm '起動に失敗しました'
		if result=0 then
			call timeout_
		elseif result=1  then
			sendln
		else
			;;goto 
			goto start_ut

		endif
	endif

	msg = 'virsh console '
	strconcat msg vm_guestname
	sendln msg
	wait login_kvm
	Wait '^]'
	sendln

	sprintf  '(%s)' vm_guestname
	pattern_server=inputstr
	
	pattern_prompt_name='\[root@(\S+) \S+\]#'
	pattern_login_name='(\S+) login:'

	waitregex pattern_server pattern_prompt_name pattern_login_name
	if result=0 then
		call timeout_
	else
		ut_name_now=groupmatchstr1
	endif

	login_id=ladder_username_tbl[0]
	sprintf  '[%s@%s' login_id  ut_name_now
	server_prompt=inputstr
	;server_prompt=ut_name_now
	;;messagebox  server_prompt 'console'
	sendln

	timeout = 40
	Wait 'login:'  server_prompt

	if result=0 then
		call timeout_
	elseif result=1  then
		call vm_login
	else
		sendln
	endif
	sta_vm=STA_VM_GUEST
endif
return

}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;connect_server
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:connect_server
	if sta_dest==0 then
		sta_dest=ladder_count
	endif
	if vm_flag==1  then
		if sta_vm!=STA_VM_GUEST then
			call connect_dest
			if sta_num == sta_dest then
				call console
				strlen last_path
				if result >0 then
					sprintf 'cd %s'  last_path
					sendln inputstr
					Wait server_prompt
				endif
			endif
		endif
	else
		if sta_vm!=STA_VM_NULL then
			call disconnect_vm
		endif
		call connect_dest
	endif
	connect_flag=1
	
return
:disconnect_server
	if sta_vm!=STA_VM_NULL then
		call disconnect_vm
	elseif sta_num == sta_dest then
		call disconnect_ladder
	endif
		
return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;disconnect_vm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:disconnect_vm
	if sta_vm!=STA_VM_NULL then
		if sta_vm==STA_VM_GUEST then
			send #29
		else
			sendln
		endif
		;;messagebox  sta_num  'disconnect_vm'
		ladder_index=ladder_memory_tbl[sta_num]
		sta_vm=STA_VM_NULL
		user_id=ladder_username_tbl[ladder_index]
		Wait user_id
		pause 1
		flushrecv
		
		call make_prompt
	endif
return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;do_su_login
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:do_su_login
	;;messagebox  parameter1    '-----do_su_login----'
	sprintf  'su - %s' parameter1
	sendln inputstr
	Wait 'パスワード:' 'Password:' 'password:' 'Last login:' '･ﾑ･ｹ･・`･ﾉ:'
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln  parameter2
	elseif result=2  then
		sendln  parameter2
	elseif result=3  then
		sendln  parameter2
	elseif result=4  then
		sendln  parameter2
	elseif result=5  then
		sendln  parameter2
	endif
	su_id_tbl[su_index]=user_id
	su_index=su_index+1
	user_id=parameter1
	;;su_flag=1
	call make_prompt
return
:do_ssh_login
	sprintf  'ssh  %s@%s' parameter1 parameter3
	sendln inputstr
	Wait 'パスワード:' 'password:' 'Last login:'
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln  parameter2
	elseif result=2  then
		sendln  parameter2
	endif
	su_id_tbl[su_index]=user_id
	su_index=su_index+1
	user_id=parameter1
	;;su_flag=1
	call make_prompt
return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;do_cmd
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:do_cmd
ladder_index=ladder_memory_tbl[sta_dest]
strlen ladder_username_tbl[ladder_index]
if result <= 0 then
	message="'login IDの設定を確認ください"
	messagebox  message 'login IDエラー'
	exit
endif
strlen ladder_password_tbl[ladder_index]
if result <= 0 then
	message="login  passwordの設定を確認ください"
	messagebox  message 'login  password エラー'
	exit
endif
timeout = 3000
sendln parameter1
if command_type=0 then
	if next_cmd != CMD_ANS then
		Wait server_prompt
	endif
else
	call init_cmd
	;;messagebox  sta_num 'do_cmd'
	Wait 'closed.'
	if su_index>0 then
		su_index=0
		user_id=su_id_tbl[su_index]
	else
		sta_num=sta_num-1
		ladder_index=ladder_memory_tbl[sta_num]
		user_id=ladder_username_tbl[ladder_index]
	endif
	call make_prompt
endif
return
:do_ans
timeout = 3000
Wait parameter1
pause 1
flushrecv
sendln parameter2
if command_type=0 then
	if next_cmd != CMD_ANS then
		Wait server_prompt
	endif
endif
return
:do_check
timeout = 3000

sendln parameter3
Wait parameter1 server_prompt
if result=0 then
	call timeout_
elseif result=1  then
	Wait server_prompt
else
	str2int value parameter2
	pause value
	flushrecv
	goto do_check
endif

return
:do_svn

timeout = 3000

call svn_checkout

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;do_upload
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:do_upload

	all_path_flag=0
	strscan parameter1 '\'
	if result>0 then
		all_path_flag=1
	endif

	strscan parameter1 '/'
	if result>0 then
		all_path_flag=1
	endif

	if all_path_flag==1 then
		strmatch parameter1 '.+[\\/]([^/\\]+)$'
		fileName=groupmatchstr1
	else
		fileName=parameter1
	endif

	uploaded_flag=0
	if sta_dest>1 then
		if uploaded_index >0 then
			for j 0 uploaded_index-1
				strcompare uploaded_filename_tbl[j] fileName
				if result=0 then
					uploaded_flag=1
					break
				endif
			next
		endif
	endif
	
	if uploaded_flag=0 then
		if sta_num < 1 then
			ladder_index=ladder_memory_tbl[1]
			call connect_first
		elseif sta_num > 1  then
			while sta_num > 1
				call disconnect_ladder
			endwhile
		endif

		if sta_dest >1 then
			uploaded_filename_tbl[uploaded_index]=fileName
			uploaded_index=uploaded_index+1
			send_to='/tmp/'
			strconcat send_to fileName
		else
			send_to=parameter2
		endif

		;;-----------------------------------------------
		if all_path_flag==1 then
			send_from=parameter1
		else
			send_from=dir
			strconcat send_from '\'
			strconcat send_from fileName
		endif
		
		scpsend send_from send_to

		;; ファイル送信プロセス確認
		do
			mpause 2000
			sprintf2 str 'ps -ef |grep -v grep |grep -c scp'
			sendln str
			waitregex '^\s*(\d+)\s*$'
			str2int value groupmatchstr1
		loop while value != 0
		;;-----------------------------------------------
	endif
	
	if sta_dest>1 then
		if sta_num == sta_dest then
			call disconnect_ladder
		endif
		until sta_num == sta_dest
			next_num=sta_num+1
			ladder_index=ladder_memory_tbl[next_num]
			if next_num == sta_dest then
				sprintf  'scp /tmp/%s   %s@%s:%s'  fileName  ladder_username_tbl[ladder_index]   ladder_hostaddr_tbl[ladder_index] parameter2
			else
				sprintf  'scp /tmp/%s   %s@%s:/tmp/%s'  fileName  ladder_username_tbl[ladder_index]   ladder_hostaddr_tbl[ladder_index] fileName
			endif
			sendln inputstr
			Wait "(yes/no)?" "password"
			if result=0 then
				call timeout_
			elseif result=1  then
				sendln 'yes'
				Wait "password"
			else

			endif
			tmp=ladder_password_tbl[ladder_index]
			sendln tmp
			wait server_prompt
			call connect_ladder
		enduntil
	endif

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;do_download
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:do_download
	strmatch parameter1 '.*[\\/]([^/\\]+)$'
	if result >0 then
		fileName=groupmatchstr1
	else
		fileName=parameter1
	endif

	uploaded_flag=0
	if sta_dest>1 then
		;scprecv '/tmp/messages-20130604'
		if downloaded_index >0 then
			for j 0 downloaded_index-1
				;messagebox  downloaded_filename_tbl[j]  '-----line1----'
				strcompare downloaded_filename_tbl[j] fileName
				if result==0 then
					sprintf "違うサーバに同じdownloadファイル名が設定されてないかを確認ください\n(server=%s,download file=%s)" server fileName
					message=inputstr
					strspecial message
					messagebox  message 'same file name'
					exit
				endif
			next
		endif
		downloaded_filename_tbl[downloaded_index]=fileName
		downloaded_index=downloaded_index+1
	endif

	call connect_dest

	;;messagebox  sta_num  '-----do_download----'
	until sta_num ==1
		next_num=sta_num-1
		ladder_index=ladder_memory_tbl[next_num]
		if sta_num == sta_dest then
			sprintf  'scp %s   %s@%s:/tmp/%s'  parameter1  ladder_username_tbl[ladder_index]   ladder_hostaddr_inner_tbl[ladder_index]  fileName
		else
			sprintf  'scp /tmp/%s   %s@%s:/tmp/%s'  fileName  ladder_username_tbl[ladder_index]   ladder_hostaddr_tbl[ladder_index] fileName
		endif
		sendln inputstr
		Wait "(yes/no)?" "password"
		if result=0 then
			call timeout_
		elseif result=1  then
			sendln 'yes'
			Wait "password"
		else

		endif
		tmp=ladder_password_tbl[ladder_index]
		sendln tmp
		wait server_prompt

		call disconnect_ladder
	enduntil

	if 1 == sta_dest then
		file_from=parameter1
	else
		sprintf  '/tmp/%s'  fileName
		file_from=inputstr
	endif
	
	strlen parameter2
	if result == 0 then
		scprecv file_from
	else
		scprecv file_from parameter2
	endif
	;; ファイル送信プロセス確認
	do
		mpause 2000
		sprintf2 str 'ps -ef |grep -v grep |grep -c scp'
		sendln str
		waitregex '^\s*(\d+)\s*$'
		str2int value groupmatchstr1
	loop while value != 0

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;svn_checkout
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:svn_checkout
	pause 1
	flushrecv
	
	
	sprintf2 checkout_url  'svn co %s  %s'  parameter1  parameter2
	sendln checkout_url

	Wait "(R)eject, accept (t)emporarily or accept (p)ermanently?" "Authentication realm:" server_prompt
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln "p"
		Wait "Authentication realm:"
		Wait "Password for"
		Wait "':"
		sendln ladder_password_tbl[ladder_index]

		Wait "Username:"
		sendln parameter3
		Wait "Password for"
		Wait "':"
		sendln parameter4
		
		Wait  "Store password unencrypted (yes/no)?"
		sendln "yes"
		
	elseif result=2  then
		Wait "Password for"
		Wait "':"
		sendln ladder_password_tbl[ladder_index]

		Wait "Username:"
		sendln parameter3
		Wait "Password for"
		Wait "':"
		sendln parameter4
		
		Wait  "Store password unencrypted (yes/no)?"
		sendln "yes"
	else
		sendln
	endif

	wait "Please type 'yes' or 'no':" "Store password unencrypted (yes/no)?" server_prompt
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln "yes"
	elseif result=2  then
		sendln "yes"
	elseif result=3  then
		sendln
	endif
	wait server_prompt
	
	sendln
	wait server_prompt

return
}


; ファイルオープン
/*
##ex.1
#ut2-master{
#mesn-br:
#ethX,ip,netmask
#upload,local file name,[remote path]
#cmd,xxxxxxx,[type]
#download,remote path,[local file name]
#}

##ex.2
#mens-gw4:
#id-pswd,root,mens-exp
#eth1,10.10.30.82,255.255.255.0
#upload,test1,/root/test1
#cmd,rm -f /tmp/test1
#download,/root/test1,mens-gw4_test1
*/
;setting_flag:1,2
;1:utX-master:
;2:vm number,ip
{
:read_setting

	filereadln fhandle line
	if result then
		goto fclose
		/*
		if file_open_flag=1 then
			file_open_flag=file_open_flag+1
			goto fclose1
		else
			goto fclose
		endif
		*/
	endif
	; 一行読み込み

	strtrim line ' 	'
	strlen line
	if result =0 then
		goto read_setting
	endif

	strscan line '#'
	if result=1 goto read_setting
	strscan line '--'
	if result=1 goto read_setting
	strscan line '=='
	if result=1 goto read_setting
	strscan line '**'
	if result=1 goto read_setting


	strscan line '*/'
	if result ==1 then
		comment_flag=0
		goto read_setting
	endif

	if comment_flag=1 then
		goto read_setting
	endif

	strscan line '/*'
	if result ==1 then
		comment_flag=1
		goto read_setting
	endif


	strmatch line '\?([-\w]+)=(.+)$'
	if result >0 then
		command_num_tbl[cmd_count]=CMD_VAR
		;command
		parameter1_tbl[cmd_count]=groupmatchstr1
		parameter2_tbl[cmd_count]=groupmatchstr2
		cmd_count=cmd_count+1

		return
	endif
	;;messagebox  line '---------'
	strmatch line '^log,(.+)$'
	if result >0 then
		command_num_tbl[cmd_count]=CMD_LOG
		logfile = groupmatchstr1
		getdate timestamp "_%Y%m%d_%H%M%S.log"
		strconcat logfile timestamp
		parameter1_tbl[cmd_count]=logfile
		cmd_count=cmd_count+1
		/*
		log_flag=1
		;strconcat logfile msg2
		;;logopen logfile 0 0
		*/
		return
	endif

	;;messagebox  line '---------'

	strmatch line '::([0-9,]+)::'
	if result >0 then
		command_num_tbl[cmd_count]=CMD_LADDER
		parameter1_tbl[cmd_count]=groupmatchstr1
		cmd_count=cmd_count+1
		/*
		ladder_info=groupmatchstr1
		login_count=1
		*/
		return
	endif

	strmatch line '::::'
	if result>0 then
		command_num_tbl[cmd_count]=CMD_LADDER_END
		cmd_count=cmd_count+1
		;ut2,vn2-master
		setting_flag=0
		return
	endif

	;;messagebox  line '-----line1----'
	call read_line
	strmatch field[1] '^([\w\?\<\>:-]+)\s*\{'
	if result >0 then
		tolower fieldName  groupmatchstr1
		strcompare  fieldName  'kvm'
		if result==0 then
			command_num_tbl[cmd_count]=CMD_VMMASTER
			cmd_count=cmd_count+1
			return
		else
			strmatch fieldName '^([^:]+):([^:]+)$'
			tolower fieldName  groupmatchstr1
			fieldVar=groupmatchstr2
			strcompare  fieldName  'fuc'
			if result==0 then
				if cmd_count>0 then
					for i 0 cmd_count-1
						if CMD_CALL==command_num_tbl[i] then
							strcompare parameter1_tbl[i] fieldVar
							if result=0 then
								command_type_tbl[i]=cmd_count
								;;messagebox  i '-----func----'
							endif
						endif
					next
				endif
				;;messagebox  cmd_count  '-----func:pos----'
				command_num_tbl[cmd_count]=CMD_FUNCTION
				parameter1_tbl[cmd_count]=fieldVar
				cmd_count=cmd_count+1
				return
			endif
		endif
		return
	endif
	
	strmatch field[1] '^}$'
	if result >0 then
		command_num_tbl[cmd_count]=CMD_SECTION_END
		cmd_count=cmd_count+1
		return
	endif
	;;messagebox  field[1] 'setting_flag=2'
	strmatch field[1] '^(.+)(:|：)\s*$'
	if result >0 then
		command_num_tbl[cmd_count]=CMD_GUEST
		parameter1_tbl[cmd_count]=groupmatchstr1
		cmd_count=cmd_count+1
		return
	endif

	strcompare field[1] 'id-pswd'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_ID_PSWD
		;command
		parameter1_tbl[cmd_count]=field[2]
		parameter2_tbl[cmd_count]=field[3]
		cmd_count=cmd_count+1
		return
	endif
	
	if sta_dest==0 then
		sta_dest=ladder_count
	endif

	strcompare field[1] 'upload'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_UPLOAD
		parameter1_tbl[cmd_count]=field[2]
		strlen field[3]
		if result =0 then
			sprintf '/tmp/%s' field[2]
			parameter2_tbl[cmd_count]=inputstr
		else
			parameter2_tbl[cmd_count]=field[3]
		endif
		cmd_count=cmd_count+1
		;;messagebox  cmd_count 'upload'
		return
	endif
	
	strcompare field[1] 'call'
	if result=0 then
		if cmd_count>0 then
			for i 0 cmd_count-1
				if CMD_FUNCTION==command_num_tbl[i] then
					strcompare parameter1_tbl[i] field[2]
					if result=0 then
						command_type_tbl[cmd_count]=i
						break
					endif
				endif
			next
		endif
		command_num_tbl[cmd_count]=CMD_CALL
		parameter1_tbl[cmd_count]=field[2]

		cmd_count=cmd_count+1
		return
	endif
	
	strcompare field[1] 'cmd'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_CMD
		;command
		setting_flag=7
		strmatch line '^cmd\s*,(\s*.+\s*),(\s*[01])$'
		if result >0 then
			strtrim groupmatchstr1 ' 	'
			parameter1_tbl[cmd_count]=groupmatchstr1
			strtrim groupmatchstr2 ' 	'
			str2int value groupmatchstr2
			command_type_tbl[cmd_count]=value
		else
			strsplit  line ',' 2
			strtrim groupmatchstr2 ' 	'
			parameter1_tbl[cmd_count]=groupmatchstr2
			command_type_tbl[cmd_count]=0
		
		endif
		cmd_count=cmd_count+1
		return
	endif

	strcompare field[1] 'download'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_DOWNLOAD

		parameter1_tbl[cmd_count]=field[2]
		strlen field[3]
		if result ==0 then
			parameter2_tbl[cmd_count]=""
		else
			parameter2_tbl[cmd_count]=field[3]
		endif
		cmd_count=cmd_count+1
		return
	endif

	strcompare field[1] 'sleep'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_SLEEP
		parameter1_tbl[cmd_count]=field[2]
		cmd_count=cmd_count+1
		return
	endif

	strcompare field[1] 'ssh'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_SSH_LOGIN
		;command
		setting_flag=10
		parameter1_tbl[cmd_count]=field[2]
		parameter2_tbl[cmd_count]=field[3]
		parameter3_tbl[cmd_count]=field[4]
		cmd_count=cmd_count+1
		return
	endif
	strcompare field[1] 'ans'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_ANS
		parameter1_tbl[cmd_count]=field[2]
		parameter2_tbl[cmd_count]=field[3]
		parameter3_tbl[cmd_count]=field[4]
		cmd_count=cmd_count+1
		return
	endif
	strcompare field[1] 'check'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_CHECK
		parameter1_tbl[cmd_count]=field[2]
		parameter2_tbl[cmd_count]=field[3]
		parameter3_tbl[cmd_count]=field[4]
		cmd_count=cmd_count+1
		return
	endif
	
	strcompare field[1] 'svn'
	if result=0 then
		command_num_tbl[cmd_count]=CMD_SVN
		parameter1_tbl[cmd_count]=field[2]
		parameter2_tbl[cmd_count]=field[3]
		parameter3_tbl[cmd_count]=field[4]
		parameter4_tbl[cmd_count]=field[5]
		cmd_count=cmd_count+1
		return
	endif


	parameter1_tbl[cmd_count]=line
	command_num_tbl[cmd_count]=CMD_CMD
	cmd_count=cmd_count+1
return

:init_cmd
	if cmd_count>0 then
		for j 0 cmd_count-1
			command_num_tbl[j]=0
			parameter1_tbl[j]=""
			parameter2_tbl[j]=""
			parameter3_tbl[j]=""
			command_type_tbl[j]=0
		next
	endif
	cmd_count=0
return
:read_line
	;;call get_variable
	; 読み込んだ行を表示
	strsplit  line ',' 8
	strtrim groupmatchstr1 ' 	'
	field[1]=groupmatchstr1
	strtrim groupmatchstr2 ' 	'
	field[2]=groupmatchstr2
	strtrim groupmatchstr3 ' 	'
	field[3]=groupmatchstr3
	strtrim groupmatchstr4 ' 	'
	field[4]=groupmatchstr4
	strtrim groupmatchstr5 ' 	'
	field[5]=groupmatchstr5
	strtrim groupmatchstr6 ' 	'
	field[6]=groupmatchstr6
	strtrim groupmatchstr7 ' 	'
	field[7]=groupmatchstr7
	strtrim groupmatchstr8 ' 	'
	field[8]=groupmatchstr8
return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
set_variable
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:set_variable
	search_flag=0
	if variable_count>0 then
		for i 0 variable_count-1
			strcompare variable_name_tbl[i] variable_name
			if result=0 then
				search_flag=1
				var_index=i
				break
			endif
		next
	endif
	if search_flag==0 then
		variable_name_tbl[variable_count]=variable_name
		variable_string_tbl[variable_count]=variable_str
		variable_count=variable_count+1
	else
		variable_string_tbl[var_index]=variable_str
	endif
return
:get_variable
	if variable_count>0 then
		for i 0 variable_count-1
			sprintf  '\<\?%s\?\>'  variable_name_tbl[i]
			var_name_=inputstr
			strreplace for_replace 1 var_name_ variable_string_tbl[i]
			strreplace for_replace 1 var_name_ variable_string_tbl[i]
			strreplace for_replace 1 var_name_ variable_string_tbl[i]
		next
	endif
return
}
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;ladder_memory_clear
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:ladder_host_setting
	ladder_username_tbl[0]=""
	ladder_password_tbl[0]=""
	ladder_hostname_tbl[0]=server_pre
	ladder_hostaddr_tbl[0]=server_pre
	ladder_hostaddr_inner_tbl[0]=""
	ladder_memory_tbl[sta_pre]=0
	login_count=0
return
:ladder_memory_read
	call read_line
	for i 1 ladder_count_max-1
		strlen field[i]
		if result==0 then
			break
		endif
		;;messagebox  i 'ladder_memory_read'
		;;messagebox  field[i] 'ladder_memory_read'
		str2int num field[i]
		ladder_memory_tbl[i]=num
		ladder_count=i
	next
return
:ladder_memory_clear
	for i 0 ladder_count_max-1
		ladder_memory_tbl[i]=-1
	next
	ladder_count=0
return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;VM接続共通設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;-------------------------------
{
:make_prompt
	;;messagebox  ladder_index  'make_prompt'

	timeout = 3
	;;messagebox  user_id  'make_prompt'
	;;sendln 'export LANG=en_US.UTF-8'
	;;sendln 'export LANG=ja_JP.UTF-8'
	sendln exp_lang
	sprintf  '\[%s@(\S+) .+\]' user_id
	matching1=inputstr
	sprintf  '%s@([^:]+):.+$' user_id
	matching2=inputstr
	
	waitregex matching1
	if result==0 then
		sendln
		waitregex matching2
		if result==0 then
			call timeout_
		elseif result==1 then
			server_name=groupmatchstr1
			sprintf  '%s@%s'  user_id server_name
			server_prompt=inputstr
			sprintf  '%s@%s' user_id server_name
			server_reg_prompt=inputstr
		endif
	elseif result==1 then
		server_name=groupmatchstr1
		sprintf  '[%s@%s'  user_id server_name
		server_prompt=inputstr
		sprintf  '\[%s@%s' user_id server_name
		server_reg_prompt=inputstr
	endif
	;messagebox  server_prompt  'server_prompt'
	sendln
	wait server_prompt
	sendln
	wait server_prompt
	;;setsync 1
	timeout = 30000
	pause 1
	flushrecv
	;;su_flag=0
return

:connect_dest
	if sta_num < 1 then
		ladder_index=ladder_memory_tbl[1]
		call connect_first
	endif
	
	;messagebox  sta_dest    '-----connect_dest----'
	until sta_num == sta_dest
		;messagebox  sta_num    '-----connect_dest1----'
		ladder_index=ladder_memory_tbl[sta_num+1]
		call connect_ladder
		;messagebox  sta_num    '-----connect_dest2----'
	enduntil
return

:connect_first
	;messagebox  ladder_index 'connect_first'
	msg = ladder_hostaddr_tbl[ladder_index]
	strconcat msg ':22 /ssh /auth=password  /user='
	strconcat msg ladder_username_tbl[ladder_index]
	strconcat msg ' /passwd='
	strconcat msg ladder_password_tbl[ladder_index]

	;messagebox  msg 'connect_first'
	connect msg
	user_id=ladder_username_tbl[ladder_index]
	;;messagebox  tmp 'connect_first'
	;;Wait ladder_username_tbl[ladder_index]
	Wait user_id

	call make_prompt
	sta_num=1

	getdir dir
	changedir dir
	
	if log_flag==1 then
		logopen logfile 0 0
		log_flag=0
	endif
	
return

;; ladder_indexを保証する
:connect_ladder
	;;messagebox  ladder_index 'connect_ladder'
	;;messagebox  ladder_username_tbl[ladder_index] 'connect_ladder'
	if log_flag==1 then
		logopen logfile 0 0
		log_flag=0
	endif
	user_id=ladder_username_tbl[ladder_index]
	sprintf 'ssh %s@' user_id
	msg=inputstr
	strconcat msg ladder_hostaddr_tbl[ladder_index]
	sendln msg

:_connet_wait
	Wait 'password:' '(yes/no)?' 'Last login:' 
	if result=0 then
		call timeout_
	elseif result=1  then
		tmp=ladder_password_tbl[ladder_index]
		sendln  tmp
		Wait user_id
	elseif result=2  then
		sendln 'yes'
		goto _connet_wait
	endif

	call make_prompt
	sta_num=sta_num+1
	su_index=0
	;;messagebox  sta_num 'connect_ladder'
return

;;  sta_numを保証する
:disconnect_ladder
	;;messagebox  sta_num 'disconnect_ladder sta_num'
	;;messagebox  setting_flag 'disconnect_ladder setting_flag'
	if su_index>0 then
		until su_index == 0
			sendln 'exit'
			Wait 'logout'
			su_index=su_index-1
		enduntil
		user_id=su_id_tbl[0]
		;;su_flag=1
		call make_prompt
	endif
	
	if sta_num > 1 then
		sendln 'exit'
		ladder_index=ladder_memory_tbl[sta_num]
		tmp=ladder_hostaddr_tbl[ladder_index]
		Wait tmp
		sta_num=sta_num-1
		if sta_num >0 then
			Wait 'closed.'
			ladder_index=ladder_memory_tbl[sta_num]
			user_id=ladder_username_tbl[ladder_index]
			call make_prompt
		endif
	elseif connect_flag < 1 then
		unlink
		sta_num=0
	endif
return


}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;time out 処理
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{

exit
:timeout_
	sendln
return
:timeout_exit_
	sendln
exit

:debug
	sprintf  ':sta_dest(%d):do_flag(%d):login_count(%d)\n%s'    sta_dest do_flag  login_count line
	message=inputstr
	strspecial message
	messagebox  message  'debug'
return

}

;send #29



