public static void Connect() { try { var jsch = new JSch(); _session = jsch.getSession(Settings.SSHUsername, Settings.SSHHost, Settings.SSHPort); _session.setHost(Settings.SSHHost); _session.setPassword(Settings.SSHPassword); UserInfo ui = new MyUserInfo(Settings.SSHPassword); _session.setUserInfo(ui); _session.connect(); int port; if (!int.TryParse(Settings.Port, out port)) port = 3306; _session.setPortForwardingL(Settings.SSHLocalPort, "localhost", port); if (!_session.isConnected()) Enabled = false; } catch (Exception ex) { Enabled = false; Trace.WriteLine(ex.Message + " at ssh connect."); Disconnect(); } }
public SshHelper(string Host, string UserName, string Password) { host = Host; var jsch = new JSch(); session = jsch.getSession(UserName, host, 22); session.setPassword(Password); var config = new Hashtable { { "StrictHostKeyChecking", "no" } }; session.setConfig(config); session.connect(); channel = (ChannelShell)session.openChannel("shell"); writer_po = new PipedOutputStream(); var writer_pi = new PipedInputStream(writer_po); var reader_pi = new PipedInputStream(); var reader_po = new PipedOutputStream(reader_pi); reader = new StreamReader(reader_pi, Encoding.UTF8); channel.setInputStream(writer_pi); channel.setOutputStream(reader_po); channel.connect(); channel.setPtySize(132, 132, 1024, 768); }
/// <summary> /// Constructs a new SSH stream. /// </summary> /// <param name="host">The hostname or IP address of the remote SSH machine</param> /// <param name="username">The name of the user connecting to the remote machine</param> /// <param name="password">The password of the user connecting to the remote machine</param> public SshStream(string host, string username, string password) { this.m_host = host; JSch jsch=new JSch(); m_session=jsch.getSession(username, host, 22); m_session.setPassword( password ); Hashtable config=new Hashtable(); config.Add("StrictHostKeyChecking", "no"); m_session.setConfig(config); m_session.connect(); m_channel=(ChannelShell)m_session.openChannel("shell"); m_in = m_channel.getInputStream(); m_out = m_channel.getOutputStream(); m_channel.connect(); m_channel.setPtySize(80, 132, 1024, 768); Prompt = "\n"; m_escapeCharPattern = "\\[[0-9;?]*[^0-9;]"; }
/// <summary> /// This class creates a jsch instance using the host that user wants. This makes it possible to /// forward the MySql Database port to a user's local port. /// </summary> public string jschServer(string host, string user, string password) { try { // Create a new JSch instance JSch jsch = new JSch(); // Saves the info to make other sessions this.host = host; this.user = user; this.password = password; // Create a new SSH session session = jsch.getSession(user, password, port); session.setHost(host); ; session.setPassword(password); // Creates a userinfo instance to pass into the session UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.connect(); return null; } catch (Exception ex) { session.disconnect(); return ex.Message; } }
internal bool SSHConnect() { try { channels_ = new Dictionary<int, ChannelSftp>(); jsch_ = new JSch(); Hashtable config = new Hashtable(); config["StrictHostKeyChecking"] = "no"; if (identity_ != null) jsch_.addIdentity(identity_, passphrase_); session_ = jsch_.getSession(user_, host_, port_); session_.setConfig(config); session_.setUserInfo(new DokanUserInfo(password_, passphrase_)); session_.setPassword(password_); session_.connect(); return true; } catch (Exception e) { Debug(e.ToString()); return false; } }
protected override void OnStart(string[] args) { bool ontWorkerInstantiated = false; if (Repository.Configuration.Processes == null) return; if (Repository.Configuration.Database.SSH.Enabled) { try { //Create a new SSH session _jsch = new JSch(); _sshSession = _jsch.getSession( Repository.Configuration.Database.SSH.UserID, Repository.Configuration.Database.SSH.Host, Repository.Configuration.Database.SSH.Port); _sshSession.setHost(Repository.Configuration.Database.SSH.Host); _sshSession.setPassword(Repository.Configuration.Database.SSH.Password); UserInfo ui = new JschUserInfo(); _sshSession.setUserInfo(ui); // Connect _sshSession.connect(); //Set port forwarding on the opened session _sshSession.setPortForwardingL( Repository.Configuration.Database.SSH.LocalPort, Repository.Configuration.Database.SSH.ForwardingHost, Repository.Configuration.Database.SSH.RemotePort); if (!_sshSession.isConnected()) throw new Exception("SSH Session did not connect."); } catch (Exception ex) { EventLogWriter.WriteError("Could not start due to SSH Error:\n{0}", ex); return; } } foreach (TextMinerServiceSettingsProcess process in Repository.Configuration.Processes) { if (!process.Enabled) continue; switch (process.Type) { case ProcessType.OntologySubsetWorker: { // Only one thread of this type allowed if (ontWorkerInstantiated == true) continue; process.Worker = new Worker.OntologySubset(process.PollingInterval, process.Timeout, process.ResponseTimeout); ontWorkerInstantiated = true; break; } case ProcessType.PubMed: { process.Worker = new Worker.PubMed(process.PollingInterval, process.Timeout, process.PostPollingInterval, process.ResponseTimeout, process.OntogratorTab); break; } case ProcessType.Pubget: { process.Worker = new Worker.Pubget(process.PollingInterval, process.Timeout, process.PostPollingInterval, process.ResponseTimeout, process.OntogratorTab); break; } case ProcessType.ClinicalTrialsGov: { process.Worker = new Worker.ClinicalTrialsGov(process.PollingInterval, process.Timeout, process.PostPollingInterval, process.ResponseTimeout, process.OntogratorTab); break; } default: { continue; } } process.Thread = new Thread(new ThreadStart(process.Worker.Start)); process.Thread.Start(); } }
protected void OpenSSH() { try { //Create a new SSH session _jsch = new JSch(); _sshSession = _jsch.getSession( Settings.SSHUserID, Settings.SSHHost, int.Parse(Settings.SSHPort)); _sshSession.setHost(Settings.SSHHost); _sshSession.setPassword(Settings.SSHPassword); UserInfo ui = new JschUserInfo(); _sshSession.setUserInfo(ui); // Connect _sshSession.connect(); //Set port forwarding on the opened session _sshSession.setPortForwardingL( int.Parse(Settings.SSHLocalPort), Settings.SSHForwardingHost, int.Parse(Settings.SSHRemotePort)); if (!_sshSession.isConnected()) throw new Exception("SSH Session did not connect."); } catch (Exception ex) { MessageBox.Show(string.Format("Could not start due to SSH Error:\n{0}", ex)); return; } }
/// <summary> /// Initiates a new SCP response on sourceforge.net for downloading a file. /// </summary> /// <param name="uri">URI to download (includes username and password)</param> /// <param name="timeout">Timeout for this session</param> public ScpWebResponse(Uri uri, int timeout) { JSch jsch = new JSch(); string[] userPass = uri.UserInfo.Split(':'); if (userPass.Length != 2) { throw new WebException("Username and password information for sourceforge.net incomplete"); } session = jsch.getSession(userPass[0], "frs.sourceforge.net", 22); // username and password will be given via UserInfo interface. //UserInfo ui = new UserInfo(); //session.setUserInfo(ui); session.setPassword(userPass[1]); Hashtable hastable = new Hashtable(); hastable.put("StrictHostKeyChecking", "no"); session.setConfig(hastable); if (DbManager.Proxy != null) { session.setProxy(new ProxyHTTP(DbManager.Proxy.Address.Host, DbManager.Proxy.Address.Port)); } try { session.connect(timeout); } catch (JSchException e) { if (e.Message == "Auth fail") { throw new WebException("Invalid username or password for sourceforge"); } throw; } // exec 'scp -f rfile' remotely string sfPath = GetSourceforgePath(uri.LocalPath); // Determine file modified date ChannelSftp channelSftp = (ChannelSftp)session.openChannel("sftp"); channelSftp.connect(); try { SftpATTRS attrs = channelSftp.lstat(sfPath); this.lastModified = RpcApplication.UnixToDotNet(attrs.getMTime()); } catch (SftpException) { throw new WebException("The file \"" + sfPath + "\" could not be found."); } finally { channelSftp.disconnect(); } String command = "scp -f " + sfPath.Replace(" ", "\\ "); Channel channel = session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); // get I/O streams for remote scp Stream outs = channel.getOutputStream(); Stream ins = channel.getInputStream(); channel.connect(); byte[] buf = new byte[1024]; // send '\0' buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush(); int c = checkAck(ins); if (c != 'C') { return; } // read '0644 ' ins.Read(buf, 0, 5); while (true) { ins.Read(buf, 0, 1); if (buf[0] == ' ') break; this.contentLength = this.contentLength * 10 + (buf[0] - '0'); } String file = null; for (int i = 0; ; i++) { ins.Read(buf, i, 1); if (buf[i] == (byte)0x0a) { file = Util.getString(buf, 0, i); break; } } this.responseUri = new Uri("scp://" + session.getHost() + sfPath); // send '\0' buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush(); this.responseStream = ins; }
public static bool Connect(string thost,string tuser,string tpass,string tport) { try { JSch jsch = new JSch(); host = thost; user = tuser; pass = tpass; sshPort = Convert.ToInt32(tport); session = jsch.getSession(user, host, sshPort); session.setHost(host); session.setPassword(pass); UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.connect(); session.setPortForwardingL(lPort, "127.0.0.1", rPort); return true; } catch (Exception ex) { error = ex; return false; } }