@@ -995,6 +995,9 @@ def get_last_query(self):
995995@click .option ('--ssh-port' , default = 22 , help = 'Port to connect to ssh server.' )
996996@click .option ('--ssh-password' , help = 'Password to connect to ssh server.' )
997997@click .option ('--ssh-key-filename' , help = 'Private key filename (identify file) for the ssh connection.' )
998+ @click .option ('--ssh-config-path' , help = 'Path to ssh configuration.' ,
999+ default = os .path .expanduser ('~' ) + '/.ssh/config' )
1000+ @click .option ('--ssh-config-host' , help = 'Host to connect to ssh server reading from ssh configuration.' )
9981001@click .option ('--ssl-ca' , help = 'CA file in PEM format.' ,
9991002 type = click .Path (exists = True ))
10001003@click .option ('--ssl-capath' , help = 'CA directory.' )
@@ -1016,6 +1019,8 @@ def get_last_query(self):
10161019 help = 'Use DSN configured into the [alias_dsn] section of myclirc file.' )
10171020@click .option ('--list-dsn' , 'list_dsn' , is_flag = True ,
10181021 help = 'list of DSN configured into the [alias_dsn] section of myclirc file.' )
1022+ @click .option ('--list-ssh-config' , 'list_ssh_config' , is_flag = True ,
1023+ help = 'list ssh configurations in the ssh config (requires paramiko).' )
10191024@click .option ('-R' , '--prompt' , 'prompt' ,
10201025 help = 'Prompt format (Default: "{0}").' .format (
10211026 MyCli .default_prompt ))
@@ -1048,7 +1053,7 @@ def cli(database, user, host, port, socket, password, dbname,
10481053 ssl_ca , ssl_capath , ssl_cert , ssl_key , ssl_cipher ,
10491054 ssl_verify_server_cert , table , csv , warn , execute , myclirc , dsn ,
10501055 list_dsn , ssh_user , ssh_host , ssh_port , ssh_password ,
1051- ssh_key_filename ):
1056+ ssh_key_filename , list_ssh_config , ssh_config_path , ssh_config_host ):
10521057 """A MySQL terminal client with auto-completion and syntax highlighting.
10531058
10541059 \b
@@ -1085,6 +1090,31 @@ def cli(database, user, host, port, socket, password, dbname,
10851090 else :
10861091 click .secho (alias )
10871092 sys .exit (0 )
1093+ if list_ssh_config :
1094+ if not paramiko :
1095+ click .secho (
1096+ "This features requires paramiko. Please install paramiko and try again." ,
1097+ err = True , fg = 'red'
1098+ )
1099+ exit (1 )
1100+ try :
1101+ ssh_config = paramiko .config .SSHConfig ().from_path (ssh_config_path )
1102+ except paramiko .ssh_exception .ConfigParseError as err :
1103+ click .secho ('Invalid SSH configuration file. '
1104+ 'Please check the SSH configuration file.' ,
1105+ err = True , fg = 'red' )
1106+ exit (1 )
1107+ except FileNotFoundError as e :
1108+ click .secho (str (e ), err = True , fg = 'red' )
1109+ exit (1 )
1110+ for host in ssh_config .get_hostnames ():
1111+ if verbose :
1112+ host_config = ssh_config .lookup (host )
1113+ click .secho ("{} : {}" .format (
1114+ host , host_config .get ('hostname' )))
1115+ else :
1116+ click .secho (host )
1117+ sys .exit (0 )
10881118 # Choose which ever one has a valid value.
10891119 database = dbname or database
10901120
@@ -1135,6 +1165,32 @@ def cli(database, user, host, port, socket, password, dbname,
11351165 if not port :
11361166 port = uri .port
11371167
1168+ if ssh_config_host :
1169+ if not paramiko :
1170+ click .secho (
1171+ "This features requires paramiko. Please install paramiko and try again." ,
1172+ err = True , fg = 'red'
1173+ )
1174+ exit (1 )
1175+ try :
1176+ ssh_config = paramiko .config .SSHConfig ().from_path (ssh_config_path )
1177+ except paramiko .ssh_exception .ConfigParseError as err :
1178+ click .secho ('Invalid SSH configuration file. '
1179+ 'Please check the SSH configuration file.' ,
1180+ err = True , fg = 'red' )
1181+ exit (1 )
1182+ except FileNotFoundError as e :
1183+ click .secho (str (e ), err = True , fg = 'red' )
1184+ exit (1 )
1185+ ssh_config = ssh_config .lookup (ssh_config_host )
1186+ ssh_host = ssh_host if ssh_host else ssh_config .get ('hostname' )
1187+ ssh_user = ssh_user if ssh_user else ssh_config .get ('user' )
1188+ if ssh_config .get ('port' ) and ssh_port == 22 :
1189+ # port has a default value, overwrite it if it's in the config
1190+ ssh_port = int (ssh_config .get ('port' ))
1191+ ssh_key_filename = ssh_key_filename if ssh_key_filename else ssh_config .get (
1192+ 'identityfile' , ['' ])[0 ]
1193+
11381194 if not paramiko and ssh_host :
11391195 click .secho (
11401196 "Cannot use SSH transport because paramiko isn't installed, "
0 commit comments