Open and System command in SFTP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Open and System command in SFTP
# 8  
Old 04-17-2011
I need to create automate sftp process ( non-interactive mode). In order to do that i am using here documents file. /dev/stdin refers current process for sftp commands.

"automate sftp using unix script". forum gives you the details.
# 9  
Old 04-27-2011
I wrote a script little edition sftp Smilie but i cannot control completely.
maybe it has some errors.now you can try this if you get error any issue you can notify me pls..

An example of use

Code:
# ./sftp.sh sftpuser 10.100.110.23 /sftpuploaddir /home/sftpuser/localdir localfiles

Code:
#!/bin/bash
# automatize SFTP script's goal is copy $DIR contents to Remote System via SFTP
# script is udner /usr/local/bin/
# to run script successfully you must add user pubkey into server authkeys
 
remoteCON=$1@$2 # username and remote host IP
localDIR=$4    # uploadDIR is source directory which will be copied from this host to remote
files=$5      # uploadfiles which files in uploadDIR for exa "*.xml" or "_.cfg_" or "server.conf" ...
remoteDIR=$3    # remoteDIR is target directory which will be uploaded to remote server
batchfile=/usr/local/bin/sftpbtch
controlfile=/usr/local/bin/sftpcntrl
errorfile=/usr/local/bin/sftperr
email=xyz@domain.com  # change email address!!
 
if [ $# -ne 5 ] ; then
 echo "usage:  \"$0 'username' 'remoteIP' 'remote_directory [target]' 'local_directory [source]' 'files' \"
[e.g -->  $0 sftpuser 10.100.110.110 /usr/users/sftp/upload  /usr/local '*'   ]
[e.g -->  $0 sftpuser remoteSTPserver /usr/users/sftp/upload  /usr/local *.xml   ]
[e.g -->  $0 sftpuser 10.100.250.149 /home/users/john/upload  /home/webuser \"file1 file2 file3\"  ] "
 exit 1
fi
remoteDIR=$(echo $3|sed 's/\/$//')
localDIR=$(echo $4|sed 's/\/$//')
if [ "$files" == '*' ] ; then files=($(ls -1a $4|grep -Ev '^[.][.]$|^[.]$') ) ; fi
 
echo 'cd '$remoteDIR''>$batchfile
for i in ${files[@]}
 do
  echo "put $localDIR/$i">>$batchfile
 done
echo quit>>$batchfile
sftp -v -b $batchfile $remoteCON<<SFTP &> $controlfile
SFTP
 
if [[ $(grep -i "not found." $controlfile) ]] ; then
  echo "Upload process is fail!!
    -- Check your input files.." | tee $errorfile
  mail -s "SFTP UPLOAD FAIL" $email <errorfile
elif [[ $(grep -i "Permission denied (publickey" $controlfile) ]] ; then
  echo "Upload process is fail!!
    -- Be sure your public key in $2 server in $(id -un) user HOME directory in 'authorized_keys'
    -- Be sure right permission on .ssh and authorized_keys [ like 700 ~/.ssh and 600 ~/.ssh/authorized_keys ] " |tee $errorfile
  mail -s "SFTP UPLOAD FAIL" $email <errorfile
elif [[ $(grep -i "t get handle" $controlfile) ]] ; then
  echo "Upload process is fail!!
    --  Be sure write permission in $remoteDIR " |tee $errorfile
  mail -s "SFTP UPLOAD FAIL" $email <errorfile
fi


regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Checking open ports in a system

Hi, There are two servers which needs to be connected via the port number 1521 from a remote server. This is the port config in the first server : L28tstream1 : # netstat -tulpn | grep 1521 tcp 0 0 10.67.26.183:1521 0.0.0.0:* LISTEN ... (2 Replies)
Discussion started by: anaigini45
2 Replies

2. Programming

open() system call in c++????

Hi friends, I am trying to use the open system call in c++ language. Please have a look at my code. vi Open.cpp 1 #include <stdio.h> 2 #include <iostream> 3 4 #define BUFSIZE 1 5 6 using namespace std; 7 8 int main() 9 { 10 ... (5 Replies)
Discussion started by: gabam
5 Replies

3. 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

4. UNIX for Advanced & Expert Users

Link from mount point to sftp account of file system

Hi, Is it possible to do hard link between the mount point of file some structure to the sftp account of some other server ? If possible then what could be the behavior of link properties? Is it could be the same as we found in our environment (unix/linux). Even if we do such link , then it... (1 Reply)
Discussion started by: posix
1 Replies

5. Shell Programming and Scripting

Shell script to Open file in SFTP and get first 6 character

Hi, I am creating a script that will: 1. Connect to SFTP server 2. Get the file content's first 6 character (ddmmyy) and compare it to today's date (also in ddmmyy format). This is the header of the file. However, the header contains a few more information. So i need to only get the first 6... (0 Replies)
Discussion started by: cherriesh
0 Replies

6. SCO

SCO Open Server 6 - System Halt

Dear All, Newbie here, hope my post goes to the correct room. System: SCO Open Server 6.0 Everything running well, after this Sunday morning noone can login via telnet or via putty, the system suddenly reject without any message (and also ftp). The local IT Support there (without enough... (2 Replies)
Discussion started by: virgani
2 Replies

7. UNIX for Advanced & Expert Users

System Call Wrapper of 'open'

When the programmer use 'open' function, the process is like below. "open -> system call wrapper of open in Glibc -> syscall_open in kernel" I found the wrapper of open, but there is no implementation like 'int $80'. int __open (file, oflag) const char *file; int oflag; { ... (3 Replies)
Discussion started by: yuno96
3 Replies

8. Shell Programming and Scripting

SFTP to MVS system

Hi All, I have written a script that transfers(FTP) files from Unix system to MVS system. Below is the code for it. ftp -inv $HOST<<ftp_test quote site LRECL=200 quote site BLKSIZE=28000 mput SOURCE DEST ftp_test Here I need the help, if the above code can get... (3 Replies)
Discussion started by: rinku11
3 Replies

9. AIX

Open Ports on System

Hi Every body, What is the command on AIX 5.2 that can be used to get all open ports? (2 Replies)
Discussion started by: aldowsary
2 Replies
Login or Register to Ask a Question