ギャバンITサービス
お菓子の家が作れるシステムエンジニアです

macでteratermマクロのようなこと(妄想)

ローカルのlinuxへの接続やgoogle cloudへの接続では、windowsからはtera termのマクロを書いておいて、それを使って接続している。 macはunixなのでtera termが動くわけじゃない。 でもログインしたときに慣用句みたいに打つ内容があるから、自動ログインした後で打鍵もやらせたい。 dfの結果とか、自前スクリプトによるプロセス稼働確認とか。 今回はmac側でやったことの記録。 結論 macで.terminalのファイルをクリックすると、tera termでttlファイル書いておいたときのように、linuxに接続してコマンドラインを自動入力して動いてくれるようにしてる。 ローカルlinuxの場合、黄色い画面色で開いてログインし接続後にonlchkってやって自前スクリプトを呼び出し、システム状態を表示させてる。 色合いとか調整した設定を.terminalって種類のファイルとして書き出し、それをクリックすることで自動接続&打鍵のスクリプトを呼び出している。 このスクリプトは大昔に作ったんやけど、当時に参考にさせてもらったサイトがどこなのかわからん。もう存在しないのかなぁ。探し方悪いだけかもしれん。 用意してるのは3種類のファイル。 当初はlinuxの種類に応じて色を変えたいだけだったけど、欲張って自動ログインとシステム状態確認をやらせたくなった。 .terminalの設定ファイル autologinってログインのための入力内容 呼び出しのスクリプト .terminalの設定ファイル 設定ファイルは、ダブルクリックすると設定した色合いやサイズ、シェルが起動する。 tera termでいう/Fで指定するウィンドウの色とか指定するときのファイルみたいなもんか。 このファイルは、ターミナルの環境設定を開いたとき「プロファイル」の画面が開くとこから作る。 テキストの色とかウィンドウサイズを設定し、「コマンドを実行」ってとこで起動時に呼び出すシェルを指定する。 自分の場合は、赤がgoogle cloud、黄色をローカルlinux、dockerコンテナを青か緑にしてtera termの色設定しているので、それに寄せて設定を作っている。 エコーバックに対する文字列送信のスクリプト tera termでいうttlファイルみたいなもの。 autologin-xxx.shってのをいくつか用意して、ローカルlinux用とgoogle cloud用とか使い分けてる。 実際は.terminalで設定した起動時の呼び出しスクリプトから最初にインクルードさせて使う。 autologinで始まるシェルに引数を受け取りつつ、打鍵内容を書いておく。 パラメータは、接続先ホスト、利用osユーザ、パスワード、ポート番号。 コマンドラインのエコーバックに来た文字列をexpectに書いておき、それに対する送信文字列をsendさせる。 最初のほうにある「Are you sure~」のあたりは初めてログインするときの確認エコーバック。 長らくこのエコーバック見てないけど、大文字小文字も厳密に書かないといけない。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #!/bin/sh auto_ssh() { host=$1 id=$2 pass=$3 ptNum=$4 ptTime=10 expect -c " set timeout ${ptTime} spawn ssh ${id}@${host} -p ${ptNum} expect \"Are you sure you want to continue connecting (yes/no)?\" { send \"yes\n\" expect \"${id}@${host}'s password:\" send \"${pass}\n\" } \"${id}@${host}'s password:\" { send \"${pass}\n\" expect \"nari\" send \"onlchk\n\" expect \"nari\" send \"df -h\n\" } interact " } montereyになった今ではnari@gvisMac ~ %って感じで「利用ユーザ@ホスト名 ホームディレクトリ」がコマンドライン待ちに来るけど、ユーザ名のみをexpectさせてonlchkとdf -hって打たせたいので書いてる。 ...

scriptWin-teraTerm-part1

