;ｖｍマシンを作成
;作成者:金忠傑
;key
include 'vm-common.ttl'
created=0
agw_name="agw19"  ;default

ut_Prefix="ut-st"
vn_Prefix="vn-st"
ut2_Prefix="ut2-st"
vn2_Prefix="vn2-st"


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 日時から保存ファイル名を組み立てる
/*
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
*/

/*

*/


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;=========================================================================
timeout = 3000
default_agw_flag=0



; ファイルオープン
fileopen fhandle 'setting.txt' 0
while 1
	call read_setting1
	
	sendln
	Wait '$'
	sprintf  'echo "%s 設定-----------------------------"'  ut_name
	sendln inputstr
	Wait '$'
	sendln
	Wait '$'

	if setting_flag=1 then
		call connect_agw
		call read_agw_config
		default_agw_flag=1
	elseif setting_flag>1 then
		if default_agw_flag=0 then
			call connect_agw
			call read_agw_config
			default_agw_flag=1
			call exit_vm
		endif

		call connect_vm
		call console
		
		if setting_flag=2 then
			;UT
			if accept_type=1 then
				;ipsec
				call ipsec_setting
			elseif accept_type=2 then
				;vlan
				call vlan_setting
			else
				call ike_setting
				;ike
			
			endif
		else
			;VN
			call vn_setting
		endif
		send #29
		Wait vm_master
		Wait '#'

	else
	endif
    
	call exit_vm

endwhile

:fclose1
; ファイルクローズ
fileclose fhandle

sendln
Wait '$'
sendln 'echo "**********arp を設定する*************************************"'
Wait '$'
sendln
Wait '$'
; ファイルオープン
fileopen fhandle 'setting.txt' 0
while 1
	call read_setting2
	sendln
	Wait '$'
	sprintf  'echo "%s  arp設定-----------------------------"'  ut_name
	sendln inputstr
	Wait '$'
	sendln
	Wait '$'
	if setting_flag=1 then
		default_agw_flag=1
	elseif setting_flag>1 then
		if default_agw_flag=0 then
			default_agw_flag=1
		endif
		
		if setting_flag=2 then
			;UT
			if accept_type=1 then
				;ipsec
				call arp_setting
			elseif accept_type=2 then
				;vlan
			else
				call arp_setting
				;ike
			
			endif
		else
			;VN
			call arp_setting
		
		endif

	else
	endif
    
endwhile

:fclose2
; ファイルクローズ
fileclose fhandle

;fileclose flog_handle
sendln

;sendln 'exit'
;Wait '$'
exit



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;start console
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:console

sendln
Wait vm_master
Wait '#'
if created=0 then
	sprintf  'if [[ "$(cat vm_list.txt | grep %s)" = "" ]];then echo $aa;else  echo $bb;fi' ut_name
	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 ut_name
		sendln msg
		Wait vm_master
		Wait '起動されました' '起動に失敗しました'
		if result=0 then
			call timeout_
		elseif result=1  then
			sendln
		else
			;;goto 
			goto start_ut

		endif
	endif
endif

;messagebox  vm_Prefix '-----line1----'

msg = 'virsh console '
strconcat msg ut_name
sendln msg
Wait '^]'
Wait 'です'
sendln


strlen vm_Prefix
read_l=result+3

;messagebox  vm_Prefix '-----line1----'

waitrecv vm_Prefix read_l 1
sprintf  '%s ~]#' inputstr
vm_prompt=inputstr
		
sendln

timeout = 20
Wait 'login:'  vm_Prefix

if result=0 then
	call timeout_
elseif result=1  then

	call login
else
	sendln 'cd'
endif

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;login
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:login

sendln 'root'
Wait 'Password:'
sendln vm_root_password

Wait vm_Prefix
Wait '#'

