SSH Linux commands

Connect to the robot via SSH

By default, when calling the Connect() method, the SDK connects in SSH with the default login "ur" and the password "easybot".

You can change this behavior by specifying the optional arguments enableSSH, login and password.

Once you are connected, you can check that you are connected with the property SshEnabled.

// Connect to the robot and specify login and password
_ur = new UR();
_ur.Connect("192.168.0.1", enableSSH: false);
// Remote execute your Linux commmand
// For example, execute a standard command :
var cmd = _ur.SSH.RunCommand("echo Hello > /home/ur/Desktop/NewFile.txt");
// Or run your own program and wait end :
var cmd = _ur.SSH.RunCommand("/home/ur/my-program -argument");
// Get command exit status
int status = cmd.ExitStatus;
// Get command output stream
System.IO.Stream output= cmd.OutputStream;

Enable and disable SSH connection

You can disconnect the SSH connection at any time or reconnect with the methods EnableSSH and DisableSSH.

// Connect to the robot without SSH
_ur = new UR();
_ur.Connect("192.168.0.1", enableSSH: false);
// Enable SSH later
_ur.EnableSSH("ur", "easybot");
// Disconnect from SSH
_ur.DisableSSH();

Run a Linux command

Once you are connected, you have the ``SSH'' property which contains the features.

You can execute synchronously a command via the RunCommand method.

// Execute a simple Linux command
var cmd = _ur.SSH.RunCommand("echo Hello > /home/ur/Desktop/NewFile.txt");
// Get command exit status
int status = cmd.ExitStatus;
// Get command output stream
System.IO.Stream output= cmd.OutputStream;

With BeginExecute and EndExecute, you can run asynchronous commands.

// Run your own program
var cmd2 = _ur.SSH.CreateCommand("/home/ur/my-program -argument");
// Start asynchronous execution
var asyncResult = cmd.BeginExecute();
// do things...
// Later, wait end of the command
cmd.EndExecute(asyncResult);

Remote start a shell

The creation of a shell allows to maintain a session and to have a working directory, like in a terminal. The method CreateShellStream allows to create a shell. It takes as parameter a window size. An event DataReceived is raised when the console outputs text.

// Create the shell
var shell = _ur.SSH.CreateShellStream("my terminal", 50, 10, 50, 10, 1000);
// Handle event that specifies received data
shell.DataReceived += Shell_DataReceived;
// Send a command
shell.WriteLine("ls -l");

All SSH methods and properties

