public void request(Session session, Channel channel) { Buffer buf=new Buffer(); Packet packet=new Packet(buf); bool reply=waitForReply(); if(reply) { channel.reply=-1; } packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(Util.getBytes("subsystem")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(Util.getBytes("sftp")); session.write(packet); if(reply) { while(channel.reply==-1) { try{System.Threading.Thread.Sleep(10);} catch//(Exception ee) { } } if(channel.reply==0) { throw new JSchException("failed to send sftp request"); } } }
public void request(Session session, Channel channel) { Buffer buf=new Buffer(); Packet packet=new Packet(buf); packet.reset(); buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString( Util.getBytes("signal")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(Util.getBytes(signal)); session.write(packet); }
public void request(Session session, Channel channel) { Buffer buf=new Buffer(); Packet packet=new Packet(buf); // send // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "shell" // boolean want reply // 0 packet.reset(); buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(Util.getBytes("shell")); buf.putByte((byte)(waitForReply() ? 1 : 0)); session.write(packet); }
//连接SFTP public bool Connect() { try { if (!Connected) { m_session.connect(); m_channel = m_session.openChannel("sftp"); m_channel.connect(); m_sftp = (ChannelSftp)m_channel; } return true; } catch { return false; } }
public void request(Session session, Channel channel) { Packet packet=session.packet; Buffer buf=session.buf; // send // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "exec" // boolean want reply // 0 // string command packet.reset(); buf.putByte((byte) Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(new Str("exec").getBytes()); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(new Str(command).getBytes()); session.write(packet); }
public void request(Session session, Channel channel) { Buffer buf=new Buffer(); Packet packet=new Packet(buf); packet.reset(); buf.WriteByte((byte) Session.SSH_MSG_CHANNEL_REQUEST); buf.WriteInt(channel.getRecipient()); buf.WriteString(Util.getBytes("pty-req")); buf.WriteByte((byte)(waitForReply() ? 1 : 0)); buf.WriteString(Util.getBytes("vt100")); buf.WriteInt(80); buf.WriteInt(24); buf.WriteInt(640); buf.WriteInt(480); buf.WriteString(Util.getBytes("")); session.write(packet); }
//连接SFTP public bool Connect() { try { if (!Connected) { m_session.connect(); m_channel = m_session.openChannel("sftp"); m_channel.connect(); m_sftp = (ChannelSftp)m_channel; } return true; } catch (Exception ex) { Console.WriteLine("host:" + m_session.getHost() + "port:" + m_session.getPort()); Console.WriteLine(ex.Message); throw new Exception(ex.Message + "\r\n" + "host:" + m_session.getHost() + "port:" + m_session.getPort()); //return false; } }
//Conexión SFTP public bool Connect() { try { if (!Connected) { m_session.connect(); m_channel = m_session.openChannel("sftp"); m_channel.connect(); m_sftp = (ChannelSftp)m_channel; } return(true); } catch { return(false); } }
public void request(Session session, Channel channel) { Buffer buf=new Buffer(); Packet packet=new Packet(buf); //byte SSH_MSG_CHANNEL_REQUEST //uint32 recipient_channel //string "window-change" //boolean FALSE //uint32 terminal width, columns //uint32 terminal height, rows //uint32 terminal width, pixels //uint32 terminal height, pixels packet.reset(); buf.WriteByte((byte) Session.SSH_MSG_CHANNEL_REQUEST); buf.WriteInt(channel.getRecipient()); buf.WriteString(Util.getBytes("window-change")); buf.WriteByte((byte)(waitForReply() ? 1 : 0)); buf.WriteInt(width_columns); buf.WriteInt(height_rows); buf.WriteInt(width_pixels); buf.WriteInt(height_pixels); session.write(packet); }
public void request(Session session, Channel channel) { Buffer buf=new Buffer(); Packet packet=new Packet(buf); // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "x11-req" // boolean want reply // 0 // boolean single connection // string x11 authentication protocol // "MIT-MAGIC-COOKIE-1". // string x11 authentication cookie // uint32 x11 screen number packet.reset(); buf.WriteByte((byte) Session.SSH_MSG_CHANNEL_REQUEST); buf.WriteInt(channel.getRecipient()); buf.WriteString(Util.getBytes("x11-req")); buf.WriteByte((byte)(waitForReply() ? 1 : 0)); buf.WriteByte((byte)0); buf.WriteString(Util.getBytes("MIT-MAGIC-COOKIE-1")); buf.WriteString(ChannelX11.getFakedCookie(session)); buf.WriteInt(0); session.write(packet); }
internal void addChannel(Channel channel) { channel.session=this; }
/// <summary> /// Stream results from the channel as ASCII strings. Only the /// "standard output" stream will be checked, not the "error" stream. /// Results will be streamed in approximately 1K chunks. /// </summary> /// <param name="chan">SSH Channel to use. Should NOT be connected yet. Will be disconnected /// when streaming finishes.</param> /// <returns></returns> protected IEnumerator<string> StreamASCII(Channel chan) { if (chan == null) throw new ArgumentNullException("chan"); if (chan.connected) throw new InvalidOperationException("The channel must not be connected when StreamASCII is called."); Stream s = chan.getInputStream(); chan.connect(); try { var fn = StreamResults(s); byte[] buff; while ((buff = fn()) != null) yield return Encoding.ASCII.GetString(buff); } finally { chan.disconnect(); } }
public void request(Session session, Channel channel, String subsystem, bool want_reply) { this.subsystem = subsystem; this.want_reply = want_reply; this.request(session, channel); }
public void write(Packet packet, Channel c, int length) { while(true) { if(in_kex) { try{Thread.Sleep(10);} catch(ThreadInterruptedException e){}; continue; } lock(c) { if(c.rwsize>=length) { c.rwsize-=length; break; } } if(c._close || !c.isConnected()) { throw new IOException("channel is broken"); } bool sendit=false; int s=0; byte command=0; int recipient=-1; lock(c) { if(c.rwsize>0) { int len=c.rwsize; if(len>length) { len=length; } if(len!=length) { s=packet.shift(len, (c2smac!=null ? c2smac.getBlockSize() : 0)); } command=packet.buffer.buffer[5]; recipient=c.getRecipient(); length-=len; c.rwsize-=len; sendit=true; } } if(sendit) { _write(packet); if(length==0) { return; } packet.unshift(command, recipient, s, length); lock(c) { if(c.rwsize>=length) { c.rwsize-=length; break; } } } try{Thread.Sleep(100);} catch(ThreadInterruptedException e){}; } _write(packet); }
/*lock*/ public void write(Packet packet, Channel c, int length) { while(true) { if(c.rwsize>=length) { c.rwsize-=length; break; } if(c._close || !IsConnected()) { throw new IOException("channel is broken"); } if(c.rwsize>0) { int len=c.rwsize; int s=packet.shift(len, (c2smac!=null ? c2smac.getBlockSize() : 0)); byte command=packet.buffer.buffer[5]; int recipient=c.getRecipient(); length-=len; c.rwsize=0; write(packet); packet.unshift(command, recipient, s, length); } try{System.Threading.Thread.Sleep(10);} catch{}; } write(packet); }
/// <summary> /// Connect a channel to the remote server using the 'SCP From' command ('scp -f') /// </summary> /// <param name="channel">Will contain the new connected channel</param> /// <param name="server">Will contaun the new connected server I/O stream</param> /// <param name="rfile">The remote path on the server</param> /// <param name="recursive">Idicate a recursive scp transfer</param> protected void SCP_ConnectFrom(out Channel channel, out Stream server, string rfile, bool recursive) { string scpCommand = "scp -f "; if(recursive) scpCommand += "-r "; scpCommand += "\""+rfile+"\""; channel = (ChannelExec)m_session.openChannel(ChannelType); ((ChannelExec)channel).setCommand(scpCommand); server = new Tamir.Streams.CombinedStream (channel.getInputStream(), channel.getOutputStream()); channel.connect(); //SCP_CheckAck(server); }
internal static void del(Channel c) { lock(pool) { pool.removeElement(c); } }
//断开SFTP public bool Disconnect() { bool result = false; if (Connected) { m_channel.disconnect(); m_channel = null; m_session.disconnect(); m_session = null; result = true; } return result; }
internal static void eof(Session session) { Channel[] channels=null; int count=0; lock(pool){ channels=new Channel[pool.Count]; for(int i=0; i<pool.Count; i++){ try{ Channel c=((Channel)(pool[i])); if(c.session==session){ channels[count++]=c; } } catch{ } } } for(int i=0; i<count; i++){ channels[i].eof(); } }
internal static void disconnect(Session session) { Channel[] channels=null; int count=0; lock(pool) { channels=new Channel[pool.size()]; for(int i=0; i<pool.size(); i++) { try { Channel c=((Channel)(pool.elementAt(i))); if(c.session==session) { channels[count++]=c; } } catch(Exception e) { } } } for(int i=0; i<count; i++) { channels[i].disconnect(); } }
internal static void del(Channel c) { lock(pool){ pool.Remove(c); } }
/// <summary> /// Get intermediate results as ASCII strings. Only the /// "standard output" stream will be checked, not the "error" stream. /// Results will be collected until the separator byte is seen, /// at which time a string will be yielded. The separator /// byte will be included at the end of each string (except possibly /// the last). /// </summary> /// <param name="sep">A value to separate strings by (e.g., 0x10 for newline). </param> /// <param name="chan">SSH Channel to use. Should NOT be connected yet. Will be disconnected /// when streaming finishes.</param> /// <returns></returns> protected IEnumerator<string> StreamASCII(byte sep, Channel chan) { if (chan == null) throw new ArgumentNullException("chan"); if (chan.connected) throw new InvalidOperationException("The channel must not be connected when StreamASCII is called."); Stream s = null; try { s = chan.getInputStream(); chan.connect(); var res = new StringBuilder(); var fn = StreamResults(s); byte[] buff; while ((buff = fn()) != null) { int upTo; if ((upTo = Array.IndexOf(buff, sep) + 1) != 0) { if (res.Length > 0) { res.Append(Encoding.ASCII.GetString(buff, 0, upTo)); yield return res.ToString(); res.Length = 0; } else yield return Encoding.ASCII.GetString(buff, 0, upTo); if (upTo < buff.Length) { int currUpTo = 0; while ((currUpTo = Array.IndexOf(buff, sep, upTo) + 1) != 0) { yield return Encoding.ASCII.GetString(buff, upTo, currUpTo - upTo); upTo = currUpTo; } if (upTo < buff.Length) res.Append(Encoding.ASCII.GetString(buff, upTo, buff.Length - upTo)); } } else res.Append(Encoding.ASCII.GetString(buff, 0, buff.Length)); } if (res.Length > 0) yield return res.ToString(); } finally { chan.disconnect(); } }
private void Conectar() { JSch jsch = new JSch(); Hashtable propiedades = new Hashtable(); this.session=jsch.getSession(this.user, this.host, this.port); this.session.setPassword(this.psw); this.session.setTimeout(this.timeOut); propiedades.Add("password", this.psw); propiedades.Add("StrictHostKeyChecking", "no"); this.session.setConfig(propiedades); this.session.connect(); this.canal = session.openChannel("sftp"); this.canal.connect(); this.canalsftp = (ChannelSftp)canal; }
/// <summary> /// Closes the SSH subsystem /// </summary> public virtual void Close() { if (m_channel != null) { m_channel.disconnect(); m_channel = null; } if (m_session != null) { m_session.disconnect(); m_session = null; } }
protected virtual void ConnectChannel() { m_channel = m_session.openChannel(ChannelType); this.OnChannelReceived(); m_channel.connect(); this.OnConnected(); }