ssh接続 1 2 3 4 5 ;単純接続 connect '192.168.1.38 /ssh /2 /auth=password /user=root /passwd=password /F=yellow.ini' wait "]$" sendln "df -h" ssh接続マクロが参照する定義ファイルをy:\teraIni\gvis.iniに用意 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 gvis_iPTH = 'y:\\teraIni\\' gvis_wS1 = ']$' gvis_wS2 = ']#' gvis_wS3 = 'パスワード:' gvis_wS4 = 'nari$' gvis_wS5 = 'Password:' gvis_wS6 = 'C:\Users\nari>' gvis_ws7 = 'nari@MacX-nari' gvis_nari = 'password1' gvis_nari2= 'password2' gvis_MAIN = 'password3' gvis_winP = 'winPassword' gvis_macP = 'macPassword' gvis_WhP = 'drivePassword' gvis_KY1 = 'Y:\\terIni\\key\\privateKEY' ;---- ;---- zip -r -P xx dirname.zip ./dirname ;---- unzip -P xx dirname.zip ;---- ;---- 記号を混ぜるとNG gvis_psw1 = 'longlongPassword1' gvis_psw2 = 'longlongPassword2' gvis_pswP = 'longlongPassword3' gvis_TOUT = '5' gvis_COUT = 10 gvis_Yps1 = 'routerPassword1' gvis_Yps2 = 'routerPassword2' gvis_YwS1 = '>' gvis_YwS2 = '#' gvis_YwS3 = 'Password:' gvis_YCot = 3 定義ファイルを利用してローカルlinuxへssh接続 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 include 'y:\teraIni\gvis.ini' ; --- 接続 -------------- conSTR = '172.16.17.218 /ssh /2 /auth=password /user=nari /passwd=' strconcat conSTR gvis_nari strconcat conSTR ' /F=' strconcat conSTR gvis_iPTH strconcat conSTR 'yellow.ini ' strconcat conSTR '/timeout=' strconcat conSTR gvis_TOUT ; ---debug -------------- ; messagebox conSTR 'info' connect conSTR if result <> 2 goto end wait gvis_wS1 sendln "df -h" wait gvis_wS1 sendln "w" wait gvis_wS1 sendln "onlchk" :end 定義ファイルを利用してGoogle Cloudのlinuxへssh接続 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 include 'y:\teraIni\gvis.ini' ; --- 接続 -------------- conSTR = 'gcp-gvis-dklinux.intra.gavann-it.com /ssh /auth=publickey /user=nari /passwd=' strconcat conSTR gvis_MAIN strconcat conSTR ' /keyfile=' strconcat conSTR gvis_KY1 strconcat conSTR ' /F=' strconcat conSTR gvis_iPTH strconcat conSTR 'wine.ini ' strconcat conSTR '/timeout=' strconcat conSTR gvis_TOUT ; ---debug -------------- ; messagebox conSTR 'info' connect conSTR if result <> 2 goto end wait gvis_wS1 sendln "df -h" wait gvis_wS1 sendln "w" :end teratermではフォントや色などの表示指定を保存して作成しておく(tertermウィンドウの「設定」->「設定の保存」)。以下、wine.iniで赤っぽい配色の定義サンプル。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 [Tera Term] Version=4.98 Language=Japanese Port=tcpip TerminalSize=90,35 TermIsWin=on AutoWinResize=off CRReceive=CR CRSend=CR LocalEcho=off Answerback= KanjiReceive=UTF-8 KatakanaReceive=8 KanjiSend=UTF-8 KatakanaSend=8 KanjiIn=B KanjiOut=B ConnectingTimeout=5 DisablePasteMouseRButton=off ConfirmPasteMouseRButton=off SelectOnlyByLButton=on DisableAcceleratorSendBreak=off EnableContinuedLineCopy=on MouseCursor=ibeam AlphaBlend=255 CygwinDirectory=c:\cygwin ViewlogEditor=notepad.exe Locale=japanese CodePage=932 ANSIColor=0,0,0,0, 1,255,0,0, 2,0,255,0, 3,255,255,0, 4,128,128,255, 5,255,0,255, 6,0,255,255, 7,255,255,255, 8,0,0,0, 9,255,0,0, 10,0,255,0, 11,255,255,0, 12,128,128,255, 13,255,0,255, 14,0,255,255, 15,255,255,255 AutoWinSwitch=off TerminalID=VT100 RussHost=Windows RussClient=Windows Title=Tera Term CursorShape=block HideTitle=off PopupMenu=off FullColor=on EnableScrollBuff=on ScrollBuffSize=10000 VTColor=255,29,202,72,27,21 VTBoldColor=0,0,255,72,27,21 VTBlinkColor=255,0,0,72,27,21 EnableClickableUrl=on URLColor=0,255,0,72,27,21 TEKColor=0,0,0,255,255,255 TEKColorEmulation=off VTFont=MS 明朝,0,-16,128 EnableBold=on RussFont=Windows TEKFont=Terminal,0,-8,128 BSKey=BS DeleteKey=on MetaKey=off DisableAppKeypad=off DisableAppCursor=off RussKeyb=Windows ComPort=1 BaudRate=9600 Parity=none DataBit=8 StopBit=1 FlowCtrl=none DelayPerChar=0 DelayPerLine=0 Telnet=off TermType=vt100 TCPPort=22 AutoWinClose=on Historylist=on TransBin=off LogAppend=on LogTypePlainText=on LogTimestamp=off LogDefaultName=teraterm.log LogDefaultPath= LogAutoStart=off XmodemOpt=checksum XmodemBin=on XmodemRcvCommand= FileDir=C:\Program Files (x86)\teraterm FileSendFilter= Accept8BitCtrl=on AllowWrongSequence=off AutoFileRename=on AutoTextCopy=on BackWrap=off Beep=on BeepOnConnect=off BPAuto=off BPEscCtl=off BPLog=off ClearComBuffOnOpen=on ConfirmDisconnect=on CtrlInKanji=on Debug=off DelimList=$20!"#$24%&'()*+,:;<=>?@[\]^`{|}~ DelimDBCS=on EnablePopupMenu=on EnableShowMenu=on EnableStatusLine=on IME=on IMEInline=on KmtLog=off LanguageSelection=on MaxBuffSize=500000 MaxComPort=256 NonblinkingCursor=off PassThruDelay=3 PassThruPort= PrnFont= PrnMargin=50,50,50,50 QVLog=off QVWinSize=8 RussPrint=Windows ScrollThreshold=12 SelectOnActivate=on Send8BitCtrl=off StartupMacro= TEKGINMouseCode=32 TelAutoDetect=on TelBin=off TelEcho=off TelLog=off TelPort=23 TelKeepAliveInterval=300 MaxBroadcatHistory=99 TCPLocalEcho=off TCPCRSend= UseTextColor=off TitleFormat=61 VTCompatTab=off VTFontSpace=0,0,0,0 VTPPI=0,0 TEKPPI=0,0 WindowMenu=on XmodemLog=off ZmodemAuto=off ZmodemDataLen=1024 ZmodemWinSize=32767 ZmodemEscCtl=off ZmodemLog=off ZmodemRcvCommand=rz UseNormalBGColor=on UILanguageFile=lang\Japanese.lng BroadcastCommandHistory=on AcceptBroadcast=on ConfirmFileDragAndDrop=on DisablePasteMouseMButton=on ConfirmChangePaste=on ConfirmChangePasteCR=on ConfirmChangePasteStringFile= ScrollWindowClearScreen=on PcBoldColor=off Aixterm16Color=off Xterm256Color=on VTReverseColor=255,255,255,0,0,0 EnableBoldAttrColor=on EnableBlinkAttrColor=on EnableReverseAttrColor=off EnableURLColor=on URLUnderline=on EnableANSIColor=on LogBinary=off LogHideDialog=off LogIncludeScreenBuffer=off LogTimestampFormat=%Y-%m-%d %H:%M:%S.%N LogTimestampType=Local LogRotate=0 LogRotateSize=0 LogRotateSizeType=0 LogRotateStep=0 DeferredLogWriteMode=on ScpSendDir= AutoComPortReconnect=on WaitCom=off FileSendHighSpeedMode=on KmtLongPacket=off KmtFileAttr=off KillFocusCursor=on PrinterCtrlSequence=on PrnConvFF=off MouseWheelScrollLine=3 SendBreakTime=1000 YmodemLog=off YmodemRcvCommand=rb TranslateWheelToCursor=on HostDialogOnStartup=on MouseEventTracking=on MaximizedBugTweak=2 UnicodeToDecSpMapping=3 AutoScrollOnlyInBottomLine=off UnknownUnicodeCharacterAsWide=off AcceptTitleChangeRequest=overwrite PasteDialogSize=330,220 DisableMouseTrackingByCtrl=on DisableWheelToCursorByCtrl=on StrictKeyMapping=off Wait4allMacroCommand=off DisableMenuSendBreak=off ClearScreenOnCloseConnection=off DisableAcceleratorDuplicateSession=off AcceleratorNewConnection=on AcceleratorCygwinConnection=on DisableMenuDuplicateSession=off DisableMenuNewConnection=off PasteDelayPerLine=10 Meta8Bit=off WindowCtrlSequence=on CursorCtrlSequence=off WindowReportSequence=on TitleReportSequence=empty EnableLineMode=on ClearOnResize=on AlternateScreenBuffer=on IMERelatedCursor=off TerminalUID=FFFFFFFF LockTUID=on JumpList=on TabStopModifySequence=on ClipboardAccessFromRemote=off NotifyClipboardAccess=on ClickableUrlBrowser= ClickableUrlBrowserArg= LogLockExclusive=on FontQuality=default BeepOverUsedCount=5 BeepOverUsedTime=2 BeepSuppressTime=5 MaxOSCBufferSize=4096 JoinSplitURL=off JoinSplitURLIgnoreEOLChar=\ DebugModes=all XmodemTimeouts=10,3,10,20,60 YmodemTimeouts=10,3,10,20,60 ZmodemTimeouts=10,0,10,3 TrimTrailingNLonPaste=off NormalizeLineBreakOnPaste=off ListHiddenFonts=off ISO2022ShiftFunction=on TerminalSpeed=38400 [BG] BGEnable=off BGUseAlphaBlendAPI=on BGSPIPath=plugin BGFastSizeMove=on BGFlickerlessMove=on BGNoFrame=on BGThemeFile=theme\*.ini [TTSSH] Enabled=1 Compression=0 DefaultUserName= DefaultForwarding= CipherOrder=78;9:3MK>HJ=GLI<FCDEB026A@? KnownHostsFiles=ssh_known_hosts DefaultRhostsLocalUserName= DefaultRhostsHostPrivateKeyFile= DefaultRSAPrivateKeyFile= DefaultAuthMethod=3 LogLevel=0 WriteBufferSize=2097152 LocalForwardingIdentityCheck=1 ProtocolVersion=2 HeartBeat=60 RememberPassword=1 CheckAuthListFirst=0 KexOrder=567:9843210 HostKeyOrder=4567230 MacOrder=@?:861>5;20<=34 CompOrder=3210 EnableRsaShortKeyServer=0 ForwardAgent=0 ForwardAgentConfirm=1 ForwardAgentNotify=1 VerifyHostKeyDNS=1 SSHIcon=Default DisablePopupMessage=0 X11Display= UpdateHostkeys=0 GexMinimalGroupSize=0 AuthBanner=1 [TTProxy] ConnectionTimeout="10" SocksResolve="auto" TelnetHostnamePrompt=">> Host name: " TelnetUsernamePrompt="Username:" TelnetPasswordPrompt="Password:" TelnetConnectedMessage="-- Connected to " TelnetErrorMessage="!!!!!!!!"

