0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【TeraTermマクロ】踏み台Ciscoスイッチから別Ciscoスイッチにtelnetログインしてshowログを採取する方法(showコマンド結果ごとに各ログに記録・passを都度入力ver)

Posted at

【事前準備】
・teraterm.exeを起動したら1から順番に展開されデフォルトで設定を進行していき「追加タスクの選択」で「.ttlファイルをttpmacro.exeに関連付ける」にチェックを入れること
・teratermの送信遅延設定をどちらも0にしないこと
・teratermログの自動採取を解除すること
・ttlファイル・commandテキスト・hostlistテキストは同じフォルダ内に置くこと
・ログを生成するフォルダの場所は事前に把握しておくこと
・別テキストに以下2つを用意
「command.txt」
show running-config
show logging
show version
など、表示結果が欲しいコマンド
「hostlist.txt」
1.1.1.254
1.1.1.253
1.1.1.252
1.1.1.251
など、ログインしたいホストアドレス

■TTLファイル説明■
*ファイルの書き直しは拡張子をtxtに直して修正する
*踏み台機器にコンソールで接続しteratermの「コントロール」→「マクロ」で開始
*showコマンド結果ごとに各ログに記録
*enableとVTYパスワードを都度手入力・各ログに記録

■TXTファイル説明■
*コマンド&ホストリストは一覧の最終行で改行禁止(最終行の下に行を作らない)


マクロここから↓↓↓

;■■■■■■■■■■パスワードの注意点■■■■■■■■■■				
;都度パスワードを入力する				
;間違えたらやりなおし				
;3回間違えたら最初に戻る				
;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■				
				
sendln ''				
:start				
wait '>'				
sendln 'en'				
				
				
	wait 'Password:'			
	:passopen			
				
	inputbox 'Password:' 'enableパスワードを入力'			
	sendln inputstr			
				
	wait 'Password:' '% Bad secrets' '#'			
				
	;↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑			
	;「Password:」表示があればresult値が1、			
	;「% Bad secrets」表示があればresult値が2、			
	;「#」表示があればresult値が3			
				
	if result = 1 then			
	goto passopen			
	elseif result = 2 then			
	goto start			
	elseif result = 3 then			
	goto passclose			
	endif			
				
:passclose				
pause 2				
sendln ''				
wait '#'				
sendln 'terminal length 0'				
wait '#'				
pause 2				
sendln ''				
wait '#'				
				
sendln 'show running-config | inc hostname'				
wait 'hostname'				
recvln				
strcopy inputstr 1 8 Host_Name				
;*************************************************				
;※↑show runのhostnameから始まる行で「hostname」のあと1~8文字目をHost_Nameとして使用する				
;文字数が違うなら修正して使用する				
;*************************************************				
				
fileopen FHcom 'command.txt' 0				
while 1				
	filereadln FHcom comline			
		if result = 1 then		
		break		
		endif		
				
		COMMAND = comline		
				
		getdate DATE '_%Y-%m%d-%H%M%S.log'		
		filename = Host_Name		
		strconcat filename '_'		
		strconcat filename COMMAND		
		strconcat filename DATE		
				
		pause 2		
		sendln ''		
		wait '#'		
				
	logopen filename 0 0			
				
		sendln COMMAND		
		wait '#'		
		pause 2		
		sendln ''		
		wait '#'		
		pause 2		
				
	logclose			
endwhile				
				
fileclose FHcom				
sendln ''				
wait '#'				
				
;■■■■■■■■■■■■■■■■■■■■■■■■■				
;★★★★★★★★次のスイッチにtelnet★★★★★★★★				
;■■■■■■■■■■■■■■■■■■■■■■■■■				
				
:start2				
sendln ''				
wait '#'				
fileopen FHtelnet 'hostlist.txt' 0				
:telnetloop				
filereadln FHtelnet telnetline				
if result goto telnetclose				
				
TELNET = telnetline				
				
pause 2				
sendln 'telnet ' TELNET				
				
	wait 'Password:'			
	:passopen2			
				
	inputbox 'Password:' 'VTYパスワードを入力'			
	sendln inputstr			
				
	wait 'Password:' '% Bad passwords' '>'			
				
	;↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑			
	;「Password:」表示があればresult値が1、			
	;「% Bad passwords」表示があればresult値が2、			
	;「>」表示があればresult値が3			
				
	if result = 1 then			
	goto passopen2			
	elseif result = 2 then			
	goto start2			
	elseif result = 3 then			
	goto passclose2			
	endif			
				
:passclose2				
:start3				
pause 2				
sendln ''				
wait '>'				
sendln 'en'				
				
	wait 'Password:'			
	:passopen3			
				
	inputbox 'Password:' 'enableパスワードを入力'			
	sendln inputstr			
				
	wait 'Password:' '% Bad secrets' '#'			
	;↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑			
	;「Password:」表示があればresult値が1、			
	;「% Bad secrets」表示があればresult値が2、			
	;「#」表示があればresult値が3			
				
	if result = 1 then			
	goto passopen3			
	elseif result = 2 then			
	goto start3			
	elseif result = 3 then			
	goto passclose3			
	endif			
				
:passclose3				
pause 2				
sendln ''				
wait '#'				
sendln 'terminal length 0'				
wait '#'				
pause 3				
sendln ''				
				
sendln 'show running-config | inc hostname'				
wait 'hostname'				
recvln				
strcopy inputstr 1 8 Host_Name				
;*************************************************				
;※↑show runのhostnameから始まる行で「hostname」のあと1~8文字目をHost_Nameとして使用する				
;文字数が違うなら修正して使用する				
;*************************************************				
				
fileopen FHcom2 'command.txt' 0				
while 1				
	filereadln FHcom2 comline			
		if result = 1 then		
		break		
		endif		
				
		COMMAND = comline		
				
		getdate DATE '_%Y-%m%d-%H%M%S.log'		
		filename = Host_Name		
		strconcat filename '_'		
		strconcat filename COMMAND		
		strconcat filename DATE		
				
		pause 3		
		sendln ''		
		wait '#'		
				
	logopen filename 0 0			
				
		sendln COMMAND		
		wait '#'		
		pause 2		
		sendln ''		
		wait '#'		
		pause 2		
				
	logclose			
endwhile				
				
fileclose FHcom2				
sendln ''				
wait '#'				
pause 3				
sendln 'exit'				
wait '#'				
				
goto telnetloop				
:telnetclose				
fileclose FHtelnet				
				
pause 2				
sendln ''				
wait '#'				
sendln 'exit'				
				
end				

マクロここまで↑↑↑

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?