sftp-cmds.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # $OpenBSD: sftp-cmds.sh,v 1.14 2013/06/21 02:26:26 djm Exp $
  2. # Placed in the Public Domain.
  3. # XXX - TODO:
  4. # - chmod / chown / chgrp
  5. # - -p flag for get & put
  6. tid="sftp commands"
  7. # test that these files are readable!
  8. for i in $(
  9. cd /bin
  10. echo l*
  11. ); do
  12. if [ -r $i ]; then
  13. GLOBFILES="$GLOBFILES $i"
  14. fi
  15. done
  16. # Path with embedded quote
  17. QUOTECOPY=${COPY}".\"blah\""
  18. QUOTECOPY_ARG=${COPY}'.\"blah\"'
  19. # File with spaces
  20. SPACECOPY="${COPY} this has spaces.txt"
  21. SPACECOPY_ARG="${COPY}\ this\ has\ spaces.txt"
  22. # File with glob metacharacters
  23. GLOBMETACOPY="${COPY} [metachar].txt"
  24. rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2
  25. mkdir ${COPY}.dd
  26. verbose "$tid: lls"
  27. (
  28. echo "lcd ${OBJ}"
  29. echo "lls"
  30. ) | ${SFTP} -D ${SFTPSERVER} 2>&1 |
  31. grep copy.dd > /dev/null 2>&1 || fail "lls failed"
  32. verbose "$tid: lls w/path"
  33. echo "lls ${OBJ}" | ${SFTP} -D ${SFTPSERVER} 2>&1 |
  34. grep copy.dd > /dev/null 2>&1 || fail "lls w/path failed"
  35. verbose "$tid: ls"
  36. echo "ls ${OBJ}" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  37. fail "ls failed"
  38. # XXX always successful
  39. verbose "$tid: shell"
  40. echo "!echo hi there" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  41. fail "shell failed"
  42. # XXX always successful
  43. verbose "$tid: pwd"
  44. echo "pwd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  45. fail "pwd failed"
  46. # XXX always successful
  47. verbose "$tid: lpwd"
  48. echo "lpwd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  49. fail "lpwd failed"
  50. # XXX always successful
  51. verbose "$tid: quit"
  52. echo "quit" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  53. fail "quit failed"
  54. # XXX always successful
  55. verbose "$tid: help"
  56. echo "help" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  57. fail "help failed"
  58. # XXX always successful
  59. rm -f ${COPY}
  60. verbose "$tid: get"
  61. echo "get $DATA $COPY" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  62. fail "get failed"
  63. cmp $DATA ${COPY} || fail "corrupted copy after get"
  64. rm -f ${COPY}
  65. verbose "$tid: get quoted"
  66. echo "get \"$DATA\" $COPY" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  67. fail "get failed"
  68. cmp $DATA ${COPY} || fail "corrupted copy after get"
  69. rm -f ${QUOTECOPY}
  70. cp $DATA ${QUOTECOPY}
  71. verbose "$tid: get filename with quotes"
  72. echo "get \"$QUOTECOPY_ARG\" ${COPY}" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  73. fail "get failed"
  74. cmp ${COPY} ${QUOTECOPY} || fail "corrupted copy after get with quotes"
  75. rm -f ${QUOTECOPY} ${COPY}
  76. rm -f "$SPACECOPY" ${COPY}
  77. cp $DATA "$SPACECOPY"
  78. verbose "$tid: get filename with spaces"
  79. echo "get ${SPACECOPY_ARG} ${COPY}" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  80. fail "get failed"
  81. cmp ${COPY} "$SPACECOPY" || fail "corrupted copy after get with spaces"
  82. rm -f "$GLOBMETACOPY" ${COPY}
  83. cp $DATA "$GLOBMETACOPY"
  84. verbose "$tid: get filename with glob metacharacters"
  85. echo "get \"${GLOBMETACOPY}\" ${COPY}" |
  86. ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 || fail "get failed"
  87. cmp ${COPY} "$GLOBMETACOPY" ||
  88. fail "corrupted copy after get with glob metacharacters"
  89. rm -f ${COPY}.dd/*
  90. verbose "$tid: get to directory"
  91. echo "get $DATA ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  92. fail "get failed"
  93. cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after get"
  94. rm -f ${COPY}.dd/*
  95. verbose "$tid: glob get to directory"
  96. echo "get /bin/l* ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  97. fail "get failed"
  98. for x in $GLOBFILES; do
  99. cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get"
  100. done
  101. rm -f ${COPY}.dd/*
  102. verbose "$tid: get to local dir"
  103. (
  104. echo "lcd ${COPY}.dd"
  105. echo "get $DATA"
  106. ) | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  107. fail "get failed"
  108. cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after get"
  109. rm -f ${COPY}.dd/*
  110. verbose "$tid: glob get to local dir"
  111. (
  112. echo "lcd ${COPY}.dd"
  113. echo "get /bin/l*"
  114. ) | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  115. fail "get failed"
  116. for x in $GLOBFILES; do
  117. cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after get"
  118. done
  119. rm -f ${COPY}
  120. verbose "$tid: put"
  121. echo "put $DATA $COPY" |
  122. ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 || fail "put failed"
  123. cmp $DATA ${COPY} || fail "corrupted copy after put"
  124. rm -f ${QUOTECOPY}
  125. verbose "$tid: put filename with quotes"
  126. echo "put $DATA \"$QUOTECOPY_ARG\"" |
  127. ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 || fail "put failed"
  128. cmp $DATA ${QUOTECOPY} || fail "corrupted copy after put with quotes"
  129. rm -f "$SPACECOPY"
  130. verbose "$tid: put filename with spaces"
  131. echo "put $DATA ${SPACECOPY_ARG}" |
  132. ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 || fail "put failed"
  133. cmp $DATA "$SPACECOPY" || fail "corrupted copy after put with spaces"
  134. rm -f ${COPY}.dd/*
  135. verbose "$tid: put to directory"
  136. echo "put $DATA ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  137. fail "put failed"
  138. cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after put"
  139. rm -f ${COPY}.dd/*
  140. verbose "$tid: glob put to directory"
  141. echo "put /bin/l? ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  142. fail "put failed"
  143. for x in $GLOBFILES; do
  144. cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put"
  145. done
  146. rm -f ${COPY}.dd/*
  147. verbose "$tid: put to local dir"
  148. (
  149. echo "cd ${COPY}.dd"
  150. echo "put $DATA"
  151. ) | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  152. fail "put failed"
  153. cmp $DATA ${COPY}.dd/$DATANAME || fail "corrupted copy after put"
  154. rm -f ${COPY}.dd/*
  155. verbose "$tid: glob put to local dir"
  156. (
  157. echo "cd ${COPY}.dd"
  158. echo "put /bin/l?"
  159. ) | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  160. fail "put failed"
  161. for x in $GLOBFILES; do
  162. cmp /bin/$x ${COPY}.dd/$x || fail "corrupted copy after put"
  163. done
  164. verbose "$tid: rename"
  165. echo "rename $COPY ${COPY}.1" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  166. fail "rename failed"
  167. test -f ${COPY}.1 || fail "missing file after rename"
  168. cmp $DATA ${COPY}.1 > /dev/null 2>&1 || fail "corrupted copy after rename"
  169. verbose "$tid: rename directory"
  170. echo "rename ${COPY}.dd ${COPY}.dd2" |
  171. ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  172. fail "rename directory failed"
  173. test -d ${COPY}.dd && fail "oldname exists after rename directory"
  174. test -d ${COPY}.dd2 || fail "missing newname after rename directory"
  175. verbose "$tid: ln"
  176. echo "ln ${COPY}.1 ${COPY}.2" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 || fail "ln failed"
  177. test -f ${COPY}.2 || fail "missing file after ln"
  178. cmp ${COPY}.1 ${COPY}.2 || fail "created file is not equal after ln"
  179. verbose "$tid: ln -s"
  180. rm -f ${COPY}.2
  181. echo "ln -s ${COPY}.1 ${COPY}.2" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 || fail "ln -s failed"
  182. test -h ${COPY}.2 || fail "missing file after ln -s"
  183. verbose "$tid: mkdir"
  184. echo "mkdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  185. fail "mkdir failed"
  186. test -d ${COPY}.dd || fail "missing directory after mkdir"
  187. # XXX do more here
  188. verbose "$tid: chdir"
  189. echo "chdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  190. fail "chdir failed"
  191. verbose "$tid: rmdir"
  192. echo "rmdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  193. fail "rmdir failed"
  194. test -d ${COPY}.1 && fail "present directory after rmdir"
  195. verbose "$tid: lmkdir"
  196. echo "lmkdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  197. fail "lmkdir failed"
  198. test -d ${COPY}.dd || fail "missing directory after lmkdir"
  199. # XXX do more here
  200. verbose "$tid: lchdir"
  201. echo "lchdir ${COPY}.dd" | ${SFTP} -D ${SFTPSERVER} > /dev/null 2>&1 ||
  202. fail "lchdir failed"
  203. rm -rf ${COPY} ${COPY}.1 ${COPY}.2 ${COPY}.dd ${COPY}.dd2
  204. rm -rf ${QUOTECOPY} "$SPACECOPY" "$GLOBMETACOPY"