forked from tobijahu/one-time-access
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathota-ssh-client.sh
More file actions
56 lines (46 loc) · 1.78 KB
/
ota-ssh-client.sh
File metadata and controls
56 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh
## The configuration file should be situated at the same dir as
## this script.
PATH_TO_CONFIGURATION_FILE=$(dirname $(readlink -f $0))/ota-ssh-client.conf
[ ! -e "$PATH_TO_CONFIGURATION_FILE" ] \
&& echo "Error: Configuration file does not exist: $PATH_TO_CONFIGURATION_FILE" \
&& exit 1
. $PATH_TO_CONFIGURATION_FILE
# Check the configuration on this client
for constant in "$SSH_PRIVATE_ID" "$SSH_REMOTE_HOST" "$PATH_TO_FILE_DIR_ON_SERVER" "$PRINT_LINK_SCRIPT" "$LOCK_SCRIPT" "$OTAUSER"
do
[ -z "$constant" ] \
&& echo "Error: A constant at the configuration is empty." \
&& exit 1
done
if [ $# -ne 1 ]
then
echo "Usage: $(basename $0) <file-name>"
exit 1
fi
## Validate the path to the file that is selected for upload.
[ ! -e "$1" ] \
&& echo "Error: $1 does not exist." \
&& exit 1
[ ! -f "$1" ] \
&& echo "Error: $1 is not a file." \
&& exit 1
## Get sha512 checksum of file
sha512SumOfFile=$(sha512sum "$1" | awk -F ' ' '{print $1}')
## Open a dash shell on this machine and advise the ssh-agent
## to cache the password of the used ssh-id $SSH_PRIVATE_ID for
## 40 seconds. Then run the script, which is defined in the called
## string.
ssh-agent -t 40 /bin/dash -c "ssh-add $SSH_PRIVATE_ID
# Create lock file
ssh $SSH_REMOTE_HOST su $OTAUSER -c \'/bin/dash $LOCK_SCRIPT create\'
# Copy the file to the server
echo 'Copying file to server...'
scp -Cp \"$1\" \"$SSH_REMOTE_HOST:$(dirname $PATH_TO_FILE_DIR_ON_SERVER)/$(basename $PATH_TO_FILE_DIR_ON_SERVER)\"
# Remove move-lock file
ssh $SSH_REMOTE_HOST /bin/su - $OTAUSER -c \'/bin/dash $LOCK_SCRIPT remove\'
# Obtain the link from the server
#ssh -t -t $SSH_REMOTE_HOST /bin/dash $PRINT_LINK_SCRIPT $sha512SumOfFile
ssh $SSH_REMOTE_HOST /bin/su - $OTAUSER -c \'/bin/dash $PRINT_LINK_SCRIPT $sha512SumOfFile\'
exit"
exit 0