- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Script to send the file through SFTP.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2011 10:34 PM
тАО03-30-2011 10:34 PM
I have written a script to send the file through sftp to customer.
cat /script/xyz_abc.ksh
#!/bin/ksh
Datestamp=$(date +%Y%m%d%H%M)
su - abcadm -c "/script/xyz_backup.ksh" -- This script first backup the original file
su - abcadm -c "/script/xyz_chown.ksh" -- This script changes the ownership of the file
su - ftpabcd -c "/script/xyz_sftp.ksh" -- This script send the file to customer
su - abcadm -c "/script/xyz_remove.ksh" -- This script remove the file
I want to add one more logic in above script to check for first sftp connectivity possible i.e. whether customer sftp server is able to connect. If sftp connectivity is ok then only the remaning part of the script should run or otherwise it should not run the remaining part of the script. If there is network connectivity
problem then it should send an e-mal "network connectivity problem"
Thanks,
Narendra
Solved! Go to Solution.
- Tags:
- sftp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2011 11:33 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2011 01:50 AM
тАО03-31-2011 01:50 AM
Re: Script to send the file through SFTP.
I am using sftp command i.e. no ssh to connect...how to use sftp command in your script as below. And also i will have to login with su - ftpabcd in script then only sftp will work i.e. if i run from root it will give an error "Permission denied (publickey,password,keyboard-interactive)."
ssh -qo "BatchMode=yes" user@host "echo 2>&1"
if [ $? -eq 0 ]
then
echo "ok";
#rest of your code
else
echo "failed";
#send email
fi
Thanks,
Narendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2011 05:05 AM
тАО03-31-2011 05:05 AM
Re: Script to send the file through SFTP.
> ...how to use sftp command...
First, you need to implement public keys for 'ssh/sftp/scp' so that no interaction is required.
If you can't simply use 'scp' to perform your copy, you can do something like this (by example):
# cat ./mysftp
HOST=${1}
FILE=${2}
sftp -b - <
cd /home/me
put -P ${FILE} ${FILE}.backup
EOF
RC=$?
print -u2 "\nSFTP returned ${RC}"
exit ${RC}
...This allows you to do:
# ./mysftp somehostname myfile
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2011 09:43 AM
тАО03-31-2011 09:43 AM
Re: Script to send the file through SFTP.
Shouldn't that be
sftp -b - ${HOST} <
?
Otherwise, isn't ${HOST} now part of the here-doc instead of the sftp command line?
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2011 10:56 AM
тАО03-31-2011 10:56 AM
Re: Script to send the file through SFTP.
> Michael: Shouldn't that be: sftp -b - ${HOST} <
Yes, it probably should though either variation works. I agree with you that your order is correct. The syntax should be:
sftp -b batchfile [user]@host
...where the '-' is STDIN or in this case the redirected here-doc.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-31-2011 09:35 PM
тАО03-31-2011 09:35 PM
Re: Script to send the file through SFTP.
Thanks for the reply but that is not what i am looking. I have already written the script for file transfer. Just my requirement was to add one more logic in above script to check for first sftp connectivity possible i.e. whether customer sftp server is able to connect. If sftp connectivity is ok then only the remaning part of the script should run or otherwise it should not run the remaining part of the script. If there is network connectivity problem then it should send an e-mail "network connectivity problem"
Thanks,
Narendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2011 02:52 AM
тАО04-01-2011 02:52 AM
Re: Script to send the file through SFTP.
Basically what you want is an if...then...else statement, which is just what they showed you. Especially look at the solution from Modris. All this logic can be incorporated into a single script, so it is more easier reaadable. If you want to use some parts of the script for another tasks, you can declare functions in the script and re-use them in other scripts. But you definitely don't need to separate your script into so much files.
Regards,
Viktor
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-01-2011 08:29 AM
тАО04-01-2011 08:29 AM
Re: Script to send the file through SFTP.
> more logic in above script to check for
> first sftp connectivity possible i.e.
> whether customer sftp server is able to
> connect. [...]
Why? What does that buy you? If SFTP does
not work, then your first file transfer
attempt should fail. Can't you detect that
failure?
If your "sftp connectivity" test succeeds,
then something could still go wrong with the
actual file transfer, couldn't it?
What do you care about, "sftp connectivity"
or an actual file transfer? If you care
about an actual file transfer, then why do
some extra task which will not tell you any
more than you will learn by trying to do the
task which you actually want done?
It might be fun, but why waste the time, if
it's not actually useful?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-04-2011 09:32 PM
тАО04-04-2011 09:32 PM
Re: Script to send the file through SFTP.
Why i am trying to add one more logic is because in main script i am deleting the file
once it is transfer to customer i.e.
"/script/xyz_remove.ksh" -- This script remove the file.
But here is the problem if there is sftp connectivity problem then remaining part of the script still runs and it will remove the file. As we have schedule the main script to run 4 times a day. If for any reason there is sftp connectivity problem then still the file will be there in the specified directory if we found that there is some connectivity problem in first sftp connectivity test so that remaining part of the script will not run. And then in next run suppose sftp connectivity problem resolves then the file will be sent to customer. And why i am deleting the file is once the file is sent to customer as we don;t want to send the duplicate files once again to customer.That's why i am deleting the files so everytime when file is sent it will be new one.
Thanks,
Narendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-05-2011 05:04 AM
тАО04-05-2011 05:04 AM
Re: Script to send the file through SFTP.
> But here is the problem if there is sftp connectivity problem then remaining part of the script still runs and it will remove the file
You need to look again at the solutions Modris and I suggested. We check the *return code* of the 'sftp' to ascertain success or failure. It is that upon which you need to base the removal (or not) of the file in question.
As Steven has pointed out, there is no reason to test connectivity (with 'ssh'?). There is nothing to gain. If you can transfer the file, then remove it. If you can't transfer the file, then don't remove it. Even if you had just made a viable connection test, there is no guarantee that the file transfer will succeed. You could have a permission problem on the destination server that prevents transfer or you could have a file size limitation there that prevents transfer.
You are making this much more difficult than it needs to be.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-26-2011 03:20 AM
тАО04-26-2011 03:20 AM
Re: Script to send the file through SFTP.
Would an SFTP/expect script such as the one at http://vouters.dyndns.org/tima/Linux-OpenSSH-expect-Doing_sftp_transfers_from_a_shell_script.html
do the trick ? Of course, you have to adapt the SFTP command to your SFTP running on your HP-UX. I do not think this represents a big deal.
Regards,
Philippe