SSH Linux commands

SSH is a protocol that allows you to remotly execute Linux command lines robot controller.

This feature is made possible because an embedded Linux is running in the robot.

You can also use this library to remote manipulate files over SSH, see the SFTP documentation.

Remote execute Linux commands

SSH features are not enabled by default when connecting to the robot. You must enable it and specify the Linux user name and password. If these credentials are not specified, the user is "ur" and the password "easybot".

To do this set the Ssh.EnableSSh property in ConnectParameters class.

Please then refer to the API reference below which lists all the available file and folder handling features.

If you wish to manipulate files outside of any instance of the UR object, you can create an instance of class Ssh.SshClient which has the same functions.

The RunCommand method allows you to synchronously execute a command and wait for its response.

// Create a new robot instance
var robot = new UR();
// Setup connection to th robot
var param = new ConnectParameters();
param.IP = "192.168.0.1";
// Enable SSH
param.Ssh.EnableSftp = true;
// If not specified, credentials are "ur" with password "easybot"
param.Ssh.Username = "John";
param.Ssh.Password = "!!PASS!!";
// Connect to the robot
robot.Connect(param);
// Create a execute the command
SshCommand command = robot.Ssh.RunCommand("echo Hello > /home/ur/Desktop/NewFile.txt");
// Get result
string result = command.Result; // console output
int exitStatus = command.ExitStatus; // exit status

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 a new robot instance
var robot = new UR();
// Setup connection to th robot
var param = new ConnectParameters();
param.IP = "192.168.0.1";
// Enable SSH
param.Ssh.EnableSsh = true;
// If not specified, credentials are "ur" with password "easybot"
param.Ssh.Username = "John";
param.Ssh.Password = "!!PASS!!";
// Connect to the robot
robot.Connect(param);
// Create a shell and specify its size
ShellStream shell = robot.Ssh.CreateShellStream("My super app", 40, 100, 40, 100, 1000);
shell.DataReceived += Shell_DataReceived;
shell.WriteLine("ping 192.168.0.10");
private static void Shell_DataReceived(object sender, ShellDataEventArgs e)
{
// New shell line from the robot
string dataReceived = e.Line;
}

Try it with the Windows example

SSH

API reference

Members of Ssh.SshClient :
public class SshClient : SshClientBase {
// Connects to the robot
public void Connect(string ip, string username, string password, int port = 22)
}
Members of Ssh.Tools.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.Tools.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.Tools.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.Tools.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.Tools.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; }
}

Read more