sendln

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;read agw config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:read_agw_config
;interface
	sendln 'cat  /telsrv/cur/config/data/AGWAPL_INTERFACE.csv'
	waitregex '^(D-Plane-user|D-Plane|Dpu-Plane|Dpr-Plane),lan\d+,[^,]+,(\d+\.\d+\.\d+\.\d+),(\d+),[^,]+,[^,]*,.*$'
	strcompare 'D-Plane-user' groupmatchstr1
	if result=0 then
		agw_ut_ip=groupmatchstr2
		agw_ut_mask=groupmatchstr3
	endif
	strcompare 'D-Plane' groupmatchstr1
	if result=0 then
		agw_vn_ip=groupmatchstr2
		agw_vn_mask=groupmatchstr3
	endif

	waitregex '^(D-Plane-user|D-Plane|Dpu-Plane|Dpr-Plane),lan\d+,[^,]+,(\d+\.\d+\.\d+\.\d+),(\d+),[^,]+,[^,]*,.*$'
	strcompare 'D-Plane-user' groupmatchstr1
	if result=0 then
		agw_ut_ip=groupmatchstr2
		agw_ut_mask=groupmatchstr3
	endif
	strcompare 'D-Plane' groupmatchstr1
	if result=0 then
		agw_vn_ip=groupmatchstr2
		agw_vn_mask=groupmatchstr3
	endif
	Wait vm_master
	Wait '#'
;gre_ip
	sendln 'cat  /telsrv/cur/config/data/AGWAPL_ENV.cnf'
	waitregex '^GRE_PEER_IP=(.+\d+)\D*$'
	ut_gre_ip=groupmatchstr1
	Wait vm_master
	Wait '#'
	sendln 'cat  /telsrv/cur/config/data/AGWAPL_ENV.cnf'
	waitregex '^GRE_SRC_IP=(.+\d+)\D*$'
	agw_gre_ip=groupmatchstr1
	Wait vm_master
	Wait '#'
	
timeout = 2
;/usr/local/flow-engine/etc/racoon2/default.conf
	sendln 'cat /usr/local/flow-engine/etc/racoon2/default.conf | grep -E "(kmp_sa_lifetime_time|ipsec_sa_lifetime_time)" | grep -v \#'
	waitregex 'kmp_sa_lifetime_time (\d+) sec;' 'kmp_sa_lifetime_time\s+infinite\s*;'
	if result = 1 then
		ike_lifetime=groupmatchstr1
	elseif result = 2 then
		ike_lifetime="28800"
	else
		ike_lifetime=""
	endif

	waitregex 'ipsec_sa_lifetime_time (\d+) sec;'  'ipsec_sa_lifetime_time\s+infinite\s*;'
	if result=1 then
		ipsec_lifetime1=groupmatchstr1
	elseif result = 2 then
		ipsec_lifetime1="28800"
	else
		ipsec_lifetime1=""
	endif

	waitregex 'ipsec_sa_lifetime_time (\d+) sec;'  'ipsec_sa_lifetime_time\s+infinite\s*;'
	if result=1 then
		ipsec_lifetime2=groupmatchstr1
	elseif result = 2 then
		ipsec_lifetime2="28800"
	else
		ipsec_lifetime2=""
	endif

	waitregex 'ipsec_sa_lifetime_time (\d+) sec;'  'ipsec_sa_lifetime_time\s+infinite\s*;'
	if result=1 then
		ipsec_lifetime3=groupmatchstr1
	elseif result = 2 then
		ipsec_lifetime3="28800"
	else
		ipsec_lifetime3=""
	endif

	strlen ipsec_lifetime3
	if result > 0 then
		ipsec_lifetime=ipsec_lifetime1
	else
		ipsec_lifetime=ipsec_lifetime2
	endif
	
	

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;ipsec ut設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:ipsec_setting

	sendln "ifconfig eth2"
	waitregex 'inet addr:(\d+\.\d+\.\d+\.\d+)  Bcast:'
	eth2_ip=groupmatchstr1

	;-------------------------------------------
	print_flag=0
	call print_setting

	sprintf  "ifconfig eth2:1 %s/%s -arp"  ut_gre_ip  agw_ut_mask
	print_str=inputstr
	call print_setting

	print_str="ip link del gretap0 >& /dev/null" 
	call print_setting

	sprintf  "ip link add gretap0 type gretap remote %s local %s"  agw_gre_ip  ut_gre_ip
	print_str=inputstr
	call print_setting

	sprintf  "ifconfig gretap0 %s mtu 8119 up"  gre_ip_mask
	print_str=inputstr
	call print_setting


	print_str="" 
	call print_setting

	print_str="setkey -c <<EOT" 
	call print_setting
	print_str="flush;" 
	call print_setting

	print_str="spdflush;" 
	call print_setting

	sprintf  'add %s %s esp %s -m tunnel -E rijndael-cbc "abcdefghijklmnopqrstuvwx" -A hmac-sha1 "ABCDEFGHIJKLMNOPQRST";' eth2_ip agw_gre_ip spi_in
	print_str=inputstr
	call print_setting
	sprintf  'add %s %s esp %s -m tunnel -E rijndael-cbc "abcdefghijklmnopqrstuvwx" -A hmac-sha1 "ABCDEFGHIJKLMNOPQRST";'  agw_gre_ip eth2_ip spi_out
	print_str=inputstr
	call print_setting
	sprintf  'spdadd %s %s any -P out ipsec esp/tunnel/%s-%s/require;' ut_gre_ip agw_gre_ip eth2_ip agw_gre_ip
	print_str=inputstr
	call print_setting
	sprintf  'spdadd %s %s any -P  in ipsec esp/tunnel/%s-%s/require;'  agw_gre_ip ut_gre_ip agw_gre_ip eth2_ip 
	print_str=inputstr
	call print_setting

	print_str="EOT" 
	call print_setting
	
	print_flag=2
	call print_setting

