BAT File SFTP Scripting Issue

Advertisement

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

BAT File SFTP Scripting Issue

Hello. I have a scripting issue that has worked for a year and suddenly stopped working after the provider changed the password. Once I updated the password I can no longer connect to there server and upload a file. I am not a expert in script writing and now I need some help.
Take a look and see what is wrong.

I know it's not the remote server because I can open up WinSCP and put in the credentials and I get connected.
echo off
echo option batch abort > ftpcmd.dat
echo option confirm off >> ftpcmd.dat
echo open sftp://username:password@secureNAME.Name.com  -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx">> ftpcmd.dat
echo put \\servername\e$\Folder1\Folder2\Folder3\Folder4\Test-4-3-18-TEST.txt >> ftpcmd.dat
echo exit>>  ftpcmd.dat
"C:\Program Files (x86)\WinSCP\winscp.com" /script=ftpcmd.dat 
del ftpcmd.dat
Thanks

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: BAT File SFTP Scripting Issue

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, use /log=path_to_log_file command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

Thanks Martin.

Unfortunately, I am not a scripting expert at all. So I have no idea how to write that into the bat file. The bat file that included, where would I place line to get it to generate the logfile. Also, When I run the bat file I noticed that it gets stuck on the password part of it.

I have attached that for you to see. I know its not the logfile but thats all I have until you help me with the log command.

Thanks
Rob
  • WINSCP.JPG (18.4 KB, Private file)

Reply with quote

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

Thanks Martin. I got it to work. I have attached part of the output. It looks like it is hanging on the password but I can't tell if the script is wrong on my side or from the remote server side for accepting it.

Rob
  • WINSCP-Log.JPG (82.2 KB, Private file)

Reply with quote

Advertisement

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

HI Martin, I did not get a chance to do that but got information back from the place that where we send the file they no longer wanted us to have the password right in the bat file. They wanted us to use a private key. So I got that set up and got the bat file to work when I run it.

The new problem is that Task Scheduler will not run the file. I have no idea why. Nothing has changed other than calling for the Private Key on another drive. So I checked the permissions and made sure that the Task Scheduler username has full permissions to that folder and file. It still does not work.

That's where I am at right now.
Thanks
Rob

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

"It still does not work" is rather vague. Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, use /log=path_to_log_file command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

Good idea Martin. I put the logging command back in and ran the file once by double clicking the batch file and the second time by running the task in Task Scheduler. Running it in Task Scheduler did not like the password. So why does it run fine when I run the batch file itself, but when I run the task in Task Scheduler that is calling this file to run it does not.

Thanks
Rob
  • 5-8-2018-Combined.log (3.95 KB, Private file)
Description: There are two logs in this one file. They are labeled.

Reply with quote

Advertisement

martin
Site Admin
martin avatar

You wrote that you set up authentication using a private key. Yet none of the log show a successful authentication with a private key. In both logs the private key authentication fails and WinSCP falls back to password authentication.

Reply with quote

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

Hey Martin. I am new to all this scripting and especially using the Private and Public Keys so that is correct. When I put in the private key and ran the script from the bat file it worked. I figured that the Private key worked as well. When I switched to using the Task Scheduler to run the script it failed. As you can see from the logs. I did not know that the private key failed until I put in the log tracking again. I have no idea why its working when you run the bat file since the password was removed from the bat file. When I got the private key setup I also sent the public key to the place we are connecting to.

If you look in the log it says
Server offered these authentication methods: publickey,password
. 2018-05-08 15:17:57.261 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2018-05-08 15:17:57.261 Using stored password.
. 2018-05-08 15:17:57.261 Sent password
. 2018-05-08 15:17:57.651 Access granted
. 2018-05-08 15:17:57.651 Opening session as main channel
. 2018-05-08 15:17:57.683 Opened main channel
. 2018-05-08 15:17:57.807 Started a shell/command
To me that says that the remote server sent the password? Why would it do that. I know the private key is not working, which I have no idea why either, but it still allow it to connect.

But then in the log from when this was run from the Task Scheduler it shows.
Server offered these authentication methods: publickey,password
. 2018-05-08 15:24:59.977 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2018-05-08 15:25:10.117 Sent password
. 2018-05-08 15:25:13.518 Password authentication failed
! 2018-05-08 15:25:13.518 Access denied
. 2018-05-08 15:25:13.518 Server offered these authentication methods: publickey,password
. 2018-05-08 15:25:13.518 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2018-05-08 15:25:23.658 Disconnected: Unable to authenticate
Why the difference? Why would it allow it when you run it direct from the bat file but not when its run from the Task Scheduler?

It seems there are two issues here.
1. Why is it allowing access from one method to another
2. Why is it not accepting the private key?

Thanks
Rob

Reply with quote

gserrano
Joined:
Posts:
2
Location:
Canada

key authentication

if you are using key authentication you need to provide the public-key. You keep the private key in your system. They store the public key and that is the one you use for authentication.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

It would really help, if you posted full log files as I have asked you already.

Though it's quite probable that you have a stored site with stored password whose name matches your session URL. So WinSCP, when executed manually picks up the stored site with the password. While when executed with Task Scheduler, WinSCP has no access to the stored site, so it fails. You should use /ini=nul switch to isolate your script from GUI configuration.
See https://winscp.net/eng/docs/scripting#configuration
You are most probably even warned about this in script output and the log (in the part you didn't share).

Also see
My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?

Reply with quote

rsmiley
Joined:
Posts:
7
Location:
Pittsburgh

Hi Martin,

Sorry that you did not get the logs, but I had attached them on 5-8-2018. I will attached them again. I put them together in one file but this time I will attach them separately. I made the file private like you told me to do. I don't want others to see the file for security reasons.

Thanks
Rob
  • 5-8-2018-using-Task-Schedular.log (5.53 KB, Private file)
  • 5-8-2018-Batch-only.log (9.76 KB, Private file)

Reply with quote

Advertisement

You can post new topics in this forum