2
2

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 1 year has passed since last update.

CSVからTera Term Menuを生成するスクリプトを作った

Posted at

仕事でTera Term Menuを使っているところに出くわしました。
サーバーの管理表はあるのですが、それと別にTera Term Menuがあります。
もらったTera Term Menuがこれまでの消えたサーバーとかIPアドレス変わったやつとかあってやたら多いしつながらない!
というわけで管理表からTera Term Menuを作るスクリプトを作りました。
蓋を開けてみればレジストリをいじるだけなので簡単でした。
しかもレジストリのファイルはini形式のテキストファイル!
面倒なパーサーいらずで作れました。no pip 最高!

一応いっておきますけどMITライセンスです。無保証です。

使い方

python csvmenu.py {変換するcsv} 

python csvmenu.py servers.csv

csvの中身はこんな感じです。ただレジストリに投げるやつをあげただけ。

servers.csv
Name,HostName,Mode,UserFlag,UserName,PasswdFlag,Password,TeraTerm,INI_File,Option,LoginPrompt,PasswdPrompt,MacroFile,TeraTerm Mode,Startup,Log,PrivateKeyFile,Challenge,Pageant
例,192.168.0.1,0,1,admin,1,pass,.\\ttermpro.exe,.\\TERATERM_MENU.INI,/KT=UTF8 /KR=UTF8,login:,password:,C:example.ttl,1,0,C:example.log,C:id_rsa,0,0
teraterm_csvmenu.py
import os
import sys
import csv
import configparser

filepath=sys.argv[1]

csv_file = open(filepath , "r", encoding="ms932")
reader = csv.DictReader(csv_file, delimiter=",", doublequote=True, lineterminator="\r\n", quotechar='"', skipinitialspace=True)

def to_dword(s):
  return 'dword:'+str(int(s)).zfill(8) if s else "\"\""

# ttpmenu\winmisc#106参照。疲れた。。。
def encode_password(s):
  return_array=['00']
  for cha in list(s):
    return_array.append('{:02x}'.format(int(cha.encode().hex() ,16) ^ 0xff))
  return ','.join(reversed(return_array))

def to_hex(s):
  return 'hex:'+encode_password(s)

def to_escape(s):
  return "\"{}\"".format(s) if s else "\"\""



default_config={
"HostName":"",
"Mode":"0",
"UserFlag":"0",
"UserName":"",
"PasswdFlag":"0",
"Password":'',
"TeraTerm":".\\ttermpro.exe",
"INI_File":".\\TERATERM.INI",
"Option":"",
"LoginPrompt":"login:",
"PasswdPrompt":"Password:",
"MacroFile":"",
"TeraTerm Mode":"0",
"Startup":"0",
"Log":"",
"PrivateKeyFile":"",
"Challenge":"0",
"Pageant":"0",
}


config = configparser.ConfigParser()

for row in reader:
  if row['Name']=='': continue 
  reg_dir='HKEY_CURRENT_USER\\Software\\ShinpeiTools\\TTermMenu\\'+row['Name']
  config[reg_dir]={}
  config[reg_dir]['HostName']=to_escape(row['HostName'] or default_config['HostName'])
  config[reg_dir]['Mode']=to_dword(row['Mode'] or default_config['Mode'])
  config[reg_dir]['UserFlag']=to_dword(row['UserFlag'] or default_config['UserFlag'])
  config[reg_dir]['UserName']=to_escape(row['UserName'] or default_config['UserName'])
  config[reg_dir]['PasswdFlag']=to_dword(row['PasswdFlag'] or default_config['PasswdFlag'])
  config[reg_dir]['Password']=to_hex(row['Password'] or default_config['Password'])
  config[reg_dir]['TeraTerm']=to_escape(row['TeraTerm'] or default_config['TeraTerm'])
  config[reg_dir]['INI_File']=to_escape(row['INI_File'] or default_config['INI_File'])
  config[reg_dir]['Option']=to_escape(row['Option'] or default_config['Option'])
  config[reg_dir]['LoginPrompt']=to_escape(row['LoginPrompt'] or default_config['LoginPrompt'])
  config[reg_dir]['PasswdPrompt']=to_escape(row['PasswdPrompt'] or default_config['PasswdPrompt'])
  config[reg_dir]['MacroFile']=to_escape(row['MacroFile'] or default_config['MacroFile'])
  config[reg_dir]['TeraTerm Mode']=to_dword(row['TeraTerm Mode'] or default_config['TeraTerm Mode'])
  config[reg_dir]['Startup']=to_dword(row['Startup'] or default_config['Startup'])
  config[reg_dir]['Log']=to_escape(row['Log'] or default_config['Log'])
  config[reg_dir]['PrivateKeyFile']=to_escape(row['PrivateKeyFile'] or default_config['PrivateKeyFile'])
  config[reg_dir]['Challenge']=to_dword(row['Challenge'] or default_config['Challenge'])
  config[reg_dir]['Pageant']=to_dword(row['Pageant'] or default_config['Pageant'])



aaa="""
config['HKEY_CURRENT_USER\Software\ShinpeiTools\TTermMenu\129_SCUL#1'] ={
"HostName":"w27sk-----scul000100",
"Mode":0,
"UserFlag":0,
"UserName":"root",
"PasswdFlag":0,
"Password":b'\x8b\x90\x90\x8d\x00',
"TeraTerm":".\\ttermpro.exe",
"INI_File":".\\TERATERM_MENU.INI",
"Option":"",
"LoginPrompt":"login:",
"PasswdPrompt":"Password:",
"MacroFile":"",
"TeraTerm Mode":1,
"Startup":0,
"Log":"",
"PrivateKeyFile":"",
"Challenge":1,
"Pageant":0,
}
"""

return_str="""Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\ShinpeiTools\TTermMenu]
"IconMode"=dword:00000000
"LeftButtonPopup"=dword:00000001
"Hotkey"=dword:00000000
"MenuTextColor"=dword:00000000
"lfHeight"=dword:fffffff4
"lfWidth"=dword:00000000
"lfEscapement"=dword:00000000
"lfOrientation"=dword:00000000
"lfWeight"=dword:00000190
"lfItalic"=dword:00000000
"lfUnderline"=dword:00000000
"lfStrikeOut"=dword:00000000
"lfCharSet"=dword:00000080
"lfOutPrecision"=dword:00000000
"lfClipPrecision"=dword:00000000
"lfQuality"=dword:00000000
"lfPitchAndFamily"=dword:00000000
"lfFaceName"="MS UI Gothic"

"""


for section in config.sections():
    return_str+="[{}]".format(section)+'\n'
    for key in default_config.keys() :
      return_str+="\"{}\"={}".format(key,config[section][key])+'\n'
    return_str+='\n'

with open(os.path.splitext(os.path.basename(filepath))[0]+'.reg','w') as f:
  f.write(return_str)

Tera Term Projectにマージしてもらえればなあ…

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?