-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathezscp.sh
executable file
·160 lines (150 loc) · 4.82 KB
/
ezscp.sh
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash
#####################
# configuration #
# loader #
#####################
confFileLocation="/etc/ezscp/ezscp.conf"
sudo touch $confFileLocation
if [ $? -eq 0 ]
then
source $confFileLocation
else
echo "could not read configuration file, did you install from the script?"
exit 1
fi
#####################
# update args check #
# will skip if not #
# update. #
#####################
version="0.1.1"
if [[ $1 == "update" ]]
then
if [[ $version == $(curl -sSL https://raw.githubusercontent.com/sam-irl/ezscp/master/version) ]]
then
echo "ezscp up-to-date"
exit 0
else
read -p "Newer version available. Enter to update, CTRL-C to abort."
curl -sSL https://raw.githubusercontent.com/sam-irl/ezscp/master/INSTALL.sh | sudo bash
if [ $? -eq 0 ]
then
exit 0
else
echo "Update failed."
exit 1
fi
fi
fi
# ezscp
echo "Hello, "$USER", and welcome to ez-scp."
echo "This provides a frontend to the scp command, more information can be found at its manual page."
echo "Please enter a hostname that your file to copy is on."
echo "Leave this blank if it is on your local machine."
echo "Hostname: "
read fileHostname
if [[ ! -z "${fileHostname// }" ]]
then
echo "Please enter a user to copy the file from, on "$fileHostname": "
read fileUser
echo "Please enter a port to use for SSH. If blank, it defaults to 22: "
read fileSshPort
if [[ -z "${fileSshPort// }" ]]
then
fileSshPort="22"
fi
echo "Will connect to "$fileUser"@"$fileHostname", SSH via port "$fileSshPort""
echo "Please CTRL-C and restart if this is incorrect."
fi
echo "Please enter a file to copy without hostnames: "
read fileToCopy
if [[ -z "$fileHostname" ]]
then
touch $fileToCopy
if [ $? -eq 0 ]
then
echo "Excellent, I found it."
echo "We will copy file "$fileToCopy"."
else
echo "Sorry, I couldn't find the file to copy. Make sure you have permission to access it."
exit 1
fi
else
echo "As the file is on a remote host, I can't verify it exists. If scp fails, this might be why."
echo "We will attempt to copy file "$fileUser"@"$fileHostname":"$fileToCopy" using SSH port "$fileSshPort"."
fi
echo "Okay, now we need to copy the file somewhere."
echo "Please enter a hostname of the machine to copy TO."
echo "Leave blank for your local machine."
echo "Hostname: "
read destHostname
if [[ ! -z "${destHostname// }" ]]
then
echo "Please enter a user to copy the file to, on "$destHostname": "
read destUser
echo "Please enter a port to use for SSH. Defaults to 22: "
read destSshPort
if [[ -z "${destSshPort// }" ]]
then
destSshPort="22"
fi
if [[ ! $fileSshPort == $destSshPort ]] && [[ ! $fileSshPort == "22" ]] && [[ ! $destSshPort == "22" ]]
then
echo "Only one port for SSH can be used, excluding 22. This is a problem with scp itself. Exiting."
exit 1
fi
echo "Will copy to "$destUser"@"$destHostname", SSH via port "$destSshPort"."
echo "Please CTRL-C and start again if this is incorrect."
fi
echo "Now, please enter the file to copy to. Please include a file name. For example, /usr/bin/my-script, not /usr/bin."
echo "File destination: "
read destFile
if [[ ! -z "${fileHostname// }" ]]
then
if [[ ! -z "${destHostname// }" ]]
then
echo "Will attempt to copy from "$fileUser"@"$fileHostname":"$fileToCopy" SSH port "$fileSshPort" to "$destUser"@"$destHostname":"$destFile" SSH port "$destSshPort""
else
echo "Will attempt to copy from "$fileUser"@"$fileHostname":"$fileToCopy" SSH port "$fileSshPort" to "$destFile""
fi
else
if [[ ! -z "${destHostname// }" ]]
then
echo "Will attempt to copy from "$fileToCopy" to "$destUser"@"$destHostname":"$destFile" SSH port "$destSshPort""
else
echo "Will attempt to copy from "$fileToCopy" to "$destFile""
fi
fi
read -p "Please verify this information is correct. Enter to continue, CTRL-C to abort."
if [[ ! -z "${fileHostname// }" ]]
then
if [[ ! -z "${destHostname// }" ]]
then
if [[ ! $fileSshPort == "22" ]]
then
echo "scp -P "$fileSshPort" "$fileUser"@"$fileHostname":"$fileToCopy" "$destUser"@"$destHostname":"$destFile"" | bash
else
echo "scp -P "$destSshPort" "$fileUser"@"$fileHostname":"$fileToCopy" "$destUser"@"$destHostname":"$destFile"" | bash
fi
echo "scp "$fileUser"@"$fileHostname":"$fileToCopy" "$destUser"@"$destHostname":"$destFile"" | bash
else
echo "scp -P "$fileSshPort" "$fileUser"@"$fileHostname":"$fileToCopy" "$destFile"" | bash
fi
else
if [[ ! -z "${destHostname// }" ]]
then
echo "scp -P "$destSshPort" "$fileToCopy" "$destUser"@"$destHostname":"$destFile"" | bash
else
echo "Fine."
echo "scp "$fileToCopy" "$destFile"" | bash
fi
fi
if [ $? -eq 0 ]
then
echo "Files copied successfully!"
exit 0
else
echo "There was an error with scp. Read man scp for help, or try again."
exit 2
fi
exit 0