scriptWin-teraTerm-part2

windowsのteratermスクリプト - part2 part1で利用したy:\teraIni\gvis.iniを利用する前提 定義ファイルを利用してローカルlinuxへssh接続し、定義ファイルに書いたパスワードを引数に渡してバックアップ処理のスクリプトを実行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 include 'y:\teraIni\gvis.ini' ; --- 接続 -------------- conSTR = 'xxx.xxx.xxx.xxx /ssh /2 /auth=password /user=nari /passwd=' strconcat conSTR gvis_nari strconcat conSTR ' /F=' strconcat conSTR gvis_iPTH strconcat conSTR 'yellow.ini ' strconcat conSTR '/timeout=' strconcat conSTR gvis_TOUT ; ---debug -------------- ; messagebox conSTR 'info' connect conSTR if result <> 2 goto end wait gvis_wS1 sendln "su - " wait gvis_wS3 sendln gvis_MAIN conSTR = "/bin/sh /gvis/script/002_GVISbackup_Image.sh " strconcat conSTR gvis_psw1 wait gvis_wS2 sendln conSTR wait gvis_wS2 sendln "exit" wait gvis_wS1 sendln "exit" 定義ファイルを利用してローカルlinuxへssh接続し、google cloudのlinuxを起動 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 include 'y:\teraIni\gvis.ini' ; --- 接続 -------------- conSTR = 'xxx.xxx.xxx.xxx /ssh /2 /auth=password /user=nari /passwd=' strconcat conSTR gvis_nari strconcat conSTR ' /F=' strconcat conSTR gvis_iPTH strconcat conSTR 'yellow.ini ' strconcat conSTR '/timeout=' strconcat conSTR gvis_TOUT ; ---debug -------------- ; messagebox conSTR 'info' connect conSTR if result <> 2 goto end wait gvis_wS1 sendln "cd /gvis/log" wait gvis_wS1 sendln "date >> 003_gcpLog.log" wait gvis_wS1 sendln "echo '------before Start status------' >> 003_gcpLog.log ; gcloud compute instances list | grep gvis-nadklinux >> 003_gcpLog.log" wait gvis_wS1 sendln "echo '------ Start gcp ------' >> 003_gcpLog.log ; gcloud compute instances start gvis-nadklinux >> 003_gcpLog.log" wait gvis_wS1 sendln "sleep 10 ; echo '------after Start status ------' >> 003_gcpLog.log ; gcloud compute instances list | grep gvis-nadklinux >> 003_gcpLog.log" wait gvis_wS1 sendln "exit" :end google cloudの操作をするためには事前に、ローカルlinuxにgcloudコマンドが使える環境を作成し(google-cloud-sdkをdnfしておく)、google cloud上に作成したプロジェクトと紐付けておくこと。

Windowsのサービスをコントロールするスクリプト

linuxでサービス起動・停止・チェックをスクリプト化してる。 oracle express/sqlserver express/postgresql13,14をサービスとして扱い、windowsでもやってみた。 ベターっと書いてるけど、書き方思い出せないときのためのメモ。 使うコマンド こんな感じで起動と停止。 1 2 net start (サービス名) net stop (サービス名) チェックはこんな感じ。 1 2 3 4 5 6 sc query (サービス名) | find "RUNNING" > NUL if %ERRORLEVEL% == 0 ( echo (サービス名) -- Running ) else ( echo (サービス名) -- Down ) Oracle起動 なんでか知らんけどVssWriterXEの操作うまくいかへんかったからコメント化。 ...