Skip to content

Commit 2b4fa1a

Browse files
committed
Add support for SSH Keys to login over sftp
1 parent a7ef129 commit 2b4fa1a

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

bin/v-add-backup-host

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ EOF
5050

5151
# Defining sftp command function
5252
sftpc() {
53+
if [ "$privatekey" != "yes" ]; then
54+
5355
expect -f "-" <<EOF "$@"
5456
set count 0
5557
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$port $user@$host
@@ -98,6 +100,52 @@ sftpc() {
98100
99101
exit \$rc
100102
EOF
103+
else
104+
expect -f "-" <<EOF "$@"
105+
set count 0
106+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$port -i $raw_password $user@$host
107+
expect {
108+
109+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
110+
set count \$argc
111+
set output "Disconnected."
112+
set rc $E_FTP
113+
exp_continue
114+
}
115+
116+
-re ".*denied.*(publickey|password)." {
117+
set output "Permission denied, wrong publickey or password."
118+
set rc $E_CONNECT
119+
}
120+
121+
"sftp>" {
122+
if {\$count < \$argc} {
123+
set arg [lindex \$argv \$count]
124+
send "\$arg\r"
125+
incr count
126+
} else {
127+
send "exit\r"
128+
set output "Disconnected."
129+
if {[info exists rc] != 1} {
130+
set rc $OK
131+
}
132+
}
133+
exp_continue
134+
}
135+
136+
timeout {
137+
set output "Connection timeout."
138+
set rc $E_CONNECT
139+
}
140+
}
141+
142+
if {[info exists output] == 1} {
143+
puts "\$output"
144+
}
145+
146+
exit \$rc
147+
EOF
148+
fi
101149
}
102150

103151
#----------------------------------------------------------#
@@ -109,7 +157,19 @@ if [ "$type" != 'local' ];then
109157
is_format_valid 'host' 'path' 'port'
110158
is_type_valid 'sftp,ftp,b2' "$type"
111159
is_username_format_valid "$user" "username"
112-
is_password_valid
160+
161+
privatekey="no"
162+
if [ -f "$raw_password" ]; then
163+
if [[ $(cat "$raw_password" | grep "OPENSSH PRIVATE") ]]; then
164+
privatekey="yes"
165+
password="$raw_password"
166+
else
167+
is_password_valid
168+
fi
169+
else
170+
is_password_valid
171+
fi
172+
113173
format_no_quotes "$password" "password"
114174

115175
if [ "$type" = 'sftp' ]; then
@@ -224,7 +284,7 @@ fi
224284
# Adding backup host
225285
if [ $type != 'local' ] && [ $type != 'b2' ]; then
226286
new_timestamp
227-
str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'"
287+
str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'\nPRIVATEKEY='$privatekey'"
228288
str="$str\nBPATH='$path'\nPORT='$port'\nTIME='$time'\nDATE='$date'"
229289
echo -e "$str" > $HESTIA/conf/$type.backup.conf
230290
chmod 660 $HESTIA/conf/$type.backup.conf

func/backup.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ ftp_delete() {
197197
# SFTP Functions
198198
# sftp command function
199199
sftpc() {
200+
if [ $PRIVATEKEY != "yes" ]; then
200201
expect -f "-" <<EOF "$@"
201202
set timeout 60
202203
set count 0
@@ -251,6 +252,65 @@ sftpc() {
251252
252253
exit \$rc
253254
EOF
255+
256+
else
257+
258+
expect -f "-" <<EOF "$@"
259+
set timeout 60
260+
set count 0
261+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
262+
-o Port=$PORT -i $PASSWORD $USERNAME@$HOST
263+
expect {
264+
-nocase "password:" {
265+
send "$PASSWORD\r"
266+
exp_continue
267+
}
268+
269+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
270+
set count \$argc
271+
set output "Disconnected."
272+
set rc $E_FTP
273+
exp_continue
274+
}
275+
276+
-re ".*denied.*(publickey|password)." {
277+
set output "Permission denied, wrong publickey or password."
278+
set rc $E_CONNECT
279+
}
280+
281+
-re "\[0-9]*%" {
282+
exp_continue
283+
}
284+
285+
"sftp>" {
286+
if {\$count < \$argc} {
287+
set arg [lindex \$argv \$count]
288+
send "\$arg\r"
289+
incr count
290+
} else {
291+
send "exit\r"
292+
set output "Disconnected."
293+
if {[info exists rc] != 1} {
294+
set rc $OK
295+
}
296+
}
297+
exp_continue
298+
}
299+
300+
timeout {
301+
set output "Connection timeout."
302+
set rc $E_CONNECT
303+
}
304+
}
305+
306+
if {[info exists output] == 1} {
307+
puts "\$output"
308+
}
309+
310+
exit \$rc
311+
EOF
312+
313+
fi
254314
}
255315

256316
# SFTP backup download function

0 commit comments

Comments
 (0)