return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;ike utを設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:ike_setting
	sendln "ifconfig eth2"
	waitregex 'inet addr:(\d+\.\d+\.\d+\.\d+)  Bcast:'
	eth2_ip=groupmatchstr1

	;-------------------------------------------
	print_flag=0
	call print_setting

	sprintf  "ifconfig eth2:1 %s/%s -arp"  ut_gre_ip  agw_ut_mask
	print_str=inputstr
	call print_setting

	print_str="ip link del gretap0 >& /dev/null" 
	call print_setting

	sprintf  "ip link add gretap0 type gretap remote %s local %s"  agw_gre_ip  ut_gre_ip
	print_str=inputstr
	call print_setting

	sprintf  "ifconfig gretap0 %s  mtu 8119 up"  gre_ip_mask
	print_str=inputstr
	call print_setting

	print_flag=2
	call print_setting
;---------------------------------------------------------------------------------
	print_flag=0
	call print_gretap_conf
	print_str='# $Id: local-test.conf,v 1.4 2007/12/27 01:08:52 mk Exp $'
	call print_gretap_conf
	print_str='# local test'
	call print_gretap_conf
	print_str='remote gretap_remote {'
	call print_gretap_conf
	print_str='        acceptable_kmp { ikev1;};'
	call print_gretap_conf
	print_str='        ikev1 {'
	call print_gretap_conf
	sprintf  '                peers_id ipaddr %s;   # D-Plane-user (AGW18)'  agw_ut_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '                peers_ipaddr %s;      # D-Plane-user (AGW18)'  agw_ut_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '                my_id ipaddr %s;      # D-Plane-user (UT)'  eth2_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '                kmp_sa_lifetime_time %s sec;' ike_lifetime
	print_str=inputstr
	call print_gretap_conf
	print_str='                kmp_enc_alg { aes192_cbc; };'
	call print_gretap_conf
	print_str='                kmp_hash_alg { sha1; };'
	call print_gretap_conf
	print_str='                kmp_dh_group { modp2048; };'
	call print_gretap_conf
	print_str='                kmp_auth_method { psk; };'
	call print_gretap_conf
	print_str='                pre_shared_key "${PSKDIR}/${PRESHRD_KEY}";'
	call print_gretap_conf
	print_str='                exchange_mode main;'
	call print_gretap_conf
	print_str='                need_pfs off;'
	call print_gretap_conf
	print_str='        };'
	call print_gretap_conf
	print_str='        ikev2 {'
	call print_gretap_conf
	sprintf  '                my_id ipaddr %s;      # D-Plane-user (UT)'  eth2_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '                peers_id ipaddr %s;   # D-Plane-user (AGW18)' agw_ut_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '                peers_ipaddr %s;      # D-Plane-user (AGW18)' agw_ut_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '                kmp_sa_lifetime_time %s sec;' ike_lifetime
	print_str=inputstr
	call print_gretap_conf
	print_str='                kmp_enc_alg { aes192_cbc; };'
	call print_gretap_conf
	print_str='                kmp_prf_alg { hmac_sha1; };'
	call print_gretap_conf
	print_str='                kmp_hash_alg { hmac_sha1; };'
	call print_gretap_conf
	print_str='                kmp_dh_group { modp2048; };'
	call print_gretap_conf
	print_str='                kmp_auth_method { psk; };'
	call print_gretap_conf
	print_str='                cookie_required on;'
	call print_gretap_conf
	print_str='                pre_shared_key "${PSKDIR}/${PRESHRD_KEY}";'
	call print_gretap_conf
	print_str='        };'
	call print_gretap_conf
	print_str='};'
	call print_gretap_conf
	print_str=''
	call print_gretap_conf
	print_str='selector gretap_out {'
	call print_gretap_conf
	print_str='        direction outbound;'
	call print_gretap_conf
	sprintf  '        src %s/32;  # GRE SRC (agw16)' ut_gre_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '        dst %s/32;    # GRE PEER (UT)'  agw_gre_ip
	print_str=inputstr
	call print_gretap_conf
	print_str='        upper_layer_protocol "any";'
	call print_gretap_conf
	print_str='        policy_index gretap_policy;'
	call print_gretap_conf
	print_str='};'
	call print_gretap_conf
	sprintf  ''
	print_str=inputstr
	call print_gretap_conf
	print_str='selector gretap_in {'
	call print_gretap_conf
	print_str='        direction inbound;'
	call print_gretap_conf
	sprintf  '        dst %s/32;  # GRE SRC (agw16)' ut_gre_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '        src %s/32;    # GRE PEER (UT)' agw_gre_ip
	print_str=inputstr
	call print_gretap_conf
	print_str='        upper_layer_protocol "any";'
	call print_gretap_conf
	print_str='        policy_index gretap_policy;'
	call print_gretap_conf
	print_str='};'
	call print_gretap_conf
	sprintf  ''
	print_str=inputstr
	call print_gretap_conf
	print_str='ipsec gretap_ipsec_esp {'
	call print_gretap_conf
	sprintf  '        ipsec_sa_lifetime_time %s sec;' ipsec_lifetime
	print_str=inputstr
	call print_gretap_conf
	print_str='        sa_index gretap_esp;'
	call print_gretap_conf
	print_str='};'
	call print_gretap_conf
	sprintf  ''
	print_str=inputstr
	call print_gretap_conf
	print_str='sa gretap_esp {'
	call print_gretap_conf
	print_str='        sa_protocol esp;'
	call print_gretap_conf
	print_str='        esp_enc_alg{aes192_cbc; aes128_cbc; 3des_cbc;};'
	call print_gretap_conf
	print_str='        esp_auth_alg{hmac_sha1;hmac_md5;};'
	call print_gretap_conf
	print_str='};'
	call print_gretap_conf
	sprintf  ''
	print_str=inputstr
	call print_gretap_conf
	print_str='policy gretap_policy {'
	call print_gretap_conf
	print_str='        action auto_ipsec;'
	call print_gretap_conf
	print_str='        remote_index gretap_remote;'
	call print_gretap_conf
	print_str='        ipsec_mode tunnel;'
	call print_gretap_conf
	print_str='        ipsec_index { gretap_ipsec_esp; };'
	call print_gretap_conf
	print_str='        ipsec_level require;'
	call print_gretap_conf
	sprintf  '        peers_sa_ipaddr %s;   # D-Plane-user (AGW)' agw_ut_ip
	print_str=inputstr
	call print_gretap_conf
	sprintf  '        my_sa_ipaddr %s;      # D-Plane-user (UT)' eth2_ip
	print_str=inputstr
	call print_gretap_conf
	print_str='};'
	call print_gretap_conf

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;vlan utを設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:vlan_setting
	;-------------------------------------------
	print_flag=0
	call print_setting

	sprintf  "vconfig rem eth2.%s"  vlan_id
	print_str=inputstr
	call print_setting
	sprintf  "vconfig add eth2 %s"  vlan_id
	print_str=inputstr
	call print_setting

	sprintf  "ifconfig eth2.%s %s up"  vlan_id vlan_ip_mask
	print_str=inputstr
	call print_setting
	
	print_flag=2
	call print_setting


