How to Make Ssh Command Execution to Timeout

How to set ssh timeout?

ssh -o ConnectTimeout=10  <hostName>

Where 10 is time in seconds. This Timeout applies only to the creation of the connection.

how to make SSH command execution to timeout

You could wrap the call to ssh using the timeout command. The timeout command exits with code 124 if a timeout occurs.

timeout 10s ssh -q harigm@8.19.71.238 exit
if [ $? -eq 124 ]; then
echo "Timeout out"
fi

Or, as Vorsprung has commented on your question (as I was looking up the man page!):

ssh -oPasswordAuthentication=no -q harigm@8.19.71.238 exit

which will disallow interactive password authentication. You'd then have to check the exit code.

How to set timeout for a ssh command executed with php?

Wrap your command with the UNIX timeout utility.

system('timeout n ../my/aa');
^

Where n is some integer value in seconds.

If the command times out, then exit with status 124. Otherwise, exit
with the status of COMMAND. If no signal is specified, send the TERM
signal upon timeout. The TERM signal kills any process that does not
block or catch that signal. For other processes, it may be necessary
to use the KILL (9) signal, since this signal cannot be caught.

Timeout when ssh command is executed from my Java program

I have abandoned my attempts at using ssh libraries for this and have instead used a bare-bones Runtime.exec() approach for issuing ssh and scp commands. Here's the code that I'm now using which is working well:

public static void executeSecureCommand(final String user,
final String remoteHost,
final String command)
{
// basic validation of the parameters
if ((user == null) || user.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the ssh command \'" + command +
"\': the supplied user name parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}
if ((remoteHost == null) || remoteHost.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the ssh command \'" + command +
"\': the supplied remote host parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}
if ((command == null) || command.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the ssh command: the supplied command parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}

// create and execute a corresponding ssh command
String sshCommand = "ssh " + user + "@" + remoteHost + " " + command;
try
{
executeShellCommand(sshCommand);
}
catch (Exception ex)
{
// log the error and throw an exception
String errorMessage = "Error executing the secure shell command \'" + sshCommand + "\'";
log.error(errorMessage, ex);
throw new LifecycleException(errorMessage, ex);
}
}

public static void executeSecureFileCopy(final String localFilePathName,
final String user,
final String remoteHost,
final String remoteFilePathName)
{
// basic validation of the parameters
if ((localFilePathName == null) || localFilePathName.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the secure copy -- the supplied local file path name parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}
if ((user == null) || user.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the secure copy -- the supplied user name parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}
if ((remoteHost == null) || remoteHost.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the secure copy -- the supplied remote host parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}
if ((remoteFilePathName == null) || remoteFilePathName.isEmpty())
{
// log the error and throw an exception
String errorMessage = "Error executing the secure copy -- the supplied remote file path name parameter is null or empty.";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}

try
{
// create an scp command we'll use to perform the secure file copy
String scpCommand = "scp -B -C -q " + localFilePathName + " " + user + "@" + remoteHost + ":" +
remoteFilePathName;

// execute the scp command
executeShellCommand(scpCommand);
}
catch (Exception ex)
{
// log the error and throw an exception
String errorMessage = "Failed to copy local file \'" + localFilePathName + "\' to remote host:file \'" +
remoteHost + ":" + remoteFilePathName + "\'.";
log.error(errorMessage, ex);
throw new LifecycleException(errorMessage, ex);
}
}

public static void executeShellCommand(final String command)
{
try
{
// create and execute a runtime process which runs the command
Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", command });

// gobble the input stream
InputStream processInputStream = process.getInputStream();
BufferedReader processInputStreamReader = new BufferedReader(new InputStreamReader(processInputStream));
String inputStreamLine = processInputStreamReader.readLine();
while (inputStreamLine != null)
{
inputStreamLine = processInputStreamReader.readLine();
}

// capture the error stream
InputStream processErrorStream = process.getErrorStream();
BufferedReader processErrorStreamReader = new BufferedReader(new InputStreamReader(processErrorStream));
String errorStreamLine = processErrorStreamReader.readLine();
StringBuffer errorBuffer = new StringBuffer();
while (errorStreamLine != null)
{
errorBuffer.append(errorStreamLine);
errorStreamLine = processErrorStreamReader.readLine();
}

// close the streams
processInputStream.close();
processErrorStream.close();

// wait for the process to finish and return the exit code
process.waitFor();
if (process.exitValue() != 0)
{
// log the error and throw an exception
String errorMessage = "Failed to execute the shell command \'" + command + "\' -- Error: \'" +
errorBuffer.toString() + "\'";
log.error(errorMessage);
throw new LifecycleException(errorMessage);
}
}
catch (Exception ex)
{
// log the error and throw an exception
String errorMessage = "Failed to execute the shell command \'" + command + "\'.";
log.error(errorMessage, ex);
throw new LifecycleException(errorMessage, ex);
}
}

If anyone sees issues with this code, i.e. possible errors I'm not catching, etc. then please point them out. My original code was quite complicated and convoluted in order to address all of the issues pointed out in this article (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html) but only recently was it pointed out to me that that article is almost 8 years old and that many of the issues it warns about are no longer applicable in current releases of Java. So I'm back to a basic solution which uses Runtime.exec() and all seems well.

Thanks again to everyone who tried to help me solve this problem.

Remote script execution using ssh timeout after 10 minutes

ConnectTimeout specifies the timeout (in seconds) used when connecting to the SSH server

You have to set ServerAliveInterval instead. To set it for all users you have to modify /etc/ssh/ssh_config (having root grants). To enable just your user modify ~/.ssh/config

Host *
ServerAliveInterval 3600
ServerAliveCountMax 2

If you can modify the server side, you can make your server keeps alive all connections with clients by adding the following to /etc/ssh/sshd_config:

ClientAliveInterval 3600
ClientAliveCountMax 2

The timeout command does not work when reading from /dev/tcp/$server/ssh, how can I make this timeout command work?

$ server="google.com"; timeout=20; 

$ time timeout "$timeout" bash -c "</dev/tcp/${server}/22" || echo "WARNING: Could not connect to $server on ssh."

real 0m20.002s
user 0m0.003s
sys 0m0.000s
WARNING: Could not connect to google.com on ssh.

Using netcat (nc)

$ time nc -z -w "$timeout" "$server" 22 || echo "WARNING: Could not connect to $server on ssh."

real 0m20.059s
user 0m0.003s
sys 0m0.001s
WARNING: Could not connect to google.com on ssh.

how to decrease ssh connection timeout value

if you call ssh script you can use something like that

ssh -o ConnectTimeout=10  <hostName>

where 10 is the number of seconds



Related Topics



Leave a reply



Submit