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


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 ?
sunhuxAsked:
Who is Participating?
 
Duncan RoeConnect With a Mentor Software DeveloperCommented:
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
 
sunhuxAuthor Commented:

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
 
Duncan RoeSoftware DeveloperCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
parparovCommented:
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
 
sunhuxAuthor Commented:

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
 
sunhuxAuthor Commented:


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
 
sunhuxAuthor Commented:

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
 
parparovConnect With a Mentor Commented:
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
 
sunhuxAuthor Commented:

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
 
parparovConnect With a Mentor Commented:
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
 
sunhuxAuthor Commented:

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
 
parparovConnect With a Mentor Commented:
This is an official openssh sftp client feature. I quoted here 'man sftp'.
0
 
sunhuxAuthor Commented:
excellent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.