return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;vn を設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:vn_setting

	sendln "ifconfig eth1"
	waitregex 'inet addr:(\d+\.\d+\.\d+\.\d+)  Bcast:'
	eth1_ip=groupmatchstr1

	;-------------------------------------------
	print_flag=0
	call print_setting

	print_str="ip link del gretap0 >& /dev/null" 
	call print_setting

	sprintf  "ip link add gretap0 type gretap remote %s local %s key %s"  agw_vn_ip  eth1_ip gre_key
	print_str=inputstr
	call print_setting

	sprintf  "ifconfig gretap0 %s  mtu 8119 up"  gre_ip_mask1
	print_str=inputstr
	call print_setting
	
	strlen gre_ip_mask2
	if result >0 then
		sprintf  "ifconfig gretap0:1 %s mtu 8119 up"  gre_ip_mask2
		print_str=inputstr
		call print_setting
	endif
	strlen gre_ip_mask3
	if result >0 then
		sprintf  "ifconfig gretap0:2 %s mtu 8119 up"  gre_ip_mask3
		print_str=inputstr
		call print_setting
	endif
	strlen gre_ip_mask4
	if result >0 then
		sprintf  "ifconfig gretap0:3%s mtu 8119 up"  gre_ip_mask4
		print_str=inputstr
		call print_setting
	endif
	
	print_flag=2
	call print_setting