Members of SSH.SshClient :
public class SshClient : BaseClient, IDisposable {
// Adds the forwarded port.
public void AddForwardedPort(ForwardedPort port),
// Creates the command to be executed.
public SshCommand CreateCommand(string commandText),
// Creates the command to be executed with specified encoding.
public SshCommand CreateCommand(string commandText, Encoding encoding),
// Creates the shell.
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput),
// Creates the shell.
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes),
// Creates the shell.
public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes, int bufferSize),
// Creates the shell.
public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput),
// Creates the shell.
public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes),
// Creates the shell.
public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes, int bufferSize),
// Creates the shell stream.
public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize),
// Creates the shell stream.
public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize, IDictionary<TerminalModes, uint> terminalModeValues),
// Releases unmanaged and - optionally - managed resources
protected override void Dispose(bool disposing),
// Gets the list of forwarded ports.
public IEnumerable<ForwardedPort> ForwardedPorts { get; },
// Stops forwarded ports.
protected override void OnDisconnected(),
// Called when client is disconnecting from the server.
protected override void OnDisconnecting(),
// Stops and removes the forwarded port from the list.
public void RemoveForwardedPort(ForwardedPort port),
// Creates and executes the command.
public SshCommand RunCommand(string commandText)
}
Members of SSH.ShellStream :
public class ShellStream : Stream, IDisposable {
// Begins the expect.
public IAsyncResult BeginExpect(AsyncCallback callback, object state, params ExpectAction[] expectActions),
// Begins the expect.
public IAsyncResult BeginExpect(AsyncCallback callback, params ExpectAction[] expectActions),
// Begins the expect.
public IAsyncResult BeginExpect(TimeSpan timeout, AsyncCallback callback, object state, params ExpectAction[] expectActions),
// Begins the expect.
public IAsyncResult BeginExpect(params ExpectAction[] expectActions),
// Gets a value indicating whether the current stream supports reading.
public override bool CanRead { get; },
// Gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; },
// Gets a value indicating whether the current stream supports writing.
public override bool CanWrite { get; },
// Gets a value that indicates whether data is available on the <xref href="UnderAutomation.UniversalRobots.SSH.ShellStream" data-throw-if-not-resolved="false"></xref> to be read.
public bool DataAvailable { get; },
// Occurs when data was received.
public event EventHandler<ShellDataEventArgs> DataReceived,
// Releases the unmanaged resources used by the <xref href="System.IO.Stream" data-throw-if-not-resolved="false"></xref> and optionally releases the managed resources.
protected override void Dispose(bool disposing),
// Ends the execute.
public string EndExpect(IAsyncResult asyncResult),
// Occurs when an error occurred.
public event EventHandler<ExceptionEventArgs> ErrorOccurred,
// Expects the expression specified by text.
public string Expect(string text),
// Expects the expression specified by text.
public string Expect(string text, TimeSpan timeout),
// Expects the expression specified by regular expression.
public string Expect(Regex regex),
// Expects the expression specified by regular expression.
public string Expect(Regex regex, TimeSpan timeout),
// Expects the specified expression and performs action when one is found.
public void Expect(TimeSpan timeout, params ExpectAction[] expectActions),
// Expects the specified expression and performs action when one is found.
public void Expect(params ExpectAction[] expectActions),
// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
public override void Flush(),
// Gets the length in bytes of the stream.
public override long Length { get; },
// Gets or sets the position within the current stream.
public override long Position { get; set; },
// Reads text available in the shell.
public string Read(),
// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
public override int Read(byte[] buffer, int offset, int count),
// Reads the line from the shell. If line is not available it will block the execution and will wait for new line.
public string ReadLine(),
// Reads a line from the shell. If line is not available it will block the execution and will wait for new line.
public string ReadLine(TimeSpan timeout),
// This method is not supported.
public override long Seek(long offset, SeekOrigin origin),
// This method is not supported.
public override void SetLength(long value),
// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
public override void Write(byte[] buffer, int offset, int count),
// Writes the specified text to the shell.
public void Write(string text),
// Writes the line to the shell.
public void WriteLine(string line)
}
Members of SSH.SshCommand :
public class SshCommand : IDisposable {
// Begins an asynchronous command execution.
public IAsyncResult BeginExecute(),
// Begins an asynchronous command execution.
public IAsyncResult BeginExecute(AsyncCallback callback),
// Begins an asynchronous command execution.
public IAsyncResult BeginExecute(AsyncCallback callback, object state),
// Begins an asynchronous command execution.
public IAsyncResult BeginExecute(string commandText, AsyncCallback callback, object state),
// Cancels command execution in asynchronous scenarios.
public void CancelAsync(),
// Gets the command text.
public string CommandText { get; },
// Gets or sets the command timeout.
public TimeSpan CommandTimeout { get; set; },
// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose(),
// Releases unmanaged and - optionally - managed resources
protected virtual void Dispose(bool disposing),
// Waits for the pending asynchronous command execution to complete.
public string EndExecute(IAsyncResult asyncResult),
// Gets the command execution error.
public string Error { get; },
// Executes command specified by <xref href="UnderAutomation.UniversalRobots.SSH.SshCommand.CommandText" data-throw-if-not-resolved="false"></xref> property.
public string Execute(),
// Executes the specified command text.
public string Execute(string commandText),
// Gets the command exit status.
public int ExitStatus { get; },
// Gets the extended output stream.
public Stream ExtendedOutputStream { get; },
// Releases unmanaged resources and performs other cleanup operations before the
<xref href="UnderAutomation.UniversalRobots.SSH.SshCommand" data-throw-if-not-resolved="false"></xref> is reclaimed by garbage collection.
protected void Finalize(),
// Gets the output stream.
public Stream OutputStream { get; },
// Gets the command execution result.
public string Result { get; }
}
Logo

UnderAutomation
66 Avenue des Champs-Elysées
75008 Paris
FRANCE

© 2021 UnderAutomation - UnderAutomation Universal Robots .NET Communication SDK (Software Development Kit) offre la possibilité de créer des applications personnalisées .NET Core, .NET Framework et .NET Standard qui peuvent communiquer avec tous les robots UR. Des exemples prêts à l'emploi pour Windows, Mac OS et Linux, LabView, Node.js et Python sont fournis. Ce SDK utilise le serveur de Dashboard Server TCP/IP et XML-RPC pour contrôler le robot à distance et le protocole Client Interface pour recevoir et décoder le flux de données.