Inheritance: Runnable
		internal static byte[] getFakedCookie(Session session)
		{
			lock(faked_cookie_hex_pool)
			{
				byte[] foo=(byte[])faked_cookie_hex_pool[session];
				if(foo==null)
				{
					Random random=Session.random;
					foo=new byte[16];
					lock(random)
					{
						random.fill(foo, 0, 16);
					}
					/*
					System.out.print("faked_cookie: ");
					for(int i=0; i<foo.length; i++){
						System.out.print(Integer.toHexString(foo[i]&0xff)+":");
					}
					System.out.println("");
					*/
					faked_cookie_pool.Add(session, foo);
					byte[] bar=new byte[32];
					for(int i=0; i<16; i++)
					{
						bar[2*i]=table[(foo[i]>>4)&0xf];
						bar[2*i+1]=table[(foo[i])&0xf];
					}
					faked_cookie_hex_pool.Add(session, bar);
					foo=bar;
				}
				return foo;
			}
		}
Esempio n. 2
0
 public void Connect()
 {
     InitVAHInfo();
     try
     {
         JSch jsch = new JSch();
         _ssn = jsch.getSession(_usr, _hip, _hp);
         System.Collections.Hashtable hashConfig = new Hashtable();
         hashConfig.Add("StrictHostKeyChecking", "No");
         _ssn.setConfig(hashConfig);
         jsch.addIdentity(_ppk);
         _ssn.connect();
         if (_ssn.isConnected())
         {
             Console.WriteLine("Log Successfully.");
         }
         else
         {
             Console.WriteLine("Log failed.");
         }
     }
     catch (Tamir.SharpSsh.jsch.JSchException jschex)
     {
         Console.WriteLine(jschex.Message);
     }
     catch (Exception anyex)
     {
         Console.WriteLine(anyex.Message);
     }
 }
Esempio n. 3
0
		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");
				}
			}
		}
Esempio n. 4
0
        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();
            }
        }
Esempio n. 5
0
        //Host: SFTP: nombre de usuario contraseña de usuario: Dirección de pwd

        public SFTPHelper(string host, string user, string pwd)
        {
            string[] arr = host.Split(':');

            string ip = arr[0];

            int port = 22;

            if (arr.Length > 1)
            {
                port = Int32.Parse(arr[1]);
            }



            JSch jsch = new JSch();

            m_session = jsch.getSession(user, ip, port);

            MyUserInfo ui = new MyUserInfo();

            ui.setPassword(pwd);

            m_session.setUserInfo(ui);
        }
		internal static PortWatcher getPort(Session session, String address, int lport)
		{
			InetAddress addr;
			try
			{
				addr=InetAddress.getByName(address);
			}
			catch(Exception uhe)
			{
				throw new JSchException("PortForwardingL: invalid address "+address+" specified.");
			}
			lock(pool)
			{
				for(int i=0; i<pool.size(); i++)
				{
					PortWatcher p=(PortWatcher)(pool.elementAt(i));
					if(p.session==session && p.lport==lport)
					{
						if(p.boundaddress.isAnyLocalAddress() ||
							p.boundaddress.equals(addr))
							return p;
					}
				}
				return null;
			}
		}
        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);
        }
Esempio n. 8
0
		internal static PortWatcher getPort(Session session, String address, int lport)
		{
			IPAddress addr;
			try
			{
				addr = Dns.GetHostEntry(address).AddressList[0];
			}
			catch(Exception)
			{
				throw new JSchException("PortForwardingL: invalid address "+address+" specified.");
			}
			lock(pool)
			{
				for(int i=0; i<pool.Count; i++)
				{
					PortWatcher p=(PortWatcher)(pool[i]);
					if(p.session==session && p.lport==lport)
					{

						if (
							IPAddress.IsLoopback(p.boundaddress) ||
							p.boundaddress == addr
						)
						{
							return p;
						}
					}
				}
				return null;
			}
		}
 protected override void configure(OpenSshConfig.Host hc, Session session)
 {
     if (!hc.isBatchMode())
     {
     #warning need something to replace jgit gui infrastructure as gitsharp is library only
         throw new NotImplementedException("GUI Configuration is not available");
     }
 }
Esempio n. 10
0
        public void releaseSession(Session session)
        {
            if (session == null)
                throw new System.ArgumentNullException ("session");

            if (session.isConnected())
                session.disconnect();
        }
Esempio n. 11
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="host"></param>
 /// <param name="user"></param>
 /// <param name="pwd"></param>
 public SftpHelper(string host, string user, string pwd)
 {
     string[] arr = host.Split(':');
     string ip = arr[0];
     int port = 22;
     if (arr.Length > 1) port = Int32.Parse(arr[1]);
     JSch jsch = new JSch();
     m_session = jsch.getSession(user, ip, port);
     MyUserInfo ui = new MyUserInfo();
     ui.setPassword(pwd);
     m_session.setUserInfo(ui);
 }
