I have following script to get given file from given remote directory by accepting following parameters
FSERVER=$1
FUSER=$2
SRC_DIR=$3
REMOTE_SRC_DIR=$4
FILE_NAME=$5
cd $SRC_DIR
sftp $FUSER@$FSERVER <<GOTO
cd $REMOTE_SRC_DIR
ascii
get $FILE_NAME
bye
To access the files from $REMOTE_SRC_DIR
to SRC_DIR
do I need port open from both side? I.e. bi-directional or just one port from Remote Server to Source and it should need "INITIATE" session from the source. And what is the reason?
As per my understanding we are connecting to remote server path and then writing the query Get File name. So we need to bi-directional access.
SFTP uses a single TCP connection. In general, TCP connection is stateful. As such, once opened both sides can send data to each other. Only the passive side of the connection needs to initially have a well known port number opened (22 for SSH/SFTP in this case). The active side opens a random port number that the passive side learns from the TCP connection initiation packed. This passive-side port closes with the TCP connection. While the active-side port is kept open for future TCP connections.
The SFTP protocol uses strictly request-response model. I.e. although the TCP allows both sides to send data anytime, with the SFTP, the server never sends data on its own, but always in a response to client request. Note that this does not mean, that no unsolicited data flows from the server to the client on network level, as in both underlying protocols of the SFTP (the TCP and the SSH) both sides of connection can send (and send) packets anytime.
Simplified flow is:
cd
command is simulated on client side. The SFTP server is not aware at all of client remote working directory. SFTP client typically only verifies existence of the new working directory with the SFTP server.ascii
command: The OpenSSH sftp
client does not have ascii
command. You should get "Invalid command." Unless you use other client than OpenSSH.get
command: For file transfers the SFTP protocol offers a similar block-level API as most operating systems (contrary to a stream API of FTP protocol). So SFTP client sends "open file" request, over the existing connection, followed by repetitive "read block" requests and "close file" request. As with any SFTP requests, responses go back over the same TCP connection.