@@ -50,20 +50,93 @@ def retrieve_file(config_path):
50
50
51
51
with open (config_path , "r" ) as json_file :
52
52
config = json .load (json_file )
53
+ if config .get ('user' ) is None :
54
+ config ['user' ] = get_user_from_os ()
55
+
56
+ if config .get ('password' ):
57
+ raise NotImplementedError ("Password authentication is not supported yet." )
58
+
59
+ if config .get ('identity_file' ):
60
+ config ['identity_file' ] = find_identity_file (config ['identity_file' ])
61
+ else :
62
+ # Declare the default identity file of id_rsa
63
+ config ['identity_file' ] = os .path .join (get_user_ssh_dir_from_home (), 'id_rsa' )
64
+
53
65
return config
54
66
55
67
68
+ def get_user_from_os ():
69
+ """
70
+ Attempt to get the user from the OS; assumes that the user you are trying to connect is the same as the user
71
+ that is invoking the command.
72
+ :return:
73
+ """
74
+ return os .getlogin ()
75
+
76
+
77
+ def get_user_ssh_dir_from_home ():
78
+ """
79
+ Gets the SSH directory for the current user.
80
+ :return:
81
+ """
82
+ return os .path .join (os .path .expanduser ('~' ), '.ssh' )
83
+
84
+
85
+ def find_identity_file (identity_file ):
86
+ """
87
+ Finds the identity file from the SSH directory.
88
+ :param identity_file:
89
+ :return:
90
+ """
91
+ # Determine if the identity file is just the name of a file or if it's a path:
92
+ if os .path .isabs (identity_file ) or identity_file .startswith ('~' ):
93
+ if os .path .exists (identity_file ):
94
+ return identity_file
95
+ else :
96
+ raise FileNotFoundError (f"Identity file not found: { identity_file } " )
97
+ else :
98
+ ssh_dir = get_user_ssh_dir_from_home ()
99
+ identity_file_path = os .path .join (ssh_dir , identity_file )
100
+ if os .path .exists (identity_file_path ):
101
+ return identity_file_path
102
+ else :
103
+ raise FileNotFoundError (f"Identity file not found: { identity_file_path } " )
104
+
105
+ def remove_double_spaces (string ):
106
+ """
107
+ Removes double spaces from a string.
108
+ :param string:
109
+ :return:
110
+ """
111
+ return ' ' .join (string .split ())
112
+
56
113
def connect_shell (config_path ):
57
114
config = retrieve_file (config_path )
58
- command = f"ssh { config ['user' ]} @{ config ['host' ]} -p { config ['port' ]} "
59
- print (f'Running: { command } ' )
115
+
116
+ fp = ''
117
+ if config .get ('forward_ports' ):
118
+ for p in config ['forward_ports' ]:
119
+ fp += f' -L { p } '
120
+
121
+ idf = f'-i { config ["identity_file" ]} '
122
+
123
+ command = f"ssh { fp } { idf } { config ['user' ]} @{ config ['host' ]} -p { config ['port' ]} "
124
+ command = remove_double_spaces (command )
125
+
126
+ if config .get ('start_commands' ):
127
+ raise NotImplementedError ("Start commands are not supported yet." )
128
+ command = [command ] + config ['start_commands' ]
129
+
60
130
return command
61
131
62
132
63
133
def connect_sftp (config_path ):
64
134
config = retrieve_file (config_path )
65
- command = f"sftp -P { config ['port' ]} { config ['user' ]} @{ config ['host' ]} "
66
- print (f'Running: { command } ' )
135
+
136
+ idf = f"-i { config ['identity_file' ]} "
137
+
138
+ command = f"sftp { idf } -P { config ['port' ]} { config ['user' ]} @{ config ['host' ]} "
139
+ command = remove_double_spaces (command )
67
140
return command
68
141
69
142
0 commit comments