Solved

sftp & Openssh limitation : can't rename file via ssh

Posted on 2011-09-29
13
2,268 Views
Last Modified: 2012-05-12

From my ssh client (on a Linux box) I would sftp to a remote Windows
2003 box (running Openssh server).

However using Shell scripts,
  sftp remoteid@remote_IPaddr
  sftp> rename file1 file1.bak
  sftp> put file1
  .....

However, the rename command would fail if file1 does not
exist on the remote Windows end & this would cause the
subsequent commands (including the put) to fail.

Alternatively, I can code my script to do as follows :
  ssh remoteid@remote_IPaddr 'cd directory; rename file1 file1.bak'
  but the above rename fails when coded as in the above in my
  Linux shell script : it says command not found but this rename
  command works (ie managed to rename file1 if I ssh manually
  into the remote server to issue it manually;  not if it's automated
  in a script).

I've found a GNU utility mv.exe (for Windows) which would allow my
Linux Shell script to work ie
  ssh remoteid@remote_IPaddr 'cd directory; mv file1 file1.bak'
But alas, due to security reason, the remote end (which is a
military related organization) won't allow freeware / GNU /
Open source tools to be installed/loaded there


So what other options do I have now to automate remote renaming
of file via sftp/scp/ssh ?
0
Comment
Question by:sunhux
  • 7
  • 4
  • 2
13 Comments
 

Author Comment

by:sunhux
Comment Utility

Also,  the following would fail if file1. bak already exists
& the failed operation would cause all subsequent sftp
commands/operations after it to fail:
sftp> rename file1 file1. bak
0
 
LVL 34

Expert Comment

by:Duncan Roe
Comment Utility
You need to query whether the file exists before trying to rename it. This is not an sftp function per se, but I would do it by driving sftp via an expect script.
0
 
LVL 9

Expert Comment

by:parparov
Comment Utility
Why would failure in rename command cause entire sftp to fail? It doesn't for me.
There is also an 'ls' command in sftp that specifically tests for file's presence.
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 125 total points
Comment Utility
Can you post actual output? (especially from the script)
For the script, insert the following 2 lines at line 2 (straight after #!/bin/sh)
exec >> /tmp/script.out 2>&1
set -xv

Open in new window

Run the script and then post /tmp/script.out
0
 

Author Comment

by:sunhux
Comment Utility

Yes I know there's ls command in sftp but can parparov give exact sample Linux
Shell script on how to test this?  How would you pass the ls test result within
sftp to the next sftp command to test before executing the rename?
0
 

Author Comment

by:sunhux
Comment Utility


Duncanroe

Below is the content of script.out :

      {
       echo "lcd /tmp"

      echo "rename nonexist.txt nonexist.txt.bak"
      echo "put exist.txt"
      echo "quit"
      } | sftp -b - remoteid@192.168.25.25

      *** Warning: Unauthorized users, logoff now ! ***

sftp> lcd /tmp
sftp> rename nonexist.txt nonexist.txt.bak
Couldn't rename file "/home/xxxx/nonexist.txt" to /home/nspd/nonexist.txt.bak": No such file or directory
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:sunhux
Comment Utility

The "put exist.txt" was not executed due to the rename failure.

So, Parparov, in my Linux sftp client (to Windows Openssh) case, everything else after an error statement don't get executed
0
 
LVL 9

Assisted Solution

by:parparov
parparov earned 375 total points
Comment Utility
Ok, you're using sftp -b.

From the manual:
 -b batchfile
             Batch mode reads a series of commands from an input batchfile
             instead of stdin.  Since it lacks user interaction it should be
             used in conjunction with non-interactive authentication.  A
             batchfile of `-' may be used to indicate standard input.  sftp
             will abort if any of the following commands fail: get, put,
             rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp,
             lpwd, df, and lmkdir.  Termination on error can be suppressed on
             a command by command basis by prefixing the command with a `-'
             character (for example, -rm /tmp/blah*).
That means you need to do
-rename file1 file2

Open in new window

instead of
rename file1 file2

Open in new window

Try it.
0
 

Author Comment

by:sunhux
Comment Utility

Ok, thanks;  the "-rename"  syntax works ie it won't fail subsequent
sftp commands if file1 doesn't exist.  However, if file2 exists, "-rename"
doesn't allow file1 to be renamed/overwritten to file2 :

I'm quite sure I used the same remoteid to sftp put to the remote WIndows
Openssh server & I used the same remoteid to "-rm file2" in sftp as well:



sftp> lcd /tmp
sftp> -cd /nonexist
Couldn't canonicalise: No such file or directory
sftp> -rm nonexist.txt.bak
Removing /home/nspd/nonexist.txt.bak
sftp> -rename nonexist.txt nonexist.txt.bak
Couldn't rename file "/home/nspd/nonexist.txt" to "/home/nspd/nonexist.txt.bak": Failure
sftp> put exist.txt nonexist.txt
Uploading exist.txt to /home/nspd/nonexist.txt
Couldn't get handle: Failure

0
 
LVL 9

Assisted Solution

by:parparov
parparov earned 375 total points
Comment Utility
Works for me:
>cat sftp.test | sftp test.host -b
Connecting to test.host...
sftp> -cd /tmp
sftp> -rm TODO.old
Couldn't stat remote file: No such file or directory
Removing /tmp/TODO.old
Couldn't delete file: No such file or directory
sftp> -rename TODO.txt TODO.old
Couldn't rename file "/tmp/TODO.txt" to "/tmp/TODO.old": No such file or directory
sftp> put TODO TODO.txt
Uploading TODO to /tmp/TODO.txt
TODO                                          100%  287     0.3KB/s   00:00    

Open in new window

0
 

Author Comment

by:sunhux
Comment Utility

Thanks, it's just that particular file only;  subsequent files that I put over
could be rm'ed.

Last question:
Btw, is this "-" (eg: -rename, -rm, ...) feature something that's officially
supported in sftp or sftp RFC ?  I can't find this feature in Linux man
pages & just wanted to be sure it will continue to be there when we
migrate to Solaris 10 sftp client or Redhat 5.x sftp client (in the next
few months' time)
0
 
LVL 9

Assisted Solution

by:parparov
parparov earned 375 total points
Comment Utility
This is an official openssh sftp client feature. I quoted here 'man sftp'.
0
 

Author Closing Comment

by:sunhux
Comment Utility
excellent
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
We all know how boring and exhausting it is to transfer huge web projects developed locally to a webserver simply via FTP. The File Transfer Protocol is a really nice solution if you need to transfer small amounts of files, but if you're plannin…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now