6
6
from ec2instanceconnectcli .EC2InstanceConnectLogger import EC2InstanceConnectLogger
7
7
from ec2instanceconnectcli import input_parser
8
8
9
- DEFAULT_USER = ''
10
9
DEFAULT_INSTANCE = ''
11
- DEFAULT_PORT = 22
12
10
DEFAULT_PROFILE = None
13
11
14
12
def main (program , mode ):
@@ -22,45 +20,61 @@ def main(program, mode):
22
20
:type mode: basestring
23
21
"""
24
22
25
- parser = argparse .ArgumentParser (description = """Usage:
26
- * mssh-putty [user@]instance_id [-u profile] [-r region] [-z availability_zone] [-i identity_file_ppk] [standard ssh flags] [command]
27
- * mssh-putty [user@]dns_name -t instance_id [-u profile] [-r region] [-z availability_zone] [-i identity_file_ppk] [standard ssh flags] [command]
28
- * msftp-putty [user@]instance_id [-u profile] [-r region] [-z availability_zone] [-i identity_file_ppk] [standard sftp flags]
29
- * msftp-putty [user@]dns_name -t instance_id [-u profile] [-r region] [-z availability_zone] [-i identity_file_ppk] [standard sftp flags]
30
- """ )
31
- parser .add_argument ('-r' , '--region' , action = 'store' , help = 'AWS region' , type = str )
32
- parser .add_argument ('-z' , '--zone' , action = 'store' , help = 'Availability zone' , type = str )
33
- parser .add_argument ('-v' , '--verbose' , help = 'Enable logging' , action = "store_true" )
34
- parser .add_argument ('-i' , '--identity' , action = 'store' , help = "Required. Identity file in ppk format" , type = str )
35
- parser .add_argument ('-u' , '--profile' , action = 'store' , help = 'AWS Config Profile' , type = str , default = DEFAULT_PROFILE )
36
- parser .add_argument ('-t' , '--instance_id' , action = 'store' , help = 'EC2 Instance ID. Required if target is DNS name or IP address.' , type = str , default = DEFAULT_INSTANCE )
23
+ usage = ""
24
+ if mode == "ssh" :
25
+ usage = """
26
+ mssh-putty [-t instance_id] [-u profile] [-r region] [-z availability_zone] [-i identity_file_ppk] [supported putty flags] target
37
27
38
- try :
39
- args = parser .parse_known_args ()
40
- except :
41
- parser .print_help ()
42
- sys .exit (1 )
28
+ target => [user@]instance_id | [user@]dns_name
29
+ [supported putty flags] => [-l login_name] [ -P port] [-m remote_command_file]
30
+ """
31
+ elif mode == "sftp" :
32
+ usage = """
33
+ msftp-putty [-t instance_id] [-u profile] [-r region] [-z availability_zone] [-i identity_file_ppk] [supported psftp flags] target
34
+
35
+ target => [user@]instance_id | [user@]dns_name
36
+ [supported psftp flags] => [-l login_name] [ -P port] [-bc] [-b batchfile]
37
+ """
38
+
39
+ parser = argparse .ArgumentParser (usage = usage )
40
+ parser .add_argument ('-r' , '--region' , action = 'store' , help = 'AWS region' , type = str , metavar = '' )
41
+ parser .add_argument ('-z' , '--zone' , action = 'store' , help = 'Availability zone' , type = str , metavar = '' )
42
+ parser .add_argument ('-i' , '--identity' , action = 'store' , help = "Required. Identity file in ppk format" , type = str , required = True , metavar = '' )
43
+ parser .add_argument ('-u' , '--profile' , action = 'store' , help = 'AWS Config Profile' , type = str , default = DEFAULT_PROFILE , metavar = '' )
44
+ parser .add_argument ('-t' , '--instance_id' , action = 'store' , help = 'EC2 Instance ID. Required if target is hostname' , type = str , default = DEFAULT_INSTANCE , metavar = '' )
45
+ parser .add_argument ('-d' , '--debug' , action = "store_true" , help = 'Turn on debug logging' )
46
+
47
+ args = parser .parse_known_args ()
43
48
44
49
#Read public key from ppk file.
45
50
#Public key is unencrypted and in rsa format.
46
51
pub_key_lines = []
47
- with open (args [0 ].identity , 'r' ) as f :
48
- pub_key_lines = f .readlines ()
52
+ pub_key = "ssh-rsa "
53
+ try :
54
+ with open (args [0 ].identity , 'r' ) as f :
55
+ pub_key_lines = f .readlines ()
49
56
50
- #Validate that the identity file format is ppk.
51
- if pub_key_lines [0 ].find ("PuTTY-User-Key-File-" ) == - 1 :
52
- print ("Not a valid Putty key." )
53
- sys .exit (1 )
57
+ #Validate that the identity file format is ppk.
58
+ if pub_key_lines [0 ].find ("PuTTY-User-Key-File-" ) == - 1 :
59
+ print ("Not a valid Putty key." )
60
+ sys .exit (1 )
54
61
55
- #public key starts from 4th line in ppk file.
56
- line_len = int (pub_key_lines [3 ].split (':' )[1 ].strip ())
57
- pub_key_lines = pub_key_lines [4 :(4 + line_len )]
58
- pub_key = "ssh-rsa "
59
- for pub_key_line in pub_key_lines :
60
- pub_key += pub_key_line [:- 1 ]
62
+ #public key starts from 4th line in ppk file.
63
+ line_len = int (pub_key_lines [3 ].split (':' )[1 ].strip ())
64
+ pub_key_lines = pub_key_lines [4 :(4 + line_len )]
65
+ for pub_key_line in pub_key_lines :
66
+ pub_key += pub_key_line [:- 1 ]
67
+ except Exception as e :
68
+ print (str (e ))
69
+ sys .exit (1 )
61
70
62
- logger = EC2InstanceConnectLogger (args [0 ].verbose )
63
- instance_bundles , flags , program_command = input_parser .parseargs (args , mode )
71
+ logger = EC2InstanceConnectLogger (args [0 ].debug )
72
+ try :
73
+ instance_bundles , flags , program_command = input_parser .parseargs (args , mode )
74
+ except Exception as e :
75
+ print (str (e ))
76
+ parser .print_help ()
77
+ sys .exit (1 )
64
78
65
79
cli_command = EC2InstanceConnectCommand (program , instance_bundles , args [0 ].identity , flags , program_command , logger .get_logger ())
66
80
cli_command .get_command ()
0 commit comments