blob: aad7fcac2325b0acc4503ea3a5d2e6a8ffe956ff [file] [log] [blame]
Damien Millerd1e26cf2013-07-25 12:11:18 +10001# $OpenBSD: sftp-cmds.sh,v 1.14 2013/06/21 02:26:26 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 Tucker933e4f12003-09-04 14:26:31 +100010# test that these files are readable!
11for i in `(cd /bin;echo l*)`
12do
13 if [ -r $i ]; then
14 GLOBFILES="$GLOBFILES $i"
15 fi
16done
17
Darren Tuckercca1bdb2003-07-19 20:11:54 +100018# Path with embedded quote
19QUOTECOPY=${COPY}".\"blah\""
20QUOTECOPY_ARG=${COPY}'.\"blah\"'
Damien Millerce0e60e2007-10-26 14:54:12 +100021# File with spaces
22SPACECOPY="${COPY} this has spaces.txt"
23SPACECOPY_ARG="${COPY}\ this\ has\ spaces.txt"
24# File with glob metacharacters
25GLOBMETACOPY="${COPY} [metachar].txt"
Darren Tuckercca1bdb2003-07-19 20:11:54 +100026
Darren Tuckerff32d7c2012-06-30 15:04:13 +100027rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2
Darren Tuckerc43362c2003-06-18 22:41:41 +100028mkdir ${COPY}.dd
Damien Miller8b9cde72003-01-22 17:53:16 +110029
30verbose "$tid: lls"
Darren Tuckered6b0c52009-10-07 10:43:57 +110031(echo "lcd ${OBJ}" ; echo "lls") | ${SFTP} -D ${SFTPSERVER} 2>&1 | \
Tim Ricedce65cf2008-03-13 18:21:06 -070032 grep copy.dd >/dev/null 2>&1 || fail "lls failed"
Damien Miller4cd5fba2008-03-12 23:59:43 +110033
34verbose "$tid: lls w/path"
Darren Tuckered6b0c52009-10-07 10:43:57 +110035echo "lls ${OBJ}" | ${SFTP} -D ${SFTPSERVER} 2>&1 | \
Tim Ricedce65cf2008-03-13 18:21:06 -070036 grep copy.dd >/dev/null 2>&1 || fail "lls w/path failed"
Damien Miller8b9cde72003-01-22 17:53:16 +110037
38verbose "$tid: ls"
Darren Tuckered6b0c52009-10-07 10:43:57 +110039echo "ls ${OBJ}" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110040 || fail "ls failed"
41# XXX always successful
42
43verbose "$tid: shell"
Darren Tuckered6b0c52009-10-07 10:43:57 +110044echo "!echo hi there" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110045 || fail "shell failed"
46# XXX always successful
47
48verbose "$tid: pwd"
Darren Tuckered6b0c52009-10-07 10:43:57 +110049echo "pwd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110050 || fail "pwd failed"
51# XXX always successful
52
53verbose "$tid: lpwd"
Darren Tuckered6b0c52009-10-07 10:43:57 +110054echo "lpwd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110055 || fail "lpwd failed"
56# XXX always successful
57
58verbose "$tid: quit"
Darren Tuckered6b0c52009-10-07 10:43:57 +110059echo "quit" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110060 || fail "quit failed"
61# XXX always successful
62
63verbose "$tid: help"
Darren Tuckered6b0c52009-10-07 10:43:57 +110064echo "help" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110065 || fail "help failed"
66# XXX always successful
67
68rm -f ${COPY}
69verbose "$tid: get"
Darren Tuckered6b0c52009-10-07 10:43:57 +110070echo "get $DATA $COPY" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +110071 || fail "get failed"
72cmp $DATA ${COPY} || fail "corrupted copy after get"
73
Darren Tucker0240ff72003-10-08 17:52:10 +100074rm -f ${COPY}
75verbose "$tid: get quoted"
Darren Tuckered6b0c52009-10-07 10:43:57 +110076echo "get \"$DATA\" $COPY" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tucker0240ff72003-10-08 17:52:10 +100077 || fail "get failed"
78cmp $DATA ${COPY} || fail "corrupted copy after get"
79
Darren Tuckerc99a19b2004-04-16 17:58:28 +100080if [ "$os" != "cygwin" ]; then
Darren Tucker0240ff72003-10-08 17:52:10 +100081rm -f ${QUOTECOPY}
82cp $DATA ${QUOTECOPY}
83verbose "$tid: get filename with quotes"
Darren Tuckered6b0c52009-10-07 10:43:57 +110084echo "get \"$QUOTECOPY_ARG\" ${COPY}" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
85 || fail "get failed"
Darren Tucker0240ff72003-10-08 17:52:10 +100086cmp ${COPY} ${QUOTECOPY} || fail "corrupted copy after get with quotes"
87rm -f ${QUOTECOPY} ${COPY}
Darren Tuckerc99a19b2004-04-16 17:58:28 +100088fi
Darren Tucker0240ff72003-10-08 17:52:10 +100089
Damien Millerce0e60e2007-10-26 14:54:12 +100090rm -f "$SPACECOPY" ${COPY}
91cp $DATA "$SPACECOPY"
92verbose "$tid: get filename with spaces"
Darren Tuckered6b0c52009-10-07 10:43:57 +110093echo "get ${SPACECOPY_ARG} ${COPY}" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Millerce0e60e2007-10-26 14:54:12 +100094 || fail "get failed"
95cmp ${COPY} "$SPACECOPY" || fail "corrupted copy after get with spaces"
96
97rm -f "$GLOBMETACOPY" ${COPY}
98cp $DATA "$GLOBMETACOPY"
99verbose "$tid: get filename with glob metacharacters"
100echo "get \"${GLOBMETACOPY}\" ${COPY}" | \
Darren Tuckered6b0c52009-10-07 10:43:57 +1100101 ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || fail "get failed"
Damien Millerce0e60e2007-10-26 14:54:12 +1000102cmp ${COPY} "$GLOBMETACOPY" || \
103 fail "corrupted copy after get with glob metacharacters"
104
Darren Tuckerc43362c2003-06-18 22:41:41 +1000105rm -f ${COPY}.dd/*
106verbose "$tid: get to directory"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100107echo "get $DATA ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000108 || fail "get failed"
Damien Millerd1e26cf2013-07-25 12:11:18 +1000109cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after get"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000110
111rm -f ${COPY}.dd/*
112verbose "$tid: glob get to directory"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100113echo "get /bin/l* ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000114 || fail "get failed"
115for x in $GLOBFILES; do
116 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get"
117done
118
119rm -f ${COPY}.dd/*
120verbose "$tid: get to local dir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100121(echo "lcd ${COPY}.dd"; echo "get $DATA" ) | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000122 || fail "get failed"
Damien Millerd1e26cf2013-07-25 12:11:18 +1000123cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after get"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000124
125rm -f ${COPY}.dd/*
126verbose "$tid: glob get to local dir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100127(echo "lcd ${COPY}.dd"; echo "get /bin/l*") | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000128 || fail "get failed"
129for x in $GLOBFILES; do
130 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get"
131done
132
Damien Miller8b9cde72003-01-22 17:53:16 +1100133rm -f ${COPY}
134verbose "$tid: put"
Damien Millerce0e60e2007-10-26 14:54:12 +1000135echo "put $DATA $COPY" | \
Darren Tuckered6b0c52009-10-07 10:43:57 +1100136 ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed"
Damien Miller8b9cde72003-01-22 17:53:16 +1100137cmp $DATA ${COPY} || fail "corrupted copy after put"
138
Darren Tuckere7f75802003-09-07 09:32:58 +1000139if [ "$os" != "cygwin" ]; then
Darren Tuckercca1bdb2003-07-19 20:11:54 +1000140rm -f ${QUOTECOPY}
141verbose "$tid: put filename with quotes"
Damien Millerce0e60e2007-10-26 14:54:12 +1000142echo "put $DATA \"$QUOTECOPY_ARG\"" | \
Darren Tuckered6b0c52009-10-07 10:43:57 +1100143 ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed"
Darren Tuckercca1bdb2003-07-19 20:11:54 +1000144cmp $DATA ${QUOTECOPY} || fail "corrupted copy after put with quotes"
Darren Tuckere7f75802003-09-07 09:32:58 +1000145fi
Darren Tuckercca1bdb2003-07-19 20:11:54 +1000146
Damien Millerce0e60e2007-10-26 14:54:12 +1000147rm -f "$SPACECOPY"
148verbose "$tid: put filename with spaces"
149echo "put $DATA ${SPACECOPY_ARG}" | \
Darren Tuckered6b0c52009-10-07 10:43:57 +1100150 ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || fail "put failed"
Damien Millerce0e60e2007-10-26 14:54:12 +1000151cmp $DATA "$SPACECOPY" || fail "corrupted copy after put with spaces"
152
Darren Tuckerc43362c2003-06-18 22:41:41 +1000153rm -f ${COPY}.dd/*
154verbose "$tid: put to directory"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100155echo "put $DATA ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000156 || fail "put failed"
Damien Millerd1e26cf2013-07-25 12:11:18 +1000157cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after put"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000158
159rm -f ${COPY}.dd/*
160verbose "$tid: glob put to directory"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100161echo "put /bin/l? ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000162 || fail "put failed"
163for x in $GLOBFILES; do
164 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put"
165done
166
167rm -f ${COPY}.dd/*
168verbose "$tid: put to local dir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100169(echo "cd ${COPY}.dd"; echo "put $DATA") | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000170 || fail "put failed"
Damien Millerd1e26cf2013-07-25 12:11:18 +1000171cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after put"
Darren Tuckerc43362c2003-06-18 22:41:41 +1000172
173rm -f ${COPY}.dd/*
174verbose "$tid: glob put to local dir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100175(echo "cd ${COPY}.dd"; echo "put /bin/l?") | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Darren Tuckerc43362c2003-06-18 22:41:41 +1000176 || fail "put failed"
177for x in $GLOBFILES; do
178 cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put"
179done
180
Damien Miller8b9cde72003-01-22 17:53:16 +1100181verbose "$tid: rename"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100182echo "rename $COPY ${COPY}.1" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +1100183 || fail "rename failed"
184test -f ${COPY}.1 || fail "missing file after rename"
185cmp $DATA ${COPY}.1 >/dev/null 2>&1 || fail "corrupted copy after rename"
186
Darren Tuckera4040f52003-06-18 22:35:11 +1000187verbose "$tid: rename directory"
Damien Millerce0e60e2007-10-26 14:54:12 +1000188echo "rename ${COPY}.dd ${COPY}.dd2" | \
Darren Tuckered6b0c52009-10-07 10:43:57 +1100189 ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || \
Damien Millerce0e60e2007-10-26 14:54:12 +1000190 fail "rename directory failed"
Darren Tuckera4040f52003-06-18 22:35:11 +1000191test -d ${COPY}.dd && fail "oldname exists after rename directory"
192test -d ${COPY}.dd2 || fail "missing newname after rename directory"
193
Damien Miller8b9cde72003-01-22 17:53:16 +1100194verbose "$tid: ln"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100195echo "ln ${COPY}.1 ${COPY}.2" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || fail "ln failed"
Darren Tucker4288c532010-12-05 09:45:50 +1100196test -f ${COPY}.2 || fail "missing file after ln"
197cmp ${COPY}.1 ${COPY}.2 || fail "created file is not equal after ln"
198
199verbose "$tid: ln -s"
200rm -f ${COPY}.2
201echo "ln -s ${COPY}.1 ${COPY}.2" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 || fail "ln -s failed"
202test -h ${COPY}.2 || fail "missing file after ln -s"
Damien Miller8b9cde72003-01-22 17:53:16 +1100203
204verbose "$tid: mkdir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100205echo "mkdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +1100206 || fail "mkdir failed"
207test -d ${COPY}.dd || fail "missing directory after mkdir"
208
209# XXX do more here
210verbose "$tid: chdir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100211echo "chdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +1100212 || fail "chdir failed"
213
214verbose "$tid: rmdir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100215echo "rmdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +1100216 || fail "rmdir failed"
217test -d ${COPY}.1 && fail "present directory after rmdir"
218
219verbose "$tid: lmkdir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100220echo "lmkdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +1100221 || fail "lmkdir failed"
222test -d ${COPY}.dd || fail "missing directory after lmkdir"
223
224# XXX do more here
225verbose "$tid: lchdir"
Darren Tuckered6b0c52009-10-07 10:43:57 +1100226echo "lchdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} >/dev/null 2>&1 \
Damien Miller8b9cde72003-01-22 17:53:16 +1100227 || fail "lchdir failed"
228
Darren Tuckerff32d7c2012-06-30 15:04:13 +1000229rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2
Damien Millerce0e60e2007-10-26 14:54:12 +1000230rm -rf ${QUOTECOPY} "$SPACECOPY" "$GLOBMETACOPY"
Damien Miller8b9cde72003-01-22 17:53:16 +1100231
Darren Tuckered6b0c52009-10-07 10:43:57 +1100232