Esempio n. 12
0
		public override void init(Session session,
			byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C) 
		{
			this.session=session;
			this.V_S=V_S;      
			this.V_C=V_C;      
			this.I_S=I_S;      
			this.I_C=I_C;      

			//    sha=new SHA1();
			//    sha.init();
			try
			{
				Type t=Type.GetType(session.getConfig("sha-1"));
				sha=(HASH)(Activator.CreateInstance(t));
				sha.init();
			}
			catch(Exception ee)
			{
				Console.WriteLine(ee);
			}

			buf=new Buffer();
			packet=new Packet(buf);

			try
			{
				Type t=Type.GetType(session.getConfig("dh"));
				dh=(DH)(Activator.CreateInstance(t));
				dh.init();
			}
			catch(Exception ee)
			{
				throw ee;
			}

			dh.setP(p);
			dh.setG(g);

			// The client responds with:
			// byte  SSH_MSG_KEXDH_INIT(30)
			// mpint e <- g^x mod p
			//         x is a random number (1 < x < (p-1)/2)

			e=dh.getE();

			packet.reset();
			buf.putByte((byte)SSH_MSG_KEXDH_INIT);
			buf.putMPInt(e);
			session.write(packet);

			state=SSH_MSG_KEXDH_REPLY;
		}
Esempio n. 13
0
		internal static Channel getChannel(int id, Session session)
		{
			lock(pool)
			{
				for(int i=0; i<pool.Count; i++)
				{
					Channel c=(Channel)(pool[i]);
					if(c.id==id && c.session==session) return c;
				}
			}
			return null;
		}
Esempio n. 14
0
		internal static Channel getChannel(int id, Session session)
		{
			lock(pool)
			{
				for(int i=0; i<pool.size(); i++)
				{
					Channel c=(Channel)(pool.elementAt(i));
					if(c.id==id && c.session==session) return c;
				}
			}
			return null;
		}
		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);
		}
Esempio n. 16
0
		protected void _Connect()
		{
			_jsch = new JSch();
			//session.setConfig();
			_session = _jsch.getSession(this.Username, this.Host, this.Port);
			UserInfo ui = new DirectPasswordUserInfo(this.Password);
			_session.setUserInfo(ui);
			_session.connect();

			_csftp = (ChannelSftp)_session.openChannel("sftp");
			_csftp.connect();

			//RootPath = csftp.getHome();
			RootPath = "";
		}
Esempio n. 17
0
 public void Connect(string User, string Password, string Host, int Port)
 {
     this.Host = Host;
     this.Username = User;
     this.Password = Password;
     this.Port = Port;
     this.session = this.handler.getSession(this.Username, this.Host, this.Port);
     this.session.setHost(this.Host);
     this.session.setPassword(this.Password);
     this.session.setUserInfo(this.UInfo);
     Hashtable config = new Hashtable();
     config.Add("StrictHostKeyChecking", "no");
     this.session.setConfig(config); 
     this.session.connect();
 }
Esempio n. 18
0
		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);
		}
Esempio n. 19
0
        //private byte[] f;
        public override void init(Session session,
            byte[] V_S, byte[] V_C, byte[] I_S, byte[] I_C)
        {
            this.session = session;
            this.V_S = V_S;
            this.V_C = V_C;
            this.I_S = I_S;
            this.I_C = I_C;

            //    sha=new SHA1();
            //    sha.init();

            try
            {
                Type t = Type.GetType(session.getConfig("sha-1"));
                sha = (HASH) (Activator.CreateInstance(t));
                sha.init();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            buf = new Buffer();
            packet = new Packet(buf);

            try
            {
                Type t = Type.GetType(session.getConfig("dh"));
                dh = (DH) (Activator.CreateInstance(t));
                dh.init();
            }
            catch (Exception e)
            {
                throw e;
            }

            packet.reset();
            buf.putByte(0x22);
            buf.putInt(min);
            buf.putInt(preferred);
            buf.putInt(max);
            session.write(packet);

            state = SSH_MSG_KEX_DH_GEX_GROUP;
        }
Esempio n. 20
0
        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);
        }
		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);
		}
