2

I am using OpenSSH. Below bat file code when called from standalone Java program transfers files properly.

@echo off
del "D:\path\inctf2.txt" /s /f /q
set par1=%1
set par2=%2
set par3=%3
set par4=%4

>>D:\path\inctf2.txt echo lcd %par2%
>>D:\path\inctf2.txt echo cd %par1%
>>D:\path\inctf2.txt echo put %par3% %par4%
>>D:\path\inctf2.txt echo quit 

C:
cd C:\Program Files\OpenSSH-Win64

sftp.exe -b D:\path\inctf2.txt userName@IP >> D:\path\%par3%%para4%.log 2>&1

goto END
:END

But When bat file is called from class file inside war deployed on weblogic server. It fails with below error:
Could not create directory '%systemroot%\system32\config\systemprofile/.ssh'. percent_expand: unknown key %s Connection closed

Copied .ssh folder from windows>User>Administrator to D:\path, provided full control also to User:everyone, but no luck.

Java Code:

processBuilder.command("cmd", "/c", "transfer.bat",cdRemoteDir, localDir_For_fileName, 
fileName, remoteFileName );

Process process = processBuilder.start();

int exitVal = process.waitFor();

Tried below code also with same result:

Runtime.getRuntime().exec(

When executed from command_line:

C:\Program Files\OpenSSH-Win64>user_name@ip_address
Connected to ip_address.
sftp> cd $DVL2.CAFINP
sftp> lcd F:\local_file_path
sftp> put OC130319.001 OC130301 0,500,500,800
Uploading OC130319.001 to /G/DVL2/CAFINP/OC130301
OC130319.001
sftp> ls -lrt OC130301
-*********    0 4294952959 199         16718 Apr 30 10:45 OC130301
sftp>

Finally I moved to WinSCP used below commands in bat file:

C:
cd C:\WinSCP5.13.7
winscp.com /script=script.txt >>D:\path\logs\alpha.txt

open sftp_ppk_auth
cd $DVL2.CAFINP
lcd F:\local_path\
put MANCAF OUT4567,0,500,500,800
exit

sftp_ppk_auth is saved WinSCP session
File was successfully transferred but below error message was recorded to logs:

MANCAF | 217589 KB | 2618.9 KB/s | binary | 100% Transfer was successfully finished, but temporary transfer file 'OUT4567,0,500,500,800.filepart' could not be renamed to target file name 'OUT4567,0,500,500,800'.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Don't run an external console application to implement SFTP. Use a native SFTP Java library, like JSch. – Martin Prikryl Apr 26 '19 at 16:10
  • I was using jsch but large size files were partially transferring to base24 switch. So had to add some extra parameters to put command: put local_file remote_file 0,500,500,800 which was not possible with jsch. Or is it possible? – user3427939 Apr 27 '19 at 17:13
  • Jsch error code 4 description failure. I think it's generic code. If you can suggest how to add those file related parameters with jsch it will be great. – user3427939 Apr 27 '19 at 17:53
  • I do not understand. What is `0,500,500,800`? [OpenSSH `sftp` `put` command](https://man.openbsd.org/sftp#put) takes at most two parameters. – Martin Prikryl Apr 27 '19 at 18:49
  • Those parameters are valid and meant to either convey page size and other file related stuff to remote system or restructure the file during transfer. I will update if I get more info about them. – user3427939 Apr 28 '19 at 06:15
  • Your code does `echo put %par3% %par4%`, there's no additional parameter. We need [mcve]. – Martin Prikryl Apr 28 '19 at 07:02
  • Hi Martin I have edited the question, added the sftp command line details with extra parameters which transfer files properly. Can you help with the issue. That command is correct will provide more details about the command in some time. – user3427939 Apr 30 '19 at 05:38
  • What do you get if you do simple `put OC130319.001 OC130301`? – Martin Prikryl Apr 30 '19 at 05:49
  • Plain put command from command line works properly, but not when called from war inside weblogic. I faced issue while transferring large file using java jsch library initially. Files were partially transferred. Remote system write to disk properly (complete file) if it is formatted with those additional parameters which was not possible with jsch. So I opted for calling bat file from Java with those parameters. But here the code works when called from standalone Java program but doesn't when called from war in side weblogic. Please suggest. – user3427939 Apr 30 '19 at 06:11
  • I do not have anything to suggest. The `put` command of standard OpenSSH `sftp` **does not take three parameters**. Unless you use some custom build of OpenSSH. But if you do, we cannot know what those a parameters do. – Martin Prikryl Apr 30 '19 at 06:14
  • @MartinPrikryl: that's a Tandem^WCompaq^WHPE NonStop in its Guardian 'personality' (which is where Base24 primarily runs) and does take optionally a letter and some numbers following the (real) filename, but: &&OP: they **should be separated from the filename (and each other) by a comma not a space**. See pp209-210 at https://www.manualslib.com/manual/513672/Hp-Nonstop-Ssh-544701-014.html?page=209#manual . I am no longer in the job where I could test this, but if you use destination filename `realname,num,num,etc` in Jsch I think it should work. – dave_thompson_085 Apr 30 '19 at 07:26
  • @dave_thompson_085 Makes sense. And only confirms my assumption that the question makes no sense. – Martin Prikryl Apr 30 '19 at 08:01
  • Thank @ dave_thompson_085 you nailed it, comma did the job. but @Martin Prikryl please refer the original question. It wasn't about those parameters but why the code is not executing from inside weblogic war. those parameters are valid even with ftp for WHPE non stop destination. Anyways thanks for your valuable time. – user3427939 Apr 30 '19 at 14:51
  • But you have asked the question because you claimed that JSch does accept those additional parameter, so you need to use command-line `sftp`. What turned out not to be true, because those additional parameters are actually a part of the file name. – Martin Prikryl May 01 '19 at 05:32
  • Btw, regarding your problem with WinSCP: use `put -resumesupport=off MANCAF OUT4567,0,500,500,800` – Martin Prikryl May 01 '19 at 05:34
  • Sorry for late revert @Martin Prikryl, Thanks a lot, finally it worked without any issues. – user3427939 May 08 '19 at 08:10
  • OK, I've posted this and the information by @dave_thompson_085 as an answer to close this question. I have also tried to edit the question title to better describe what the question is actually about. – Martin Prikryl May 08 '19 at 12:53

2 Answers2

0

Those additional parameters should actually come as a part of the destination file name (credit goes to @dave_thompson_085).

So no matter, what SFTP client you end up using (JSch, psftp, WinSCP), use a target file name like:

MANCAF OUT4567,0,500,500,800

With WinSCP, you will need to use -resumesupport=off switch to avoid a transfer via a temporary file name. Otherwise WinSCP will get confused by the non-existence of what it believe is an actual file name, while it is not.

put -resumesupport=off MANCAF OUT4567,0,500,500,800
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1

My guess is that it is attempting to create a new entry in HOME/.ssh/known_hosts and your bat file is inheriting the credentials of the weblogic server, which is why it's trying to create an .ssh directory under %systemroot%\system32\config\systemprofile.

Try suppressing this by adding -o CheckHostIP=no to the sftp command line.

schtever
  • 3,210
  • 16
  • 25
  • It's a longer weekend will try the option once get my hands on the system. Thanks for you quick replies – user3427939 Apr 28 '19 at 06:11
  • Tried: sftp.exe -o CheckHostIP=no -b D:\path\inctf2.txt user_name@ip_address >> D:\path\ftps\%par3%%para4%.log 2>&1 Resulted in same error message in logs. – user3427939 Apr 30 '19 at 05:42