Hello team,

In my program i use jscraft.JSch.jar

Thru this pgm i enter into a remote machine and execute commands.
after execution of each commands, i need to read the output.

after that i have to close the channel and session.

but it is not working. Also it does not give any exception.
i have attached the code.

please help in this regard.
-- 
TS KarthiKeyan
Guindy, Chennai - 600032
import java.io.InputStream;
import java.io.PrintStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class OAMLocal {
	private static Session session = null;
	private static JSch jsch = new JSch();
	private static Channel channel = null;
	private static InputStream in;
	private static PrintStream out;

	public static void executeScript_telnet(String ip,String username,String password,String staticip,String call) throws Exception
	{ 
		System.out.println("OAMCE: Inside executeScript_telnet: "+ip);

		UserInfo ui=new MyUserInfo(password);
		try{ 
		session=jsch.getSession(username,ip,22);}
		catch(Exception e)
		{
			System.out.println("OAMCE: Could not connect to "+ip+". Session failed.\n"+e.toString());
			
		}
		session.setUserInfo(ui);
		try{
		session.connect();}
		catch(Exception e)
		{
			System.out.println("OAMCE: Could not connect to "+ip+". Session connection failed.\n"+e.toString());
			
		}
		System.out.println("OAMCE: After session connect");
		try{
		channel = session.openChannel("shell");}
		catch(Exception e)
		{
			session.disconnect();
			System.out.println("OAMCE: Could not connect to "+ip+". Channel open failed.\n"+e.toString());
			
		}
		try{	
		System.out.println("OAMCE: Telnet Command : "+ip);
		channel.setInputStream(System.in);
		channel.setOutputStream(System.out);
		channel.connect();}		
		catch(Exception e)
		{
			System.out.println("OAMCE: Coming Exception after channel.connect()");
			session.disconnect();
			System.out.println("OAMCE: Could not connect to "+ip+". Channel connect failed.\n"+e.toString());
			
		}
		try{	
		in = channel.getInputStream();
		out = new PrintStream( channel.getOutputStream() );
		}
		catch( Exception e )
		{
			 session.disconnect();
			 System.out.println("OAMCE: Could not connect to "+ip+". Wrong Password.\n"+e.toString());
			 
		}	
			
			 if(call.compareTo("sys_read") == 0)
			 {
				 System.out.println("OAMCE: %%%%%%%Going to Call SYSTEM readUntil()%%%%%%%");
				 readUntil_sys(staticip);
			 }
//			 if(call.compareTo("oracle_read") == 0)
//			 {
//				 System.out.println("OAMCE: $$$$$$Going to Call ORACLE readUntil()$$$$$$$");
//				 readUntil_oracle(staticip);
////				 oracle_mode = true;
//			 }	
//			 if(call.compareTo("reboot") == 0)
//			 {
//				 System.out.println("OAMCE: $$$$$$Going to Call reboot()$$$$$$$");
//				 readUntil_reboot(staticip);
//			 }
	    channel.disconnect();
	    session.disconnect();
//	     return mode;
	}
	
	public static String readUntil_sys(String staticip) throws Exception
	{
		System.out.println("OAMCE: Inside readUntil....and Call for SYSTEM RESTORE"); 
		byte[] tmp=new byte[1024];
//		System.out.println("OAMCE: In readUntil System Command..." + in.available());
		String out="";
		int res_success = 0;
		int j;
		boolean backup = false;
		String ip = "/usr/bin/telnet "+ staticip;
		while(true)
		{
//			System.out.println("OAMCE: In side True..." + in.available());
			while(in.available()>0)	       	
			{	        	  
				int i=in.read(tmp, 0, 1024);
				if(i<0)break;
				System.out.print(new String(tmp, 0, i));
				out = new String(tmp,0,i);
				String temp[] = out.split("\n");            		
//				for(i=0;i<temp.length;i++){
//				for(j=0;j<temp.length;j++){
				j=(temp.length - 1);
//				System.out.println(temp[j] + " j value "+j);           			
				if(temp[j].indexOf("@localhost]")!= -1)
				{
					write(ip);
//					System.out.println("OAMCE: Successfully written telnet"); 
//					Thread.sleep(10000);
					break;
				}
				
				if(temp[j].indexOf("login:")!= -1)
				{
//					System.out.println("OAMCE: Successfully logged in"); 
					write("root");
					break;
//					System.out.println("OAMCE: Successfully written username");            		  		 
				}
				if(temp[j].indexOf("Password:")!= -1)
				{
//					System.out.println("OAMCE: Successfully logged in"); 
					write("softswitch");
					break;
//					System.out.println("OAMCE: Successfully written password");            		  		
				}
				
				if(temp[j].indexOf(":~#")!= -1)
				{    
					if(!backup){
//						System.out.println("OAMCE: Successfully logged in going to Write for system");
						write("/opt/ALAsps/sphwm/BackRest/bin/backup.ksh backup system");
						backup=true;
						break;
					}
				}            			
				if(temp[j].indexOf("OAM not in MMM mode")!=-1)
				{
//					System.out.println("OAMCE: ####OAM not in Maintenance mode######");
					channel.disconnect();
					session.disconnect();
					break;
				}
				if(temp[j].indexOf("Are you sure you want to restore")!=-1)
				{
//					System.out.println("OAMCE: Restore command executed successfully & confirm???");
					write("y");
//					System.out.println("OAMCE: Restore Operation Confirmed for system");             				
					res_success = 1;
					Thread.sleep(6000);
					break;
				}            			
				if(backup && temp[j].indexOf(":~#")!=-1)
				{
					System.out.println("OAMCE: !!!!!!!!!Backup Operation Success for SYSTEM!!!!!!!!");
					write("exit");
					System.out.println("channel disconnect " + channel.isClosed() + channel.isConnected());
					channel.disconnect();
//					System.out.println("channel disconnect " + channel.isClosed() + channel.isConnected());
					break;
				}            			
				if(temp[j].indexOf("Directory doesn't contain required data")!=-1 )
				{
//					System.out.println("OAMCE: Directory doesn't contain required data");
					break;
				}
				
//				}//forloop           		
			}
			//System.out.println("OAMCE: In side True..channel.isClosed." + channel.isClosed());
			
			if(backup){
			channel.disconnect();
			System.out.println("channel disconnect " + channel.isClosed() + channel.isConnected());
			}

			if(channel.isClosed())
//			if(backup)
			{
//				System.out.println("OAMCE: readUntil-sys(exit-status): "+channel.getExitStatus());
//				session.disconnect();
				break;
			}         
			try{Thread.sleep(5000);}catch(Exception ee){ee.printStackTrace();}
			
//			System.out.println("I am still running expecting output");
			
		}
		return out;
	}
	
	public static void write( String value )
	  {
	       try
	       {    
	    	   out.println( value );    	   
	           out.flush();
	           System.out.println("Written in terminal " + value);
	       }
	       catch( Exception e )
	       {
	    	   System.out.println(e.toString());
	       }
	  }
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		executeScript_telnet("172.21.132.205","mgc15user01","user01","172.16.100.0","sys_read");
//		executeScript_telnet("172.21.132.201","root","softswitch","172.16.100.1","sys_read");
	}
	public static class MyUserInfo implements UserInfo
	   {
	         String passwd ;
	         MyUserInfo(String pwd)
	         {
	             this.passwd = pwd;
	         }

	             public String getPassword()
	             {
	                 return passwd;
	             }
	             public boolean promptYesNo(String str)
	             {
	                 return true;
	             }
	             public String getPassphrase()
	             {
	                return null;
	             }
	             public boolean promptPassphrase(String message)
	             {
	                return true;
	             }
	             public boolean promptPassword(String message)
	             {
	                 return true;
	             }
	             public void showMessage(String message){}             

	   } 
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to