blob: 3b453c5e80fc0545abde9b96f6ea7f16b713deff [file] [log] [blame]
Damien Miller4cd5fba2008-03-12 23:59:43 +11001# $OpenBSD: sftp-cmds.sh,v 1.9 2007/12/12 05:04:03 djm Exp $
Damien Miller8b9cde72003-01-22 17:53:16 +11002# Placed in the Public Domain.
3
4# XXX - TODO:
Damien Miller8b9cde72003-01-22 17:53:16 +11005# - chmod / chown / chgrp
6# - -p flag for get & put
7
8tid="sftp commands"
9
Darren Tuckerc2c42232003-09-04 14:54:39 +100010DATA=/bin/ls${EXEEXT}
Damien Miller8b9cde72003-01-22 17:53:16 +110011COPY=${OBJ}/copy
Darren Tucker933e4f12003-09-04 14:26:31 +100012# test that these files are readable!
13for i in `(cd /bin;echo l*)`
14do
15 if [ -r $i ]; then
16 GLOBFILES="$GLOBFILES $i"
17 fi
18done
19
Tim Rice23ee3592003-09-11 22:32:17 -070020if have_prog uname
Darren Tuckere7f75802003-09-07 09:32:58 +100021then
22 case `uname` in
23 CYGWIN*)
24 os=cygwin
25 ;;
26 *)
27 os=`uname`
28 ;;
29 esac
30else
31 os="unknown"
32fi
33
Darren Tuckercca1bdb2003-07-19 20:11:54 +100034# Path with embedded quote
35QUOTECOPY=${COPY}".\"blah\""
36QUOTECOPY_ARG=${COPY}'.\"blah\"'
Damien Millerce0e60e2007-10-26 14:54:12 +100037# File with spaces
38SPACECOPY="${COPY} this has spaces.txt"
39SPACECOPY_ARG="${COPY}\ this\ has\ spaces.txt"
40# File with glob metacharacters
41GLOBMETACOPY="${COPY} [metachar].txt"
Darren Tuckercca1bdb2003-07-19 20:11:54 +100042
Tim Rice1eb40cf2003-09-09 08:35:20 -070043rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2
Darren Tuckerc43362c2003-06-18 22:41:41 +100044mkdir ${COPY}.dd
Damien Miller8b9cde72003-01-22 17:53:16 +110045
46verbose "$tid: lls"
Tim Rice51430432008-03-14 10:39:17 -070047(echo "lcd ${OBJ}" ; echo "lls") | ${SFTP} -P ${SFTPSERVER} 2>&1 | \
Tim Ricedce65cf2008-03-13 18:21:06 -070048 grep copy.dd >/dev/null 2>&1 || fail "lls failed"
Damien Miller4cd5fba2008-03-12 23:59:43 +110049
50verbose "$tid: lls w/path"
51echo "lls ${OBJ}" | ${SFTP} -P ${SFTPSERVER} 2>&1 | \
Tim Ricedce65cf2008-03-13 18:21:06 -070052 grep copy.dd >/dev/null 2>&1 || fail "lls w/path failed"
Damien Miller8b9cde72003-01-22 17:53:16 +110053
54verbose "$tid: ls"
55echo "ls ${OBJ}" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
56 || fail "ls failed"
57# XXX always successful
58
59verbose "$tid: shell"
60echo "!echo hi there" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
61 || fail "shell failed"
62# XXX always successful
63
64verbose "$tid: pwd"
65echo "pwd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
66 || fail "pwd failed"
67# XXX always successful
68
69verbose "$tid: lpwd"
70echo "lpwd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
71 || fail "lpwd failed"
72# XXX always successful
73
74verbose "$tid: quit"
75echo "quit" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
76 || fail "quit failed"
77# XXX always successful
78
79verbose "$tid: help"
80echo "help" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
81 || fail "help failed"
82# XXX always successful
83
84rm -f ${COPY}
85verbose "$tid: get"
86echo "get $DATA $COPY" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
87 || fail "get failed"
88cmp $DATA ${COPY} || fail "corrupted copy after get"
89
Darren Tucker0240ff72003-10-08 17:52:10 +100090rm -f ${COPY}
91verbose "$tid: get quoted"
92echo "get \"$DATA\" $COPY" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
93 || fail "get failed"
94cmp $DATA ${COPY} || fail "corrupted copy after get"
95
Darren Tuckerc99a19b2004-04-16 17:58:28 +100096if [ "$os" != "cygwin" ]; then
Darren Tucker0240ff72003-10-08 17:52:10 +100097rm -f ${QUOTECOPY}
98cp $DATA ${QUOTECOPY}
99verbose "$tid: get filename with quotes"
Damien Millerce0e60e2007-10-26 14:54:12 +1000100echo "get \"$QUOTECOPY_ARG\" ${COPY}" | \
101 ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \ || fail "get failed"
Darren Tucker0240ff72003-10-08 17:52:10 +1000102cmp ${COPY} ${QUOTECOPY} || fail "corrupted copy after get with quotes"
103rm -f ${QUOTECOPY} ${COPY}
Darren Tuckerc99a19b2004-04-16 17:58:28 +1000104fi
Darren Tucker0240ff72003-10-08 17:52:10 +1000105
Damien Millerce0e60e2007-10-26 14:54:12 +1000106rm -f "$SPACECOPY" ${COPY}
107cp $DATA "$SPACECOPY"
108verbose "$tid: get filename with spaces"
109echo "get ${SPACECOPY_ARG} ${COPY}" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
110 || fail "get failed"
111cmp ${COPY} "$SPACECOPY" || fail "corrupted copy after get with spaces"
112
113rm -f "$GLOBMETACOPY" ${COPY}
114cp $DATA "$GLOBMETACOPY"
115verbose "$tid: get filename with glob metacharacters"
116echo "get \"${GLOBMETACOPY}\" ${COPY}" | \
117 ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "get failed"
118cmp ${COPY} "$GLOBMETACOPY" || \
119 fail "corrupted copy after get with glob metacharacters"
120
Darren Tuckerc43362c2003-06-18 22:41:41 +1000121rm -f ${COPY}.dd/*
122verbose "$tid: get to directory"
123echo "get $DATA ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
124 || fail "get failed"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000125cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after get"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000126
127rm -f ${COPY}.dd/*
128verbose "$tid: glob get to directory"
129echo "get /bin/l* ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
130 || fail "get failed"
131for x in $GLOBFILES; do
132 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get"
133done
134
135rm -f ${COPY}.dd/*
136verbose "$tid: get to local dir"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000137(echo "lcd ${COPY}.dd"; echo "get $DATA" ) | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000138 || fail "get failed"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000139cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after get"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000140
141rm -f ${COPY}.dd/*
142verbose "$tid: glob get to local dir"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000143(echo "lcd ${COPY}.dd"; echo "get /bin/l*") | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000144 || fail "get failed"
145for x in $GLOBFILES; do
146 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get"
147done
148
Damien Miller8b9cde72003-01-22 17:53:16 +1100149rm -f ${COPY}
150verbose "$tid: put"
Damien Millerce0e60e2007-10-26 14:54:12 +1000151echo "put $DATA $COPY" | \
152 ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed"
Damien Miller8b9cde72003-01-22 17:53:16 +1100153cmp $DATA ${COPY} || fail "corrupted copy after put"
154
Darren Tuckere7f75802003-09-07 09:32:58 +1000155if [ "$os" != "cygwin" ]; then
Darren Tuckercca1bdb2003-07-19 20:11:54 +1000156rm -f ${QUOTECOPY}
157verbose "$tid: put filename with quotes"
Damien Millerce0e60e2007-10-26 14:54:12 +1000158echo "put $DATA \"$QUOTECOPY_ARG\"" | \
159 ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed"
Darren Tuckercca1bdb2003-07-19 20:11:54 +1000160cmp $DATA ${QUOTECOPY} || fail "corrupted copy after put with quotes"
Darren Tuckere7f75802003-09-07 09:32:58 +1000161fi
Darren Tuckercca1bdb2003-07-19 20:11:54 +1000162
Damien Millerce0e60e2007-10-26 14:54:12 +1000163rm -f "$SPACECOPY"
164verbose "$tid: put filename with spaces"
165echo "put $DATA ${SPACECOPY_ARG}" | \
166 ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed"
167cmp $DATA "$SPACECOPY" || fail "corrupted copy after put with spaces"
168
Darren Tuckerc43362c2003-06-18 22:41:41 +1000169rm -f ${COPY}.dd/*
170verbose "$tid: put to directory"
171echo "put $DATA ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
172 || fail "put failed"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000173cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after put"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000174
175rm -f ${COPY}.dd/*
176verbose "$tid: glob put to directory"
Damien Miller89437ed2007-10-26 15:37:50 +1000177echo "put /bin/l? ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000178 || fail "put failed"
179for x in $GLOBFILES; do
180 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put"
181done
182
183rm -f ${COPY}.dd/*
184verbose "$tid: put to local dir"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000185(echo "cd ${COPY}.dd"; echo "put $DATA") | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000186 || fail "put failed"
Darren Tuckerc2c42232003-09-04 14:54:39 +1000187cmp $DATA ${COPY}.dd/`basename $DATA` || fail "corrupted copy after put"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000188
189rm -f ${COPY}.dd/*
190verbose "$tid: glob put to local dir"
Damien Miller89437ed2007-10-26 15:37:50 +1000191(echo "cd ${COPY}.dd"; echo "put /bin/l?") | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000192 || fail "put failed"
193for x in $GLOBFILES; do
194 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put"
195done
196
Damien Miller8b9cde72003-01-22 17:53:16 +1100197verbose "$tid: rename"
198echo "rename $COPY ${COPY}.1" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
199 || fail "rename failed"
200test -f ${COPY}.1 || fail "missing file after rename"
201cmp $DATA ${COPY}.1 >/dev/null 2>&1 || fail "corrupted copy after rename"
202
Darren Tuckera4040f52003-06-18 22:35:11 +1000203verbose "$tid: rename directory"
Damien Millerce0e60e2007-10-26 14:54:12 +1000204echo "rename ${COPY}.dd ${COPY}.dd2" | \
205 ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || \
206 fail "rename directory failed"
Darren Tuckera4040f52003-06-18 22:35:11 +1000207test -d ${COPY}.dd && fail "oldname exists after rename directory"
208test -d ${COPY}.dd2 || fail "missing newname after rename directory"
209
Damien Miller8b9cde72003-01-22 17:53:16 +1100210verbose "$tid: ln"
211echo "ln ${COPY}.1 ${COPY}.2" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 || fail "ln failed"
Darren Tucker47351152003-09-04 15:24:50 +1000212test -h ${COPY}.2 || fail "missing file after ln"
Damien Miller8b9cde72003-01-22 17:53:16 +1100213
214verbose "$tid: mkdir"
215echo "mkdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
216 || fail "mkdir failed"
217test -d ${COPY}.dd || fail "missing directory after mkdir"
218
219# XXX do more here
220verbose "$tid: chdir"
221echo "chdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
222 || fail "chdir failed"
223
224verbose "$tid: rmdir"
225echo "rmdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
226 || fail "rmdir failed"
227test -d ${COPY}.1 && fail "present directory after rmdir"
228
229verbose "$tid: lmkdir"
230echo "lmkdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
231 || fail "lmkdir failed"
232test -d ${COPY}.dd || fail "missing directory after lmkdir"
233
234# XXX do more here
235verbose "$tid: lchdir"
236echo "lchdir ${COPY}.dd" | ${SFTP} -P ${SFTPSERVER} >/dev/null 2>&1 \
237 || fail "lchdir failed"
238
Tim Rice1eb40cf2003-09-09 08:35:20 -0700239rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2
Damien Millerce0e60e2007-10-26 14:54:12 +1000240rm -rf ${QUOTECOPY} "$SPACECOPY" "$GLOBMETACOPY"
Damien Miller8b9cde72003-01-22 17:53:16 +1100241