c# - SFTP in Windows Service -



c# - SFTP in Windows Service -

i need execute sftp process through c# code. using windows service. below code using.

webcash.writetofile(webcash.outerbatchpath, command); webcash.writetofile(webcash.outerbatchpath, "exit"); processstartinfo psi = new processstartinfo("cmd.exe", "/c "+webcash.outerbatchpath); psi.redirectstandardoutput = true; psi.redirectstandardinput = true; psi.redirectstandarderror = true; psi.useshellexecute = false; psi.createnowindow = true; psi.windowstyle = processwindowstyle.hidden; process p = new process(); p.startinfo = psi; p.start(); p.waitforexit(); psi = null; webcash.writetofile(webcash.geterrorlog(), datetime.now.tostring() + "- running sftp end - " + p.exitcode); homecoming p.exitcode;

while trying debug above code debugger not able pass p.waitforexit() function(i.e) service not executing after p.waitforexit().

i running below command(sftp) in outerbatch.

psftp -b d:\source_data\sftp\innerbatch.bat -l username -i d:\privatekey.ppk @servername

inner batch contains below command

put d:\source_data\sftp\filetotransfer.xml

i need exit code error. pls help.

i assume programme running ("outer batch") never finishes.

when redirect process output, must reading it. there's limited buffer redirected output. when fills, kid process stops on next effort produce output , waits until parent process reads info buffer. never , wait process exit, deadlock occurs.

see msdn processstartinfo.redirectstandardoutput property :

synchronous read operations introduce dependency between caller reading standardoutput stream , kid process writing stream. these dependencies can cause deadlock conditions. when caller reads redirected stream of kid process, dependent on child. caller waits read operation until kid writes stream or closes stream. when kid process writes plenty info fill redirected stream, dependent on parent. kid process waits next write operation until parent reads total stream or closes stream. deadlock status results when caller , kid process wait each other finish operation, , neither can continue. can avoid deadlocks evaluating dependencies between caller , kid process.

it's recommended utilize p.standardoutput.readtoend() instead of p.waitforexit() in case. both commands wait process exit, first ensures deadlock not occur.

on other hand, transferring 1 file, i'm not sure psftp output big plenty fill buffer. if above suggestion not help, i'd recommend simplify code straight running psftp.exe, removing unnecessary layers of cmd.exe , outer batch file.

edit: if never run psftp under service account, perchance prompting host key confirmation.

the server's host key not cached in registry. you have no guarantee server computer you think is. server's dss key fingerprint is: ssh-dss 1024 0b:77:8b:68:f4:45:b1:3c:87:ad:5c:be:3b:c5:72:78 if trust host, come in "y" add together key to putty's cache , carry on connecting. if want carry on connecting once, without adding key cache, come in "n". if not trust host, press homecoming abandon the connection. store key in cache? (y/n)

make sure transfer host key cache interactive business relationship registry service business relationship registry. it's in [hkey_current_user\software\simontatham\putty\sshhostkeys]

alternatively utilize sftp client can verify host key script or improve utilize sftp .net library.

for illustration winscp .net library, can utilize sessionoptions.sshhostkeyfingerprint.

side note: naming psftp script .bat confusing. it's not (windows) batch file.

c# windows-services sftp

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -