SFTP function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SFTP function
# 1  
Old 01-22-2015
SFTP function

I have FTP function:
Code:
FTPfunction ()
{
integer sw=4
ftp -niv <<- !  > $FTPLOG 2>&1
open $1
user $2
$3
put /tmp/ptdata.csv
close
bye
!

but, I need to use sFTP and port 22.
Can you please let me know how to modify FTPfunction I have?

Please advise. Thank you!!
# 2  
Old 01-22-2015
As you provided the code:
Code:
FTPfunction ()
{
integer sw=4
sftp -niv <<- !  > $FTPLOG 2>&1
open ${1}:22
user $2
$3
put /tmp/ptdata.csv
close
bye
!
}

hth
# 3  
Old 01-22-2015
sftp by default connects to port 22 on the remote host. I'm not sure about the :22 part of the answer above ...on connecting to my sftp server it causes problems.
# 4  
Old 01-22-2015
Also the -niv parameters are not valid for sftp.

Password authentication is also an issue - if you need to automate sftp connections you should setup non-interactive ssh sessions first. Once you have that your function could be:

Code:
FTPfunction ()
{
integer sw=4
sftp -b - ${2}@${1} <<- !  > $FTPLOG 2>&1
put /tmp/ptdata.csv
quit
!
}

or simply:

Code:
FTPfunction ()
{
  scp -B /tmp/ptdata.csv ${2}@{$1}: > $FTPLOG 2>&1
}

# 5  
Old 01-23-2015
As Chubler_XL says, you need to sort out authentication. I don't believe you can use sftp with a here document to include the credentials. You could write an expect script, but it is so much effort and degrades the security that sftp is enforcing.

You can also use sftp in 'batch' mode, in that it will read a command file. Your main script could then end up as something like this:-
Code:
sftp -b $command_file $user@$server

The contents of the command file would be the commands you would require, e.g. cd, put etc.

Errors in this process will cause a non-zero return code from sftp which is far easier to test than reading the output from a normal ftp with the -v flag. It's very easy in shell script and more complex in expect so there's another reason to avoid that.



I hope that this helps,
Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. Shell Programming and Scripting

Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements . #!/bin/sh echo "Starting to sftp..." sftp admin@myip << END_SCRIPT cd /remotepath/ lcd /localpath/ mget myfiles*.csv bye END_SCRIPT echo "Sftp successfully." echo echo... (11 Replies)
Discussion started by: scriptscript
11 Replies

6. Red Hat

Chroot sftp users, remote sftp login shows wrong timestamp on files

Hello, I have a weird issue, I have RHEL 5.7 running with openssh5.2 where sftpgroup OS group is chroot. I see the difference difference in timestamp on files, when I login via ssh and SFTP, I see four hour difference, is something missing in my configuration. #pwd... (8 Replies)
Discussion started by: bobby320
8 Replies

7. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

8. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

9. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question