return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;arp を設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:arp_setting
	vm_Prefix1=vm_Prefix
	ut_name1=ut_name

	strmatch gre_ip_mask '(\d+\.\d+\.\d+)\.\d+'
	my_ip_group=groupmatchstr1
	peer_name=peer_vm
	call arp_setting_peer

	my_ip_group=""
	peer_name=peer_vm1
	call arp_setting_peer
	peer_name=peer_vm2
	call arp_setting_peer


return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;arp sub
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:arp_setting_peer
	strlen peer_name
	if result>0 then
		strmatch peer_name '([vu].+st)\d+'
		vm_Prefix2=groupmatchstr1
		ut_name2=peer_name

		vm_Prefix=vm_Prefix2
		ut_name=ut_name2
		call connect_vm
		call console
		
		strlen my_ip_group
		if result >0 then
			sendln "ifconfig gretap0"
			waitrecv "HWaddr " 24 1
			toupper mac inputstr
			strremove mac 1 7
			Wait vm_prompt
			
			sprintf  'ifconfig | grep %s' my_ip_group
			sendln inputstr
			waitregex 'inet addr:(\d+\.\d+\.\d+\.\d+)  Bcast:'
			ip=groupmatchstr1

		else
			sendln "ifconfig gretap0"
			waitrecv "HWaddr " 24 1
			toupper mac inputstr
			strremove mac 1 7
			
			waitregex 'inet addr:(\d+\.\d+\.\d+\.\d+)  Bcast:'
			ip=groupmatchstr1
		endif
		Wait vm_prompt
		
		send #29
		Wait vm_master
		Wait '#'
		call exit_vm

		vm_Prefix=vm_Prefix1
		ut_name=ut_name1
		call connect_vm
		call console
		sprintf  'arp -s %s %s' ip mac
		sendln inputstr
		Wait vm_prompt
		
		send #29
		Wait vm_master
		Wait '#'
		call exit_vm
	endif
return
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;print /usr/local/racoon2/etc/racoon2/gretap.conf
;; print_gretap_conf
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:print_gretap_conf


if print_flag=0 then
	sendln  'echo "" > /usr/local/racoon2/etc/racoon2/gretap.conf' 
	;sendln
	Wait vm_prompt
	print_flag=print_flag+1

