home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / sftp / part01 < prev    next >
Encoding:
Text File  |  1993-07-02  |  12.6 KB  |  640 lines

  1. Newsgroups: comp.sources.unix
  2. From: mzraly@cs.umb.edu (Michael S Zraly)
  3. Subject: v26i279: sftp - command line ftp interface, Part01/01
  4. Sender: unix-sources-moderator@gw.home.vix.com
  5. Approved: vixie@gw.home.vix.com
  6.  
  7. Submitted-By: mzraly@cs.umb.edu (Michael S Zraly)
  8. Posting-Number: Volume 26, Issue 279
  9. Archive-Name: sftp/part01
  10.  
  11. Sftp is a shell script that allows a user to quickly perform the most
  12. common ftp activities -- listing files and retrieving files -- with a
  13. minimum of fuss and minimum time connected to busy FTP sites.
  14.  
  15.     mzraly@cs.umb.edu (Michael S Zraly)
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of archive 1 (of 1)."
  24. # Contents:  MANIFEST Makefile README sftp sftp.1
  25. # Wrapped by vixie@gw.home.vix.com on Sat Jul  3 13:06:41 1993
  26. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  27. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  28.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  29. else
  30. echo shar: Extracting \"'MANIFEST'\" \(269 characters\)
  31. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  32. X   File Name        Archive #    Description
  33. X-----------------------------------------------------------
  34. X MANIFEST                   1    This shipping list
  35. X Makefile                   1    
  36. X README                     1    
  37. X sftp                       1    
  38. X sftp.1                     1    
  39. END_OF_FILE
  40. if test 269 -ne `wc -c <'MANIFEST'`; then
  41.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  42. fi
  43. # end of 'MANIFEST'
  44. fi
  45. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  46.   echo shar: Will not clobber existing file \"'Makefile'\"
  47. else
  48. echo shar: Extracting \"'Makefile'\" \(594 characters\)
  49. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  50. X# Makefile
  51. X# Michael S Zraly
  52. X# mzraly@cs.umb.edu
  53. X# 26 July 1992
  54. X
  55. XPROG=    sftp
  56. X
  57. XBIN=    /usr/local/bin
  58. XMAN=    /usr/local/man
  59. X
  60. XFILES=    Makefile README $(PROG) $(PROG).1
  61. X
  62. Xall:    $(PROG).man $(PROG).1 $(PROG)
  63. X
  64. Xinstall:    all
  65. X    cp $(PROG) $(BIN) && chmod 755 $(BIN)/$(PROG)
  66. X    cp $(PROG).1 $(MAN)/man1/$(PROG).1 && chmod 644 $(MAN)/man1/$(PROG).1
  67. X    cp $(PROG).man $(MAN)/cat1/$(PROG).1 && chmod 644 $(MAN)/cat1/$(PROG).1
  68. X
  69. Xclean:
  70. X    rm -f $(PROG).man $(PROG).tar.Z
  71. X
  72. X$(PROG).man:    $(PROG).1
  73. X    nroff -man $(PROG).1 > $(PROG).man
  74. X
  75. Xdist:    $(PROG).tar.Z
  76. X
  77. X$(PROG).tar.Z:    $(FILES)
  78. X    tar cvf - $(FILES) | compress > $(PROG).tar.Z
  79. END_OF_FILE
  80. if test 594 -ne `wc -c <'Makefile'`; then
  81.     echo shar: \"'Makefile'\" unpacked with wrong size!
  82. fi
  83. # end of 'Makefile'
  84. fi
  85. if test -f 'README' -a "${1}" != "-c" ; then 
  86.   echo shar: Will not clobber existing file \"'README'\"
  87. else
  88. echo shar: Extracting \"'README'\" \(959 characters\)
  89. sed "s/^X//" >'README' <<'END_OF_FILE'
  90. XREADME
  91. XMichael S Zraly
  92. Xmzraly@cs.umb.edu
  93. X26 July 1992
  94. X
  95. X
  96. XDESCRIPTION
  97. X
  98. XSftp is a shell script that allows a user to quickly perform the most
  99. Xcommon ftp activities -- listing files and retrieving files -- with a
  100. Xminimum of fuss and minimum time connected to busy FTP sites.
  101. X
  102. XREQUIREMENTS
  103. X
  104. XNo special permissions are required.  The only non-standard command
  105. Xthat sftp uses is getopts(1).  It is available by anonymous FTP
  106. Xfrom a number of sites; let archie(1) be your guide.  The source for
  107. Xgetopts is in perl.
  108. X
  109. XINSTALLATION
  110. X
  111. XType "make install".  The only "compiling" necessary is the formatting
  112. Xof the manual page; "make install" merely formats the manual page with
  113. Xnroff(1) and copies the manual pages and shell script into the
  114. Xappropriate directories.  You'll have to update the whatis(1) database
  115. Xby hand, though, since there are too many variations for me to worry
  116. Xabout.
  117. X
  118. XBUG REPORTS
  119. X
  120. XSend them (and fixes, if you have them) to me at:
  121. X
  122. X    mzraly@cs.umb.edu
  123. X
  124. END_OF_FILE
  125. if test 959 -ne `wc -c <'README'`; then
  126.     echo shar: \"'README'\" unpacked with wrong size!
  127. fi
  128. # end of 'README'
  129. fi
  130. if test -f 'sftp' -a "${1}" != "-c" ; then 
  131.   echo shar: Will not clobber existing file \"'sftp'\"
  132. else
  133. echo shar: Extracting \"'sftp'\" \(3169 characters\)
  134. sed "s/^X//" >'sftp' <<'END_OF_FILE'
  135. X#! /bin/sh
  136. X#
  137. X# sftp -- "surgical" FTP
  138. X#
  139. X# Michael S Zraly
  140. X# mzraly@cs.umb.edu
  141. X# 26 July 1992
  142. X#
  143. X# Based loosely on getfile, written by John Granrose
  144. X# for his anonymous FTP archive at pilot.njin.net.
  145. X
  146. Xcmd=
  147. Xdbg=false
  148. Xuse=false
  149. Xver=false
  150. Xbin=false
  151. Xhash=false
  152. Xmac=
  153. Xdir=
  154. Xfil=
  155. Xkey=
  156. Xsub=
  157. X
  158. Xusage='-(g|l|m) [-duvbh] (site dir | [-f file] -k key [-s subdir]) file[s]'
  159. X
  160. Xprog=`basename $0`
  161. X
  162. XUSAGE="usage: $prog $usage"
  163. X
  164. X#
  165. X#    Set initial value for fil
  166. X#
  167. X
  168. Xif test -n "$FTPKEYS"
  169. Xthen
  170. X    fil=$FTPKEYS
  171. Xelif test -r .ftpkeys
  172. Xthen
  173. X    fil=.ftpkeys
  174. Xelif test -r $HOME/.ftpkeys
  175. Xthen
  176. X    fil=$HOME/.ftpkeys
  177. Xfi
  178. X
  179. X#
  180. X#    Parse options
  181. X#
  182. X
  183. Xwhile getopts glmduvbhf:k:s: c
  184. Xdo
  185. X    case $c in
  186. X    g)    if test -n "$cmd"
  187. X        then
  188. X            echo option -g 1>&2
  189. X            echo $USAGE 1>&2
  190. X            exit 1
  191. X        fi
  192. X        cmd=get
  193. X        ;;
  194. X    l)    if test -n "$cmd"
  195. X        then
  196. X            echo option -l 1>&2
  197. X            echo $USAGE 1>&2
  198. X            exit 1
  199. X        fi
  200. X        cmd=ls
  201. X        ;;
  202. X    m)    if test -n "$cmd"
  203. X        then
  204. X            echo option -m 1>&2
  205. X            echo $USAGE 1>&2
  206. X            exit 1
  207. X        fi
  208. X        cmd=mget
  209. X        ;;
  210. X    d)    dbg=true
  211. X        ;;
  212. X    u)    use=true
  213. X        ;;
  214. X    v)    ver=true
  215. X        ;;
  216. X    b)    bin=true
  217. X        ;;
  218. X    h)    hash=true
  219. X        ;;
  220. X    f)    fil=$OPTARG
  221. X        ;;
  222. X    k)    key=$OPTARG
  223. X        ;;
  224. X    s)    sub=$OPTARG
  225. X        ;;
  226. X    *)    echo option -$c 1>&2
  227. X        echo $USAGE 1>&2
  228. X        exit 1
  229. X        ;;
  230. X    esac
  231. Xdone
  232. X
  233. Xshift `expr $OPTIND - 1`
  234. X
  235. X#
  236. X#    Set cmd, fil variables appropriately
  237. X#
  238. X
  239. Xif test -z "$cmd"
  240. Xthen
  241. X    cmd=ls
  242. Xfi
  243. X
  244. Xif test -z "$key" -o -z "$fil"
  245. Xthen
  246. X    fil=/dev/null
  247. Xfi
  248. X
  249. X#
  250. X#    Now to determine mac, dir variables
  251. X#
  252. X
  253. Xif test -z "$key"
  254. Xthen
  255. X    if test "$cmd" = "ls"
  256. X    then
  257. X        min=2
  258. X    else
  259. X        min=3
  260. X    fi
  261. X
  262. X    if test "$#" -lt "$min"
  263. X    then
  264. X        echo too few args 1>&2
  265. X        echo $USAGE 1>&2
  266. X        exit 1
  267. X    else
  268. X        mac=$1
  269. X        dir=$2
  270. X        shift
  271. X        shift
  272. X    fi
  273. Xelse
  274. X    #
  275. X    #    Here we need to read $fil and look for $key to get $mac. $dir
  276. X    #
  277. X    str=`grep "^$key[     ]" $fil`
  278. X    mac=`echo "$str" | awk '{ print $2 }'`
  279. X    dir=`echo "$str" | awk '{ print $3 }'`
  280. Xfi
  281. X
  282. Xif test -z "$mac"
  283. Xthen
  284. X    echo machine not set 1>&2
  285. X    echo $prog: ERROR 1>&2
  286. X    dbg=true
  287. Xfi
  288. X
  289. X#
  290. X#    Take care of possible subdirectory
  291. X#
  292. X
  293. Xif test -n "$sub"
  294. Xthen
  295. X    dir=$dir/$sub
  296. Xfi
  297. X
  298. X#
  299. X#    Now check for debugging option
  300. X#
  301. X
  302. Xif test $dbg = "true"
  303. Xthen
  304. X    echo "cmd  = \"$cmd\""
  305. X    echo "dbg  = \"$dbg\""
  306. X    echo "use  = \"$use\""
  307. X    echo "ver  = \"$ver\""
  308. X    echo "bin  = \"$bin\""
  309. X    echo "hash = \"$hash\""
  310. X    echo "mac  = \"$mac\""
  311. X    echo "dir  = \"$dir\""
  312. X    echo "fil  = \"$fil\""
  313. X    echo "key  = \"$key\""
  314. X    echo "sub  = \"$sub\""
  315. X    exit 0
  316. Xfi
  317. X
  318. X#
  319. X#    Set arguments to FTP command
  320. X#
  321. X
  322. Xif test "$use" = "false"
  323. Xthen
  324. X    optn=-n
  325. Xfi
  326. X
  327. Xif test "$ver" = "true"
  328. Xthen
  329. X    optv=-v
  330. Xfi
  331. X
  332. X#
  333. X#    Now for the actual FTP'ing
  334. X#
  335. X
  336. X(
  337. X    #
  338. X    #    If not using .netrc file, assume anonymous login
  339. X    #
  340. X    if test "$use" = "false"
  341. X    then
  342. X        echo user anonymous $USER@$HOST
  343. X    fi
  344. X    #
  345. X    #    cd to base directory
  346. X    #
  347. X    if test -n "$dir"
  348. X    then
  349. X        echo cd $dir
  350. X    fi
  351. X    #
  352. X    #    take care of binary, hash commands if needed
  353. X    #
  354. X    if test "$bin" = "true"
  355. X    then
  356. X        echo binary
  357. X    fi
  358. X    if test "$hash" = "true"
  359. X    then
  360. X        echo hash
  361. X    fi
  362. X    #
  363. X    #    execute ftp command
  364. X    #
  365. X    case "$cmd" in
  366. X    ls)    if test "$#" -gt 0
  367. X        then
  368. X            for n do
  369. X                echo ls $n
  370. X            done
  371. X        else
  372. X            echo ls
  373. X        fi
  374. X        ;;
  375. X    get)    echo get $1 $2
  376. X        ;;
  377. X    mget)    for n do
  378. X            echo mget $n
  379. X        done
  380. X        ;;
  381. X    *)    echo $prog: Invalid ftp command $cmd 1>&2
  382. X        ;;
  383. X    esac
  384. X    #
  385. X    #    all done
  386. X    #
  387. X    echo bye
  388. X) | ftp -i $optn $optv $mac
  389. X
  390. X#
  391. X#    Exit with status of FTP command
  392. X#
  393. Xexit $?
  394. END_OF_FILE
  395. if test 3169 -ne `wc -c <'sftp'`; then
  396.     echo shar: \"'sftp'\" unpacked with wrong size!
  397. fi
  398. # end of 'sftp'
  399. fi
  400. if test -f 'sftp.1' -a "${1}" != "-c" ; then 
  401.   echo shar: Will not clobber existing file \"'sftp.1'\"
  402. else
  403. echo shar: Extracting \"'sftp.1'\" \(4190 characters\)
  404. sed "s/^X//" >'sftp.1' <<'END_OF_FILE'
  405. X.\" man page
  406. X.\" Michael S Zraly
  407. X.\" mzraly@cs.umb.edu
  408. X.\" 26 July 1992
  409. X.\"
  410. X.TH SFTP 1 "July 25, 1992"
  411. X.SH NAME
  412. Xsftp \- "surgical" FTP interface
  413. X.SH SYNOPSIS
  414. X\fBsftp \-g\fP [\fB\-duvbh\fP]
  415. Xmachine dir local [remote]
  416. X.br
  417. X\fBsftp \-g\fP [\fB\-duvbh\fP]
  418. X[\fB\-f\fPfile] \fB\-k\fPkey \fB\-s\fPsubdir local [remote]
  419. X.br
  420. X\fBsftp \-l\fP [\fB\-duvbh\fP]
  421. Xmachine dir [files]
  422. X.br
  423. X\fBsftp \-l\fP [\fB\-duvbh\fP]
  424. X[\fB\-f\fPfile] \fB\-k\fPkey \fB\-s\fPsubdir [files]
  425. X.br
  426. X\fBsftp \-m\fP [\fB\-duvbh\fP]
  427. Xmachine dir file[s]
  428. X.br
  429. X\fBsftp \-m\fP [\fB\-duvbh\fP]
  430. X[\fB\-f\fPfile] \fB\-k\fPkey \fB\-s\fPsubdir file[s]
  431. X.br
  432. X.SH DESCRIPTION
  433. X.PP
  434. X.I sftp
  435. Xis a front end for the get, ls, and mget commands of
  436. X.IR ftp (1).
  437. XIt provides the means for a user to execute a ``surgical strike''
  438. Xinto an FTP site; that is, the FTP session is quick, direct, and
  439. Xeffective.  Little time is wasted.
  440. XNote that this command is very useful in conjunction with the
  441. X.IR archie (1)
  442. Xclient.
  443. X.PP
  444. XWhich command is used depends on which of the options
  445. X.BR \-g ,
  446. X.BR \-l ,
  447. Xor
  448. X.B \-m
  449. Xis given.
  450. XIn each case the user may specify the target machine and directory
  451. Xeither directly on the command line, or by specifying a
  452. X.I keyword
  453. X(with the
  454. X.B \-k
  455. Xoption)
  456. Xthat refers to an entry in a
  457. X.I ftpkeys
  458. Xfile which contains lines of the form:
  459. X.RS
  460. X.sp
  461. Xkeyword machine directory
  462. X.sp
  463. X.RE
  464. XThe
  465. X.I ftpkeys
  466. Xfile may be specified by the environment variable
  467. X``FTPKEYS''.  If this variable is not set,
  468. Xthe current directory will be searched for a file
  469. Xnamed ``.ftpkeys''; finally, if there is no such
  470. Xfile in the current directory, the user's home
  471. Xdirectory will be searched for a ``.ftpkeys'' file.
  472. X.SH OPTIONS
  473. X.TP
  474. X.B \-g
  475. XUse the FTP
  476. X.I get
  477. Xcommand.
  478. X.TP
  479. X.B \-l
  480. XUse the FTP
  481. X.I ls
  482. Xcommand.
  483. X.TP
  484. X.B \-m
  485. XUse the FTP
  486. X.I mget
  487. Xcommand.
  488. X.TP
  489. X.B \-d
  490. XRather than executing FTP command,
  491. Xprint debugging information.
  492. X.TP
  493. X.B \-u
  494. XRather than FTP'ing anonymously, use the
  495. X.I \.netrc
  496. Xfile (see
  497. X.IR ftp (1)).
  498. X.TP
  499. X.B \-v
  500. XPass the
  501. X.B \-v
  502. Xoption to the
  503. X.IR ftp (1)
  504. Xcommand, so that the session is printed to the terminal.
  505. X.TP
  506. X.B \-b
  507. XBefore file transfer, give the FTP
  508. X.I binary
  509. Xcommand.  Note that some FTP daemons automatically issue this command.
  510. X.TP
  511. X.B \-h
  512. XBefore file transfer, give the FTP
  513. X.I hash
  514. Xcommand.
  515. X.TP
  516. X.BR \-f file
  517. XUse
  518. X.I file
  519. Xin place of ``$FTPKEYS'' or ``./.ftpkeys'' or ``~/.ftpkeys''
  520. X(see above).
  521. X.TP
  522. X.BR \-k key
  523. XUse key to search for appropriate entry in
  524. X.I ftpkeys
  525. Xfile to determine target machine and directory.
  526. X.TP
  527. X.BR \-s sub
  528. XAppend
  529. X.I sub
  530. Xto target directory.  Used mainly with the
  531. X.B \-k
  532. Xoption, but allowed without it.
  533. X.SH EXAMPLES
  534. X.PP
  535. XTo list the top-level at ftp.uu.net:
  536. X.RS
  537. X.sp
  538. Xsftp ftp.uu.net /
  539. X.RE
  540. X.PP
  541. XTo get the shell script from which this command is ultimately derived:
  542. X.RS
  543. X.sp
  544. Xsftp -g pilot.njin.net pub/ftp-list getfile
  545. X.RE
  546. X.PP
  547. XTo view that file without actually copying it to the local machine:
  548. X.RS
  549. X.sp
  550. Xsftp -g pilot.njin.net pub/ftp-list getfile "|cat"
  551. X.RE
  552. X.PP
  553. XTo read that file through a more complicated command,
  554. Xor any command with spaces in it, nest quotation marks thusly:
  555. X.RS
  556. X.sp
  557. Xsftp -g pilot.njin.net pub/ftp-list getfile '"|more -c"'
  558. X.RE
  559. X.PP
  560. XTo get the index for comp.sources.misc:
  561. X.RS
  562. X.sp
  563. Xsftp -g ftp.uu.net usenet/comp.sources.misc index
  564. X.RE
  565. X.PP
  566. XTo get all the compressed files from that directory:
  567. X(Note that single quotes are used to prevent expansion
  568. Xof the shell metacharacter ``*'').
  569. X.RS
  570. X.sp
  571. Xsftp -m ftp.uu.net usenet/comp.sources.misc '*.Z'
  572. X.RE
  573. X.PP
  574. XAssume there is an entry in the ftpkeys file:
  575. X.RS
  576. X.sp
  577. XCSG ftp.uu.net usenet/comp.sources.games
  578. X.RE
  579. X.PP
  580. XNow to get the sources for the 
  581. X.IR rogue (6)
  582. Xclone:
  583. X.RS
  584. X.sp
  585. Xsftp -m -k CSG -s volume1/rogue '*.Z'
  586. X.RE
  587. X.SH ENVIRONMENT
  588. X.TP
  589. X.B FTPKEYS
  590. XIf set, this variable holds the name of the
  591. X.I ftpkeys
  592. Xfile to use.
  593. XSee the DESCRIPTION section above for more information.
  594. X.SH FILES
  595. X.TP
  596. X.B ftpkeys
  597. XThe
  598. X.I ftpkeys
  599. Xfile is used to store keywords with associated machine\- and
  600. Xdirectory\-entries.
  601. XSee the DESCRIPTION section above for more information.
  602. X.SH "SEE ALSO"
  603. X.PP
  604. X.IR archie (1),
  605. X.IR awk (1),
  606. X.IR ftp (1),
  607. X.IR getopts (1),
  608. X.IR grep (1),
  609. X.IR sh (1),
  610. X.IR test (1).
  611. X.SH BUGS
  612. X.PP
  613. XThe keyword lookup scheme could be more fully developed.
  614. X.SH AUTHOR
  615. X.PP
  616. XMichael S Zraly (mzraly@cs.umb.edu)
  617. END_OF_FILE
  618. if test 4190 -ne `wc -c <'sftp.1'`; then
  619.     echo shar: \"'sftp.1'\" unpacked with wrong size!
  620. fi
  621. # end of 'sftp.1'
  622. fi
  623. echo shar: End of archive 1 \(of 1\).
  624. cp /dev/null ark1isdone
  625. MISSING=""
  626. for I in 1 ; do
  627.     if test ! -f ark${I}isdone ; then
  628.     MISSING="${MISSING} ${I}"
  629.     fi
  630. done
  631. if test "${MISSING}" = "" ; then
  632.     echo You have the archive.
  633.     rm -f ark[1-9]isdone
  634. else
  635.     echo You still need to unpack the following archives:
  636.     echo "        " ${MISSING}
  637. fi
  638. ##  End of shell archive.
  639. exit 0
  640.