You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "sivaprasad (Jira)" <ji...@apache.org> on 2023/06/03 04:49:00 UTC

[jira] [Comment Edited] (SSHD-1328) Hanging while executing rsync commands.

    [ https://issues.apache.org/jira/browse/SSHD-1328?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17727165#comment-17727165 ] 

sivaprasad edited comment on SSHD-1328 at 6/3/23 4:48 AM:
----------------------------------------------------------

public static final Duration HEARTBEAT = Duration.ofSeconds(10L);
	public static CmdResultBean executeCommandWithMina(String hostname, String username, String password, String command) throws Exception {
		String line = null;
		SshClient client = null;
		ClientSession session = null;
		ClientChannel channel = null;
		CmdResultBean outBean = new CmdResultBean();
		List<RequestHandler<ConnectionService>> gloHandlers = null;
		BufferedReader bufferedReader = null;
		String errorop = null;
		BufferedReader errorReader = null;
		StringBuffer stringBuffer = null;
		long defaultTime = 864000000;
		int defaultport = 22;
		try 
		{
			System.out.println("----- in minassh command execution.");
			client = SshClient.setUpDefaultClient();
			client.start();
			gloHandlers = client.getGlobalRequestHandlers();
			client.setGlobalRequestHandlers(Collections.singletonList(new AbstractConnectionServiceRequestHandler() {
				@Override
				public Result process(ConnectionService connectionService, String request, boolean wantReply,
						Buffer buffer) throws Exception {
					connectionService.process(255, buffer);
					return Result.Replied;
				}
			}));
			CoreModuleProperties.HEARTBEAT_INTERVAL.set(client, HEARTBEAT);
			CoreModuleProperties.HEARTBEAT_REPLY_WAIT.set(client, Duration.ofSeconds(5L));
			session = client.connect(username, hostname, defaultport).verify(defaultTime, TimeUnit.SECONDS).getSession();
			session.addPasswordIdentity(password);
			AuthFuture authFeature = session.auth().verify(defaultTime, TimeUnit.SECONDS);
			session.setSessionHeartbeat(HeartbeatType.IGNORE, Duration.ofSeconds(5L));
			CoreModuleProperties.HEARTBEAT_INTERVAL.set(session, HEARTBEAT);
			CoreModuleProperties.HEARTBEAT_REPLY_WAIT.set(session, Duration.ofSeconds(5L));
			boolean isConnected = authFeature.isSuccess();
			if (isConnected)
			{
				channel = session.createChannel(Channel.CHANNEL_EXEC, command);
				channel.open().verify(defaultTime, TimeUnit.SECONDS);
				CoreModuleProperties.HEARTBEAT_INTERVAL.set(channel, HEARTBEAT);
				CoreModuleProperties.HEARTBEAT_REPLY_WAIT.set(channel, Duration.ofSeconds(5L));
				channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), defaultTime);
				InputStream stdout = new StreamGobbler(channel.getInvertedOut());
				bufferedReader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8));
				InputStream errorout = new StreamGobbler(channel.getInvertedErr());
				errorReader = new BufferedReader(new InputStreamReader(errorout, StandardCharsets.UTF_8));
				stringBuffer = new StringBuffer();
				while ((line = bufferedReader.readLine()) != null) {
					System.out.println(line);
					stringBuffer.append(line + "\n");
				}
				while ((errorop = errorReader.readLine()) != null) {
					System.out.println(errorop);
					stringBuffer.append(errorop + "\n");
				}
			} else 
			{
				System.out.println(" *** Unable to authenticate user.");
			}
		} catch (Exception e) {
			line = "127";
			e.printStackTrace();
			throw e;
		} finally
		{
			try
			{
				line = (channel == null || channel.getExitStatus() == null)? "127" : ""+channel.getExitStatus();
				String cmdOutput = ( stringBuffer != null && Strings.isNotEmpty(stringBuffer.toString())) ? stringBuffer.toString():outBean.setCommand(command);
				outBean.setStatus(line);
				outBean.setOutput(cmdOutput);
				System.out.println("- exit status of command : " + line);
				System.out.println("----- out from minassh command execution	-----");
				client.setGlobalRequestHandlers(gloHandlers);
				CoreModuleProperties.HEARTBEAT_INTERVAL.remove(client);
				CoreModuleProperties.HEARTBEAT_REPLY_WAIT.remove(client);
				CoreModuleProperties.HEARTBEAT_INTERVAL.remove(session);
				CoreModuleProperties.HEARTBEAT_REPLY_WAIT.remove(session);
				if (channel != null) {
					CoreModuleProperties.HEARTBEAT_INTERVAL.remove(channel);
					CoreModuleProperties.HEARTBEAT_REPLY_WAIT.remove(channel);
				}
				if (channel != null) {channel.close();}
				if (session != null) {session.close();}
				if (client != null) {client.close();}
				if (bufferedReader != null) {bufferedReader.close();}
				if (errorReader != null) {errorReader.close();}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return outBean;
	}


was (Author: JIRAUSER295690):
 
{code:java}
public static final Duration HEARTBEAT = Duration.ofSeconds(10L); public static CmdResultBean executeCommandWithMina(String hostname, String username, String password, String command) throws Exception { String line = null; SshClient client = null; ClientSession session = null; ClientChannel channel = null; CmdResultBean outBean = new CmdResultBean(); List<RequestHandler<ConnectionService>> gloHandlers = null; BufferedReader bufferedReader = null; String errorop = null; BufferedReader errorReader = null; StringBuffer stringBuffer = null; long defaultTime = 864000000; int defaultport = 22; try  { System.out.println("----- in minassh command execution."); client = SshClient.setUpDefaultClient(); client.start(); gloHandlers = client.getGlobalRequestHandlers(); client.setGlobalRequestHandlers(Collections.singletonList(new AbstractConnectionServiceRequestHandler() { @Override public Result process(ConnectionService connectionService, String request, boolean wantReply, Buffer buffer) throws Exception { connectionService.process(255, buffer); return Result.Replied; } })); CoreModuleProperties.HEARTBEAT_INTERVAL.set(client, HEARTBEAT); CoreModuleProperties.HEARTBEAT_REPLY_WAIT.set(client, Duration.ofSeconds(5L)); session = client.connect(username, hostname, defaultport).verify(defaultTime, TimeUnit.SECONDS).getSession(); session.addPasswordIdentity(password); AuthFuture authFeature = session.auth().verify(defaultTime, TimeUnit.SECONDS); session.setSessionHeartbeat(HeartbeatType.IGNORE, Duration.ofSeconds(5L)); CoreModuleProperties.HEARTBEAT_INTERVAL.set(session, HEARTBEAT); CoreModuleProperties.HEARTBEAT_REPLY_WAIT.set(session, Duration.ofSeconds(5L)); boolean isConnected = authFeature.isSuccess(); if (isConnected) { channel = session.createChannel(Channel.CHANNEL_EXEC, command); channel.open().verify(defaultTime, TimeUnit.SECONDS); CoreModuleProperties.HEARTBEAT_INTERVAL.set(channel, HEARTBEAT); CoreModuleProperties.HEARTBEAT_REPLY_WAIT.set(channel, Duration.ofSeconds(5L)); channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), defaultTime); InputStream stdout = new StreamGobbler(channel.getInvertedOut()); bufferedReader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8)); InputStream errorout = new StreamGobbler(channel.getInvertedErr()); errorReader = new BufferedReader(new InputStreamReader(errorout, StandardCharsets.UTF_8)); stringBuffer = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { System.out.println(line); stringBuffer.append(line + "\n"); } while ((errorop = errorReader.readLine()) != null) { System.out.println(errorop); stringBuffer.append(errorop + "\n"); } } else  { System.out.println(" *** Unable to authenticate user."); } } catch (Exception e) { line = "127"; e.printStackTrace(); throw e; } finally { try { line = (channel == null || channel.getExitStatus() == null)? "127" : ""+channel.getExitStatus(); String cmdOutput = ( stringBuffer != null && Strings.isNotEmpty(stringBuffer.toString())) ? stringBuffer.toString():outBean.setCommand(command); outBean.setStatus(line); outBean.setOutput(cmdOutput); System.out.println("- exit status of command : " + line); System.out.println("----- out from minassh command execution-----"); client.setGlobalRequestHandlers(gloHandlers); CoreModuleProperties.HEARTBEAT_INTERVAL.remove(client); CoreModuleProperties.HEARTBEAT_REPLY_WAIT.remove(client); CoreModuleProperties.HEARTBEAT_INTERVAL.remove(session); CoreModuleProperties.HEARTBEAT_REPLY_WAIT.remove(session); if (channel != null) { CoreModuleProperties.HEARTBEAT_INTERVAL.remove(channel); CoreModuleProperties.HEARTBEAT_REPLY_WAIT.remove(channel); } if (channel != null) { channel.close(); } if (session != null) { session.close(); } if (client != null) { client.close(); } if (bufferedReader != null) { bufferedReader.close(); } if (errorReader != null) { errorReader.close(); } } catch (Exception e) { e.printStackTrace(); } } return outBean; }{code}
 

> Hanging while executing rsync commands. 
> ----------------------------------------
>
>                 Key: SSHD-1328
>                 URL: https://issues.apache.org/jira/browse/SSHD-1328
>             Project: MINA SSHD
>          Issue Type: Bug
>         Environment: Linux and Solaris 
>            Reporter: sivaprasad
>            Priority: Blocker
>
> Hi Team,
> I hope all are doing great, I'm using Mina to execute remote commands execution and file uploads/downloads. everything is working fine, but while executing rsync commands( this command will take more than 1hr to complete) Mina is hanging forever and not returning anything. 
> I have added HEARTBEAT in my code, but Mina is printing command output after command execution is completed but I need to print command output parallel with command execution. 
> I have attached my code, can someone look into this and help me to resolve this issue’s.
>  
> Thanks,
> Siva.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org