Esempio n. 22
0
		public virtual bool start(Session session)
		{
			Packet packet=session.packet;
			Buffer buf=session.buf;
			// send
			// byte      SSH_MSG_SERVICE_REQUEST(5)
			// string    service name "ssh-userauth"
			packet.reset();
			buf.putByte((byte)Session.SSH_MSG_SERVICE_REQUEST);
			buf.putString(Util.getBytes("ssh-userauth"));
			session.write(packet);

			// receive
			// byte      SSH_MSG_SERVICE_ACCEPT(6)
			// string    service name
			buf=session.read(buf);
			//System.out.println("read: 6 ? "+buf.buffer[5]);
			return buf.buffer[5]==6;
		}
		internal static String[] getPortForwarding(Session session)
		{
			java.util.Vector foo=new java.util.Vector();
			lock(pool)
			{
				for(int i=0; i<pool.size(); i++)
				{
					PortWatcher p=(PortWatcher)(pool.elementAt(i));
					if(p.session==session)
					{
						foo.addElement(p.lport+":"+p.host+":"+p.rport);
					}
				}
			}
			String[] bar=new String[foo.size()];
			for(int i=0; i<foo.size(); i++)
			{
				bar[i]=(String)(foo.elementAt(i));
			}
			return bar;
		}
Esempio n. 24
0
        internal PortWatcher(Session session, 
            String boundaddress, int lport,
            String host, int rport)
        {
            this.session=session;
            this.boundaddress=boundaddress;
            this.lport=lport;
            this.host=host;
            this.rport=rport;
            try
            {
                //    ss=new ServerSocket(port);
                ss=new TcpListener(Dns.GetHostByName(this.boundaddress).AddressList[0], lport);

            }
            catch(Exception e)
            {
                Console.WriteLine(e);
                throw new JSchException("PortForwardingL: local port "+lport+" cannot be bound.");
            }
        }
Esempio n. 25
0
		internal static String[] getPortForwarding(Session session)
		{
			ArrayList foo = new ArrayList();
			lock(pool)
			{
				for(int i=0; i<pool.Count; i++)
				{
					PortWatcher p=(PortWatcher)(pool[i]);
					if(p.session==session)
					{
						foo.Add(p.lport + ":" + p.host + ":" + p.rport);
					}
				}
			}
			String[] bar=new String[foo.Count];
			for(int i = 0; i < foo.Count; i++)
			{
				bar[i] = (String)(foo[i]);
			}
			return bar;
		}
Esempio n. 26
0
 internal PortWatcher(Session session, 
     String address, int lport,
     String host, int rport,
     ServerSocketFactory factory)
 {
     this.session=session;
     this.lport=lport;
     this.host=host;
     this.rport=rport;
     try
     {
         boundaddress = Dns.GetHostEntry(address).AddressList[0];
         ss=(factory==null) ?
             new TcpListener(boundaddress, lport) :
             factory.createServerSocket(lport, 0, boundaddress);
     }
     catch(Exception e)
     {
         Console.WriteLine(e);
         throw new JSchException("PortForwardingL: local port "+address+":"+lport+" cannot be bound.");
     }
 }
		/// <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;]";
		}
Esempio n. 28
0
        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);
        }
Esempio n. 29
0
        public PortWatcher(Session session, String address, int lport, String host, int rport, ServerSocketFactory factory)
        {
            this.session = session;
            this.lport = lport;
            this.host = host;
            this.rport = rport;

            try
            {
                //Ignore microsoft's warning regarding "GetHostByName" being obsoleted.
                //
                //The proposed alternative, "GetHostEntry", is intended to be more generally correct (also handles IPV6), but
                // it actually does completely the WRONG thing when provided with a raw IPV4 address like "127.0.0.1": it should
                // use it as-is (at least that was the intention in this code), and instead it identifies the host and gets ALL
                // the most-specific IP addresses of the host ("localhost"), including IPV6 link-local addresses etc. So you say
                // "Bind to 127.0.0.1", and you get a listener that is actually bound to some link-local IPV6 address like
                // "fe80::84a:eaa6:244a:bde1%12" OR (if you filter out IPV6) "192.168.1.111", either way NOT binding to localhost!
                //
                // In other words, only change this if you really know what you're doing.
                //
                boundaddress = new InetAddress(
                                        Dns.GetHostEntry(address).AddressList[0]);
                                        // Dns.GetHostByName(address).AddressList[0]);

                ss = (factory == null) ?
                        new TcpListener(boundaddress.addr, lport) :
                        factory.createServerSocket(lport, 0, boundaddress);

                //In the move from custom "ServerSocket" class to standard "TcpListener", we lost the auto-start.
                ss.Start();
            }
            catch (Exception e)
            {
                throw new JSchException("PortForwardingL: local port " + address + ":" + lport + " cannot be bound.", e);
            }
        }
Esempio n. 30
0
		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 static PortWatcher addPort(Session session, String address, int lport, String host, int rport, ServerSocketFactory ssf) 
		{
			if(getPort(session, address, lport)!=null)
			{
				throw new JSchException("PortForwardingL: local port "+ address+":"+lport+" is already registered.");
			}
			PortWatcher pw=new PortWatcher(session, address, lport, host, rport, ssf);
			pool.addElement(pw);
			return pw;
		}