private static void knownHosts(JSch sch) { DirectoryInfo home = FS.userHome(); if (home == null) return; var known_hosts = new FileInfo(Path.Combine(home.ToString(), ".ssh/known_hosts")); try { using (var s = new StreamReader(known_hosts.FullName)) { sch.setKnownHosts(s); } } catch (FileNotFoundException) { // Oh well. They don't have a known hosts in home. } catch (IOException) { // Oh well. They don't have a known hosts in home. } }
public virtual void FtpPoll(string fileName, int timeout, Dictionary<string, string> config) { fileName = fileName + ".asc"; var printxml = config["printxml"] == "true"; if (printxml) { Console.WriteLine("Polling for outbound result file. Timeout set to " + timeout + "ms. File to wait for is " + fileName); } ChannelSftp channelSftp; var url = config["sftpUrl"]; var username = config["sftpUsername"]; var password = config["sftpPassword"]; var knownHostsFile = config["knownHostsFile"]; var jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); var session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); var channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e); } //check if file exists SftpATTRS sftpAttrs = null; var stopWatch = new Stopwatch(); stopWatch.Start(); do { if (printxml) { Console.WriteLine("Elapsed time is " + stopWatch.Elapsed.TotalMilliseconds); } try { sftpAttrs = channelSftp.lstat("outbound/" + fileName); if (printxml) { Console.WriteLine("Attrs of file are: " + sftpAttrs); } } catch (SftpException e) { if (printxml) { Console.WriteLine(e.message); } System.Threading.Thread.Sleep(30000); } } while (sftpAttrs == null && stopWatch.Elapsed.TotalMilliseconds <= timeout); }
public virtual void FtpDropOff(string fileDirectory, string fileName, Dictionary<string, string> config) { ChannelSftp channelSftp; var url = config["sftpUrl"]; var username = config["sftpUsername"]; var password = config["sftpPassword"]; var knownHostsFile = config["knownHostsFile"]; var filePath = fileDirectory + fileName; var printxml = config["printxml"] == "true"; if (printxml) { Console.WriteLine("Sftp Url: " + url); Console.WriteLine("Username: "******"Password: "******"Known hosts file path: " + knownHostsFile); } var jsch = new JSch(); if (printxml) { // grab the contents fo the knownhosts file and print var hostFile = File.ReadAllText(knownHostsFile); Console.WriteLine("known host contents: " + hostFile); } jsch.setKnownHosts(knownHostsFile); // setup for diagnostic // Get the KnownHosts repository from JSchs var hkr = jsch.getHostKeyRepository(); var hks = hkr.getHostKey(); HostKey hk; if (printxml) { // Print all knownhosts and keys if (hks != null) { Console.WriteLine(); Console.WriteLine("Host keys in " + hkr.getKnownHostsRepositoryID() + ":"); foreach (var t in hks) { hk = t; Console.WriteLine("local HostKey host: <" + hk.getHost() + "> type: <" + hk.getType() + "> fingerprint: <" + hk.getFingerPrint(jsch) + ">"); } Console.WriteLine(""); } } var session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); // more diagnostic code for troubleshooting sFTP connection errors if (printxml) { // Print the host key info of the connected server: hk = session.getHostKey(); Console.WriteLine("remote HostKey host: <" + hk.getHost() + "> type: <" + hk.getType() + "> fingerprint: <" + hk.getFingerPrint(jsch) + ">"); } var channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e); } catch (JSchException e) { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e); } try { if (printxml) { Console.WriteLine("Dropping off local file " + filePath + " to inbound/" + fileName + ".prg"); } channelSftp.put(filePath, "inbound/" + fileName + ".prg", ChannelSftp.OVERWRITE); if (printxml) { Console.WriteLine("File copied - renaming from inbound/" + fileName + ".prg to inbound/" + fileName + ".asc"); } channelSftp.rename("inbound/" + fileName + ".prg", "inbound/" + fileName + ".asc"); } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to upload and save the file to SFTP", e); } channelSftp.quit(); session.disconnect(); }
private static void knownHosts(JSch sch) { DirectoryInfo home = FS.userHome(); if (home == null) return; FileInfo known_hosts = new FileInfo(Path.Combine(home.ToString(), ".ssh/known_hosts")); try { FileStream s = new FileStream(known_hosts.ToString(), System.IO.FileMode.Open, FileAccess.Read); try { sch.setKnownHosts(new StreamReader(s)); } finally { s.Close(); } } catch (FileNotFoundException) { } catch (IOException) { } }
public virtual void FtpPickUp(string destinationFilePath, Dictionary<string, string> config, string fileName) { ChannelSftp channelSftp; var printxml = config["printxml"] == "true"; var url = config["sftpUrl"]; var username = config["sftpUsername"]; var password = config["sftpPassword"]; var knownHostsFile = config["knownHostsFile"]; var jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); var session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); var channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e); } try { if (printxml) { Console.WriteLine("Picking up remote file outbound/" + fileName + ".asc"); Console.WriteLine("Putting it at " + destinationFilePath); } channelSftp.get("outbound/" + fileName + ".asc", destinationFilePath); if (printxml) { Console.WriteLine("Removing remote file output/" + fileName + ".asc"); } channelSftp.rm("outbound/" + fileName + ".asc"); } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to retrieve and save the file from SFTP", e); } channelSftp.quit(); session.disconnect(); }
public static void RunExample(String[] arg) { try { //Get the "known hosts" filename from the user Console.WriteLine("Please select your 'known_hosts' from the poup window..."); String file = InputForm.GetFileFromUser("Choose your known_hosts(ex. ~/.ssh/known_hosts)"); Console.WriteLine("You chose "+file+"."); //Create a new JSch instance JSch jsch=new JSch(); //Set the known hosts file jsch.setKnownHosts(file); //Get the KnownHosts repository from JSchs HostKeyRepository hkr=jsch.getHostKeyRepository(); //Print all known hosts and keys HostKey[] hks=hkr.getHostKey(); HostKey hk; if(hks!=null) { Console.WriteLine(); Console.WriteLine("Host keys in "+hkr.getKnownHostsRepositoryID()+":"); for(int i=0; i<hks.Length; i++) { hk=hks[i]; Console.WriteLine(hk.getHost()+" "+ hk.getType()+" "+ hk.getFingerPrint(jsch)); } Console.WriteLine(""); } //Now connect to the remote server... //Prompt for username and server host Console.WriteLine("Please enter the user and host info at the popup window..."); String host = InputForm.GetUserInput ("Enter username@hostname", Environment.UserName+"@localhost"); String user=host.Substring(0, host.IndexOf('@')); host=host.Substring(host.IndexOf('@')+1); //Create a new SSH session Session session=jsch.getSession(user, host, 22); // username and password will be given via UserInfo interface. UserInfo ui=new MyUserInfo(); session.setUserInfo(ui); //Connect to remote SSH server session.connect(); //Print the host key info //of the connected server: hk=session.getHostKey(); Console.WriteLine("HostKey: "+ hk.getHost()+" "+ hk.getType()+" "+ hk.getFingerPrint(jsch)); //Open a new Shell channel on the SSH session Channel channel=session.openChannel("shell"); //Redirect standard I/O to the SSH channel channel.setInputStream(Console.OpenStandardInput()); channel.setOutputStream(Console.OpenStandardOutput()); //Connect the channel channel.connect(); Console.WriteLine("-- Shell channel is connected using the {0} cipher", session.getCipher()); //Wait till channel is closed while(!channel.isClosed()) { System.Threading.Thread.Sleep(500); } //Disconnect from remote server channel.disconnect(); session.disconnect(); } catch(Exception e) { Console.WriteLine(e.Message); } }
public virtual void FtpDropOff(string fileDirectory, string fileName, Dictionary<String, String> config) { ChannelSftp channelSftp = null; Channel channel; string url = config["sftpUrl"]; string username = config["sftpUsername"]; string password = config["sftpPassword"]; string knownHostsFile = config["knownHostsFile"]; string filePath = fileDirectory + fileName; JSch jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); Session session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { if (e.message != null) { throw new LitleOnlineException(e.message); } else { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection"); } } catch (JSchException e) { if (e.Message != null) { throw new LitleOnlineException(e.Message); } else { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection"); } } try { channelSftp.put(filePath, "inbound/" + fileName, ChannelSftp.OVERWRITE); channelSftp.rename("inbound/" + fileName, "inbound/" + fileName + ".asc"); } catch (SftpException e) { if (e.message != null) { throw new LitleOnlineException(e.message); } else { throw new LitleOnlineException("Error occured while attempting to upload and save the file to SFTP"); } } channelSftp.quit(); session.disconnect(); }
public virtual void FtpPoll(string fileName, int timeout, Dictionary<string, string> config) { ChannelSftp channelSftp = null; Channel channel; string url = config["sftpUrl"]; string username = config["sftpUsername"]; string password = config["sftpPassword"]; string knownHostsFile = config["knownHostsFile"]; JSch jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); Session session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { if (e.message != null) { throw new LitleOnlineException(e.message); } else { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection"); } } //check if file exists SftpATTRS sftpATTRS = null; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); do { try { sftpATTRS = channelSftp.lstat("outbound/" + fileName); } catch { } } while (sftpATTRS == null && stopWatch.Elapsed.TotalMilliseconds <= timeout); }
public virtual void FtpPickUp(string destinationFilePath, Dictionary<String, String> config, string fileName) { ChannelSftp channelSftp = null; Channel channel; string url = config["sftpUrl"]; string username = config["sftpUsername"]; string password = config["sftpPassword"]; string knownHostsFile = config["knownHostsFile"]; JSch jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); Session session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { if (e.message != null) { throw new LitleOnlineException(e.message); } else { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection"); } } try { channelSftp.get("outbound/" + fileName + ".asc", destinationFilePath); channelSftp.rm("outbound/" + fileName + ".asc"); } catch (SftpException e) { if (e.message != null) { throw new LitleOnlineException(e.message); } else { throw new LitleOnlineException("Error occured while attempting to retrieve and save the file from SFTP"); } } channelSftp.quit(); session.disconnect(); }
public virtual void FtpDropOff(string fileDirectory, string fileName, Dictionary<String, String> config) { ChannelSftp channelSftp = null; Channel channel; string url = config["sftpUrl"]; string username = config["sftpUsername"]; string password = config["sftpPassword"]; string knownHostsFile = config["knownHostsFile"]; string filePath = fileDirectory + fileName; bool printxml = config["printxml"] == "true"; if (printxml) { Console.WriteLine("Sftp Url: " + url); Console.WriteLine("Username: "******"Password: "******"Known hosts file path: " + knownHostsFile); } JSch jsch = new JSch(); jsch.setKnownHosts(knownHostsFile); Session session = jsch.getSession(username, url); session.setPassword(password); try { session.connect(); channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp)channel; } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection",e); } catch (JSchException e) { throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e); } try { if (printxml) { Console.WriteLine("Dropping off local file " + filePath + " to inbound/" + fileName + ".prg"); } channelSftp.put(filePath, "inbound/" + fileName + ".prg", ChannelSftp.OVERWRITE); if (printxml) { Console.WriteLine("File copied - renaming from inbound/" + fileName + ".prg to inbound/" + fileName + ".asc"); } channelSftp.rename("inbound/" + fileName + ".prg", "inbound/" + fileName + ".asc"); } catch (SftpException e) { throw new LitleOnlineException("Error occured while attempting to upload and save the file to SFTP", e); } channelSftp.quit(); session.disconnect(); }