elseif print_flag=1 then
	sprintf  "echo '%s' >> /usr/local/racoon2/etc/racoon2/gretap.conf"  print_str
	sendln inputstr
	Wait vm_prompt
else

endif

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;print rc.local
;; print_flag
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:print_setting


if print_flag=0 then
	sendln  'echo "" > /etc/rc.local' 
	;sendln
	Wait vm_prompt
	print_flag=print_flag+1

elseif print_flag=1 then
	sprintf  "echo '%s' >> /etc/rc.local"  print_str
	sendln inputstr
	Wait vm_prompt
else
	sendln '/etc/rc.local'
	Wait vm_prompt
endif

return
}









; ファイルオープン
/*
agw16:
ut-st000(field1),ike(field2),vn-st000(field3),192.168.3.3(field4),192.168.3.4(field5)
ut-st000(field1),ipsec(field2),vn-st000(field3),192.168.3.3(field4),192.168.3.4(field5)
ut-st000(field1),vlan(field2),vn-st000(field3),eth2.30(field4),192.168.3.4(field5)
vn-st000(field1),12345(field2),ut-st000(field3),192.168.3.2(field4),192.168.4.1(field5),ut-stXXX(field6)
*/
;setting_flag:1,2,3
;1:agwXX
;2:ut setting
;3:vn setting
{
:read_setting1
:next_line1
	filereadln fhandle line
	if result goto fclose1
	; 一行読み込み
	strscan line '#'
	if result=1 goto read_setting1
	
	strtrim line ' 	'
	strlen line
	if result =0 then
		goto read_setting1
	endif
	;messagebox  line '-----line1----'
	call read_setting
return 
:read_setting2
:next_line2
	filereadln fhandle line
	if result goto fclose2
	; 一行読み込み
	strscan line '#'
	if result=1 goto read_setting2
	
	strtrim line ' 	'
	strlen line
	if result =0 then
		goto read_setting2
	endif
	;messagebox  line '-----line2----'
	call read_setting
return 
:read_setting


	call read_line
	
	strmatch field1 '(\w+\d+):'
	if result >0 then
		;pause 5
		setting_flag=1
		agw_name=groupmatchstr1
		ut_name=agw_name
		return
	endif
	;messagebox  field1 '---------'
	;pause 5

	call init_read_vmName_ip
	ut_name=field1
	strmatch field1 '(ut.*-st)(\d+)'
	if result >0 then
		setting_flag=2
		vm_Prefix=groupmatchstr1
		vm_number=groupmatchstr2
		
		tolower ut_type field2 
		strscan 'ike   ipsec vlan  ' ut_type
		if result >10 then
			;vlan
			accept_type=2

			strmatch field3 'eth2\.(\d+)'
			if result >0 then
				vlan_id=groupmatchstr1
				vlan_ip_mask=field4
			else
				messagebox 'vlan err' 'setting err'
				exit
			endif

		elseif result >5 then
			;IPSEC
			accept_type=1
			spi_in=field3
			spi_out=field4
			strlen spi_in
			if result =0 then
				spi_in='512'
			endif
			strlen spi_out
			if result =0 then
				spi_out='768'
			endif
			peer_vm=field5
			;tennel_mask=field4
			gre_ip_mask=field6
			gre_ip2_mask=field7
		elseif result >0 then
			;ike
			accept_type=3
			peer_vm=field3
			;tennel_mask=field4
			
			gre_ip_mask=field4
			gre_ip2_mask=field5
		else
			messagebox 'UT type err' 'setting err'
			exit
		endif
		return
	endif

	strmatch field1 '(vn.*-st)(\d+)'
	if result >0 then
		setting_flag=3
		vm_Prefix=groupmatchstr1
		vm_number=groupmatchstr2
		gre_key=field2

		NameOrIP=field3
		call read_vmName_ip
		NameOrIP=field4
		call read_vmName_ip
		NameOrIP=field5
		call read_vmName_ip
		NameOrIP=field6
		call read_vmName_ip
		NameOrIP=field7
		call read_vmName_ip
		
		return
	endif
	setting_flag=0
return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;for read_vn setting
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:init_read_vmName_ip
	vm_name_num=0
	ip_name_num=0
	spi_in=""
	spi_out=""
	peer_vm=""
	peer_vm1=""
	peer_vm2=""
	peer_vm3=""
	peer_vm4=""
	peer_vm5=""
	gre_ip_mask1=""
	gre_ip_mask2=""
	gre_ip_mask3=""
	gre_ip_mask4=""
	gre_ip_mask5=""
return

:read_vmName_ip
	strmatch NameOrIP '(\d+\.\d+\.\d+\.\d+)'
	if result >0 then
		call read_ip
	else
		call read_vm_name
	endif
return
:read_vm_name
	vm_name_num=vm_name_num+1
	if vm_name_num=1 then
		peer_vm1=NameOrIP
	elseif vm_name_num=2 then
		peer_vm2=NameOrIP
	elseif vm_name_num=3 then
		peer_vm3=NameOrIP
	elseif vm_name_num=4 then
		peer_vm4=NameOrIP
	elseif vm_name_num=5 then
		peer_vm5=NameOrIP
	else
	endif
return

:read_ip
	ip_name_num=ip_name_num+1
	if ip_name_num=1 then
		gre_ip_mask1=NameOrIP
	elseif ip_name_num=2 then
		gre_ip_mask2=NameOrIP
	elseif ip_name_num=3 then
		gre_ip_mask3=NameOrIP
	elseif ip_name_num=4 then
		gre_ip_mask4=NameOrIP
	elseif ip_name_num=5 then
		gre_ip_mask5=NameOrIP
	else
	endif
return
:read_line
	; 読み込んだ行を表示
	strsplit  line ',' 8
	strtrim groupmatchstr1 ' 	'
	field1=groupmatchstr1
	strtrim groupmatchstr2 ' 	'
	field2=groupmatchstr2
	strtrim groupmatchstr3 ' 	'
	field3=groupmatchstr3
	strtrim groupmatchstr4 ' 	'
	field4=groupmatchstr4
	strtrim groupmatchstr5 ' 	'
	field5=groupmatchstr5
	strtrim groupmatchstr6 ' 	'
	field6=groupmatchstr6
	strtrim groupmatchstr7 ' 	'
	field7=groupmatchstr7
	strtrim groupmatchstr8 ' 	'
	field8=groupmatchstr8
return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;VM接続共通設定
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;-------------------------------
{
:connect_agw
	vm_path='/home/agw'
	vm_master=agw_name
	call connect_vm_com

	return
:connect_vm

	strcompare vm_Prefix ut_Prefix
	if result=0 then
		vm_path=vm_ut_path
		vm_master=vm_ut_master
		call connect_vm_com
	endif
	strcompare vm_Prefix vn_Prefix
	if result=0 then
		vm_path=vm_vn_path
		vm_master=vm_vn_master
		call connect_vm_com
	endif
	strcompare vm_Prefix ut2_Prefix
	if result=0 then
		vm_path=vm_ut2_path
		vm_master=vm_ut2_master
		call connect_vm_com
	endif
	strcompare vm_Prefix vn2_Prefix
	if result=0 then
		vm_path=vm_vn2_path
		vm_master=vm_vn2_master
		call connect_vm_com
	endif
	sendln 'virsh list > vm_list.txt'
	Wait vm_master
	Wait '#'
	sendln 'aa="NOT";bb="EXIST"'
	Wait vm_master
	Wait '#'
	/*
	*/

return

:connect_vm_com

	msg = 'ssh root@'
	strconcat msg vm_master
	sendln msg
	Wait 'password:'
	sendln vm_root_password
	Wait '#'
	sendln 'export LANG=ja_JP.UTF-8'
	Wait '#'

	msg = "cd "
	strconcat msg vm_path
	sendln msg
	Wait vm_master
	Wait '#'
	sendln 'aa="NOT";bb="EXIST"'
	Wait vm_master
	Wait '#'

	return


:exit_vm

	sendln 'exit'
	Wait vm_master
	Wait 'closed.'
	Wait '$'
	return
}




