Skip to content

Instantly share code, notes, and snippets.

@Nuxij
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nuxij/137451a2100ef9502ceb to your computer and use it in GitHub Desktop.
Save Nuxij/137451a2100ef9502ceb to your computer and use it in GitHub Desktop.
Background FTP session, with processable output
#!/bin/bash
# Make the fifos
mgmtFifo="/tmp/.ftp.mgmt.fifo"
cmdFifo="/tmp/.ftp.cmd.fifo"
outFifo="/tmp/.ftp.out.fifo"
mkfifo "$mgmtFifo"
mkfifo "$cmdFifo"
mkfifo "$outFifo"
# We may want a way to echo back for the sub-shell
exec 4>&1
# Open fifos with r/w
exec 5<>"$mgmtFifo"
exec 6<>"$cmdFifo"
exec 7<>"$outFifo"
# No more stdin (&0)
exec 0>&-
{
ftp -v example.com <&6 >&7 &
while true; do
read -u5 mgmtString
[[ "$mgmtString" == "exit-subshell" ]] && exit 0
sleep 1
done
} &
echo "prompt" >&6
echo "ls" >&6
# Process ftp output until we say so
startSeen=0
finishedProcessing=0
while [[ "$finishedProcessing" -eq 0 ]]; do
read -u7 ftpOutput
[[ "$ftpOutput" == "Interactive mode off." ]] && startSeen=1
if [[ "$startSeen" -eq 1 ]]; then
if [[ "$ftpOutput" != "226 Directory send OK." ]]; then
if [[ ! "$ftpOutput" =~ ^[1-6][0-5][0-9]\ ]]; then
filename="$(echo $ftpOutput | cut -d' ' -f9)"
echo "$filename"
fi
else
finishedProcessing=1
fi
fi
done
echo "quit" >&6
sleep 1
echo "exit-subshell" >&5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment