當前位置: 首頁>>代碼示例>>C#>>正文


C# JSch.getSession方法代碼示例

本文整理匯總了C#中Tamir.SharpSsh.jsch.JSch.getSession方法的典型用法代碼示例。如果您正苦於以下問題:C# JSch.getSession方法的具體用法?C# JSch.getSession怎麽用?C# JSch.getSession使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Tamir.SharpSsh.jsch.JSch的用法示例。


在下文中一共展示了JSch.getSession方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: RunExample

		public static void RunExample(String[] arg)
		{
			try
			{
				//Create a new JSch instance
				JSch jsch=new JSch();
			
				//Prompt for username and server host
				Console.WriteLine("Please enter the user and host info at the popup window...");
				String host = InputForm.GetUserInput
					("Enter [email protected]",
					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);

				//Add AES128 as default cipher in the session config store
				System.Collections.Hashtable config=new System.Collections.Hashtable();
				config.Add("cipher.s2c", "aes128-cbc,3des-cbc");
				config.Add("cipher.c2s", "aes128-cbc,3des-cbc");
				session.setConfig(config);

				//Connect to remote SSH server
				session.connect();			

				//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);
			}
		}
開發者ID:stux2000,項目名稱:dokan,代碼行數:60,代碼來源:AES.cs

示例2: Connect

 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);
     }
 }
開發者ID:rogerluo,項目名稱:testcsharp,代碼行數:30,代碼來源:TestShell.cs

示例3: SshHelper

        public SshHelper(string host, string username, string password)
        {
            this.host = host;
            JSch jsch=new JSch();
            Session session=jsch.getSession(username, host, 22);
            session.setPassword( password );

            Hashtable config=new Hashtable();
            config.Add("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            channel=(ChannelShell)session.openChannel("shell");

            writer_po = new PipedOutputStream();
            PipedInputStream writer_pi = new PipedInputStream( writer_po );

            PipedInputStream reader_pi = new PipedInputStream();
            PipedOutputStream 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);
        }
開發者ID:yash0924,項目名稱:csharputils,代碼行數:28,代碼來源:SshHelper.cs

示例4: Run

		public static void Run()
		{
			JSch jsch=new JSch();
			Session session=jsch.getSession("root", "rhmanage", 22);
			session.setPassword( "cisco" );

			Hashtable config=new Hashtable();
			config.Add("StrictHostKeyChecking", "no");
			session.setConfig(config);

			session.connect();
			
				Channel channel=session.openChannel("exec"); 
				((ChannelExec)channel).setCommand("ifconfig");

				StreamReader sr = new StreamReader( channel.getInputStream() );

				channel.connect();

				string line;

				while( (line=sr.ReadLine()) != null )
				{
					Console.WriteLine( line );
				}		

				channel.disconnect();
				session.disconnect();
		}
開發者ID:MatanDavidCohen,項目名稱:StockAnalyzerWin,代碼行數:29,代碼來源:ExecTest.cs

示例5: Connect

        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();
            }
        }
開發者ID:Chaplain,項目名稱:WowPacketParser,代碼行數:26,代碼來源:SSHTunnel.cs

示例6: SshHelper

        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);
        }
開發者ID:gamchantoi,項目名稱:astra-contact-manager,代碼行數:27,代碼來源:SshHelper.cs

示例7: RunExample

		public static void RunExample(String[] arg)
		{
			try
			{
				JSch jsch=new JSch();

				//Get the "known hosts" filename from the user
				Console.WriteLine("Please choose your private key file...");
				String file = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
				Console.WriteLine("You chose "+file+".");

				//Add the identity file to JSch
				jsch.addIdentity(file);

				//Prompt for username and server host
				Console.WriteLine("Please enter the user and host info at the popup window...");
				String host = InputForm.GetUserInput
					("Enter [email protected]",
					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();

				//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();

				//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);
			}
		}
開發者ID:stux2000,項目名稱:dokan,代碼行數:58,代碼來源:UserAuthPubKey.cs

示例8: SftpHelper

 /// <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);
 }
開發者ID:rexyanglucky,項目名稱:uba,代碼行數:18,代碼來源:SftpHelper.cs

示例9: RunExample

		public static void RunExample(String[] arg)
		{
			int port;

			try
			{
				//Create a new JSch instance
				JSch jsch=new JSch();

				//Prompt for username and server host
				Console.WriteLine("Please enter the user and host info at the popup window...");
				String host = InputForm.GetUserInput
					("Enter [email protected]",
					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);
				session.connect();

				//Get from user the remote host and remote host port
				String foo = InputForm.GetUserInput("Enter host and port", "host:port");
				host=foo.Substring(0, foo.IndexOf(':'));
				port=int.Parse(foo.Substring(foo.IndexOf(':')+1));

				Console.WriteLine("System.{in,out} will be forwarded to "+
					host+":"+port+".");
				Channel channel=session.openChannel("direct-tcpip");
				((ChannelDirectTCPIP)channel).setInputStream(Console.OpenStandardInput());
				((ChannelDirectTCPIP)channel).setOutputStream(Console.OpenStandardOutput());
				((ChannelDirectTCPIP)channel).setHost(host);
				((ChannelDirectTCPIP)channel).setPort(port);
				channel.connect();

				while(!channel.isClosed())
				{
					System.Threading.Thread.Sleep(500);
				}
				channel.disconnect();
				session.disconnect();
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
			}
		}
開發者ID:stux2000,項目名稱:dokan,代碼行數:51,代碼來源:StreamForwarding.cs

示例10: _Connect

		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 = "";
		}
開發者ID:soywiz,項目名稱:csharputils,代碼行數:15,代碼來源:SftpFileSystem.cs

示例11: RunExample

		public static void RunExample(String[] arg)
		{
			//int port;

			try
			{
				//Create a new JSch instance
				JSch jsch=new JSch();

				//Prompt for username and server host
				Console.WriteLine("Please enter the user and host info at the popup window...");
				String host = InputForm.GetUserInput
					("Enter [email protected]",
					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);

				//Get from user the local port, remote host and remote host port
				String foo = InputForm.GetUserInput("Enter -L port:host:hostport","port:host:hostport");
				int lport=int.Parse(foo.Substring(0, foo.IndexOf(':')));
				foo=foo.Substring(foo.IndexOf(':')+1);
				String rhost=foo.Substring(0, foo.IndexOf(':'));
				int rport=int.Parse(foo.Substring(foo.IndexOf(':')+1));

				// username and password will be given via UserInfo interface.
				UserInfo ui=new MyUserInfo();
				session.setUserInfo(ui);
				session.connect();

				Console.WriteLine("localhost:"+lport+" -> "+rhost+":"+rport);

				//Set port forwarding on the opened session
				session.setPortForwardingL(lport, rhost, rport);			
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
			}
		}
開發者ID:TonyZhu2015,項目名稱:sharpssh,代碼行數:42,代碼來源:PortForwardingL.cs

示例12: SshStream

		/// <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;]";
		}
開發者ID:MatanDavidCohen,項目名稱:StockAnalyzerWin,代碼行數:29,代碼來源:SshStream.cs

