Pull a file from a remote server through a shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pull a file from a remote server through a shell script
# 1  
Old 04-23-2008
Pull a file from a remote server through a shell script

Hi,

I am writing a shell script to pull a file from a remote server (Let say its a windows based remote server). One of my criteria is to pull a file only if it is not empty.

We have done a similar script to push a file from our end to a remote server and before pushing it we check for the file size and only when it is not empty we proceed to ftp the file. This script works fine without any issues.

Here is the sample script for pushing the files to remote server.
----------------------------------------------------------------------
#!/usr/bin/ksh

"Usage: $0 <Server> <USERID> <LOCALFILEPATH> <REMOTEFILEPATH> <FTPTYPE> <FILECHECKER> <FILENAME1> <FILENAME2>...."

#FTPTYPE is either ascii or binary
#FILECHECKER is either Y or N. When Y the script has to check for file size before ftp'ing the file. It has to ftp only if the file size is > 0

#Counting the list of Files to be FTP'ed
shift 6
file_count=$#
echo "Total files to be ftped : ${file_count} "

LOCAL_FILE_LIST=$@
#Get the files that needs to be ftp'ed into a LOCAL_FILE_LIST.


#If File Checker is "Y" check if the files are empty and prepare a new file list
fl_cnt=0

if [ ${FILE_CHECKER} = "Y" ]
then
for flnm in ${LOCAL_FILE_LIST}
do
if [ -s ${flnm} ]
then
FILE_LIST=${FILE_LIST}" "${flnm}
fl_cnt=`expr ${fl_cnt} + 1`
fi
done
else
FILE_LIST=$LOCAL_FILE_LIST
fi
echo $FILE_LIST


if [ ! -z "$FILE_LIST" ]
then
echo "FTP Started"
ftp -n << EOF > $LOG_FILE
verbose
open ${SERVER}
$LOGON
$FTP_TYPE
cd ${REMOTE_FILE_PATH}
mput ${FILE_LIST}
close
bye
EOF
else
echo "Files Empty hence not FTP'd"
exit 0
fi
----------------------------------------------------------------------

Now I am having issues with my pull script, when I connect to the remote server I am not able to execute any shell commands on that server since it is a windows based. Hence I won't be able to check for file size before I pull those files.

Can anyone give some idea's/tips on how to check for file size on remote server or how to execute some shell commands on remote server?

Thanks,
Sashank
# 2  
Old 04-23-2008
The FTP protocol has commands for getting the file size, but the output format etc was never standardized and what you get from a random Windoze box might be a bit tacky. Anyway, the big problem with using a run-of-the-mill ftp client is that once you have started the client, you can't easily interact with it.

My first suggestion would be to look at more feature-oriented ftp clients such as lftp or ncftp. My second suggestion, half in jest, would be to cobble together your own little simple client from Perl and chewing gum. Probably Python has good libraries for this, too (actually most likely better out of the box than Perl). Unless of course you can install a better server over on the Wintendo box (SSH has the added benefit that it is also secure).
# 3  
Old 04-23-2008
Quote:
Originally Posted by era
The FTP protocol has commands for getting the file size, but the output format etc was never standardized and what you get from a random Windoze box might be a bit tacky. Anyway, the big problem with using a run-of-the-mill ftp client is that once you have started the client, you can't easily interact with it.

My first suggestion would be to look at more feature-oriented ftp clients such as lftp or ncftp. My second suggestion, half in jest, would be to cobble together your own little simple client from Perl and chewing gum. Probably Python has good libraries for this, too (actually most likely better out of the box than Perl). Unless of course you can install a better server over on the Wintendo box (SSH has the added benefit that it is also secure).
Or when you get the file to the local unix server check for the file size then, if it's empty then remove it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Except script to run a local shell script on remote server using root access

local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script" (1 Reply)
Discussion started by: Aditya Avanth
1 Replies

2. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

3. Shell Programming and Scripting

To run a shell script in remote server from windows batch file

Hi all, i need to run a shell script on remote server. I have created file .bat file in windows server with following code, c:\Users\Desktop\putty.exe -ssh -pw password user@server ./script.sh i need to run the script.sh in my remote server Above command is not working, any... (4 Replies)
Discussion started by: rammm
4 Replies

4. Shell Programming and Scripting

Editing a file on remote server using shell script locally

Hi Scott, My previous post was not for any school or college projects. I am currently working with a IT company (Cannot provide more details than this). I am trying to implement the below script in my day-to-day work, Apologies for the confusion in previous post :). My question remains same... (4 Replies)
Discussion started by: Nishant Ladiwal
4 Replies

5. Shell Programming and Scripting

Editing a file on remote server using shell script locally

Hi All, I have below requirements for my project: 1. Building a shell script which connects to a remote server 2. running script on local machine as user 'A' 3. connecting to server using user 'B' with password 4. login with a powerbroker role 'P' (asks for same password as 'B') on that... (1 Reply)
Discussion started by: Nishant Ladiwal
1 Replies

6. Shell Programming and Scripting

Script to pull uid greater than 1000 from remote server

Hello, I am trying to get UID # greater than 1000 from all linux server, I tried this script getting this error message, someone please suggest. $for i in `cat hostlist.0709.org` ; do ssh -t $i 'awk -F':' "{ if($3 >= 1000) print $0 }" /etc/passwd ' >> output ; done $ cat output hostname1... (4 Replies)
Discussion started by: bobby320
4 Replies

7. Shell Programming and Scripting

Shell script to read from a file and update remote server

Its a tough one and would appreciate any guidance for a script that i am trying to develop....Again I do understand its a complicated script and help would be greatly appreciated.... Thank you 1- Need to check for a file (in a certain location on a server) every 15 minute or so if it is there... (4 Replies)
Discussion started by: aavam
4 Replies

8. Shell Programming and Scripting

How to FTP the latest file, based on date, from a remote server through a shell script?

How to FTP the latest file, based on date, from a remote server through a shell script? I have four files to be FTP'ed from remote server. They are of the following format. build1_runtime_mmddyyyy.txt build2_runtime_mmddyyyy.txt build3_runtime_mmddyyyy.txt buifile_count_mmddyyyy.txt ... (9 Replies)
Discussion started by: imran_affu
9 Replies

9. Shell Programming and Scripting

Archive log Pull script from one server to another server

Hi all, Iam looking out for a shell script which pulls archive from Server A to Server B. And at the same time, we dont want the archives which are already pulled in Server B to be removed from Server A. Please help me on this, I have been trying on this for a quiet few time. ... (3 Replies)
Discussion started by: vivi.raghav
3 Replies

10. Shell Programming and Scripting

Use cURL in shell script and get most recent file from remote server using SFTP

I have been trying to implement the following shell script -- sftp to remote server get list of files in the directory sftp get the most recent listed file exit This script will be scheduled to be executed everyday using CRON or CONTROL-M and the need is to have absolutely no human... (7 Replies)
Discussion started by: toobrown1
7 Replies
Login or Register to Ask a Question