示例13: Launch

		///<summary>Returns true if the communications were successful, and false if they failed. Both sends and retrieves.</summary>
		public static bool Launch(Clearinghouse clearhouse,int batchNum) {
			clearinghouse=clearhouse;
			//Before this function is called, the X12 file for the current batch has already been generated in
			//the clearinghouse export folder. The export folder will also contain batch files which have failed
			//to upload from previous attempts and we must attempt to upload these older batch files again if
			//there are any.
			//Step 1: Retrieve reports regarding the existing pending claim statuses.
			//Step 2: Send new claims in a new batch.
			bool success=true;
			//Connect to the Denti-Cal SFTP server.
			Session session=null;
			Channel channel=null;
			ChannelSftp ch=null;
			JSch jsch=new JSch();
			try {
				session=jsch.getSession(clearinghouse.LoginID,remoteHost);
				session.setPassword(clearinghouse.Password);
				Hashtable config=new Hashtable();
				config.Add("StrictHostKeyChecking","no");
				session.setConfig(config);
				session.connect();
				channel=session.openChannel("sftp");
				channel.connect();
				ch=(ChannelSftp)channel;
			}
			catch(Exception ex) {
				MessageBox.Show(Lan.g("DentiCal","Connection Failed")+": "+ex.Message);
				return false;
			}
			try {
				string homeDir="/Home/"+clearhouse.LoginID+"/";
				//At this point we are connected to the Denti-Cal SFTP server.
				if(batchNum==0) { //Retrieve reports.
					if(!Directory.Exists(clearhouse.ResponsePath)) {
						throw new Exception("Clearinghouse response path is invalid.");
					}
					//Only retrieving reports so do not send new claims.
					string retrievePath=homeDir+"out/";
					Tamir.SharpSsh.java.util.Vector fileList=ch.ls(retrievePath);
					for(int i=0;i<fileList.Count;i++) {
						string listItem=fileList[i].ToString().Trim();
						if(listItem[0]=='d') {
							continue;//Skip directories and focus on files.
						}
						Match fileNameMatch=Regex.Match(listItem,".*\\s+(.*)$");
						string getFileName=fileNameMatch.Result("$1");
						string getFilePath=retrievePath+getFileName;
						string exportFilePath=CodeBase.ODFileUtils.CombinePaths(clearhouse.ResponsePath,getFileName);
						Tamir.SharpSsh.java.io.InputStream fileStream=null;
						FileStream exportFileStream=null;
						try {
							fileStream=ch.get(getFilePath);
							exportFileStream=File.Open(exportFilePath,FileMode.Create,FileAccess.Write);//Creates or overwrites.
							byte[] dataBytes=new byte[4096];
							int numBytes=fileStream.Read(dataBytes,0,dataBytes.Length);
							while(numBytes>0) {
								exportFileStream.Write(dataBytes,0,numBytes);
								numBytes=fileStream.Read(dataBytes,0,dataBytes.Length);
							}
						}
						catch {
							success=false;
						}
						finally {
							if(exportFileStream!=null) {
								exportFileStream.Dispose();
							}
							if(fileStream!=null) {
								fileStream.Dispose();
							}
						}
						if(success) {
							//Removed the processed report from the Denti-Cal SFTP so it does not get processed again in the future.
							try {
								ch.rm(getFilePath);
							}
							catch {
							}
						}
					}
				}
				else { //Send batch of claims.
					if(!Directory.Exists(clearhouse.ExportPath)) {
						throw new Exception("Clearinghouse export path is invalid.");
					}
					string[] files=Directory.GetFiles(clearhouse.ExportPath);
					for(int i=0;i<files.Length;i++) {
						//First upload the batch file to a temporary file name. Denti-Cal does not process file names unless they start with the Login ID.
						//Uploading to a temporary file and then renaming the file allows us to avoid partial file uploads if there is connection loss.
						string tempRemoteFilePath=homeDir+"in/temp_"+Path.GetFileName(files[i]);
						ch.put(files[i],tempRemoteFilePath);
						//Denti-Cal requires the file name to start with the Login ID followed by a period and end with a .txt extension.
						//The middle part of the file name can be anything.
						string remoteFilePath=homeDir+"in/"+clearhouse.LoginID+"."+Path.GetFileName(files[i]);
						ch.rename(tempRemoteFilePath,remoteFilePath);
						File.Delete(files[i]);//Remove the processed file.
					}
				}
			}
//.........這裏部分代碼省略.........
開發者ID:romeroyonatan,項目名稱:opendental,代碼行數:101,代碼來源:DentiCal.cs

示例14: RunExample

		public static void RunExample(String[] arg)
		{
			try
			{
				//Create a new JSch instance
				JSch jsch=new JSch();
			
				//Prompt for username and server host
				Console.WriteLine("Please enter the user and host info at the popup window...");
				
				String host = InputForm.GetUserInput("Enter [email protected]",
											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);
				
				String proxy=InputForm.GetUserInput("Enter proxy server",
					"hostname:port");

				string proxy_host=proxy.Substring(0, proxy.IndexOf(':'));
				int proxy_port=int.Parse(proxy.Substring(proxy.IndexOf(':')+1));

				session.setProxy(new ProxyHTTP(proxy_host, proxy_port));

				// username and password will be given via UserInfo interface.
				UserInfo ui=new MyUserInfo();
				session.setUserInfo(ui);

				//Connect to remote SSH server
				session.connect();			

				//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);
			}
		}
開發者ID:TonyZhu2015,項目名稱:sharpssh,代碼行數:63,代碼來源:ViaHTTP.cs

示例15: Main

        public static void Main(String[] arg)
        {
            if(arg.Length!=2)
            {
                Console.WriteLine("usage: java ScpFrom [email protected]:file1 file2");
                return;
            }

            try
            {
                String user=arg[0].Substring(0, arg[0].IndexOf('@'));
                arg[0]=arg[0].Substring(arg[0].IndexOf('@')+1);
                String host=arg[0].Substring(0, arg[0].IndexOf(':'));
                String rfile=arg[0].Substring(arg[0].IndexOf(':')+1);
                String lfile=arg[1];

                String prefix=null;
                if(Directory.Exists(lfile))
                {
                    prefix=lfile+Path.DirectorySeparatorChar;
                }

                JSch jsch=new JSch();
                Session session=jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui=new MyUserInfo();
                session.setUserInfo(ui);
                session.connect();

                // exec 'scp -f rfile' remotely
                String command="scp -f "+rfile;
                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();

                while(true)
                {
                    int c=checkAck(ins);
                    if(c!='C')
                    {
                        break;
                    }

                    // read '0644 '
                    ins.Read(buf, 0, 5);

                    int filesize=0;
                    while(true)
                    {
                        ins.Read(buf, 0, 1);
                        if(buf[0]==' ')break;
                        filesize=filesize*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;
                        }
                    }

                    //Console.WriteLine("filesize="+filesize+", file="+file);

                    // send '\0'
                    buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();

                    // read a content of lfile
                    FileStream fos=File.OpenWrite(prefix==null ?
                    lfile :
                        prefix+file);
                    int foo;
                    while(true)
                    {
                        if(buf.Length<filesize) foo=buf.Length;
                        else foo=filesize;
                        ins.Read(buf, 0, foo);
                        fos.Write(buf, 0, foo);
                        filesize-=foo;
                        if(filesize==0) break;
                    }
                    fos.Close();

                    byte[] tmp=new byte[1];

                    if(checkAck(ins)!=0)
//.........這裏部分代碼省略.........
開發者ID:darbio,項目名稱:MTSharpSSH,代碼行數:101,代碼來源:ScpFrom.cs


注:本文中的Tamir.SharpSsh.jsch.JSch.getSession方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。