This guide will help you connect to an SSH server and execute your first command in just a few minutes.
Prerequisites
Before you begin, make sure you have:
- Completed the installation steps
- An SSH server you can connect to (hostname, port, username, and password or private key)
- A React Native project set up and running
Connect and execute a command
Follow these steps to establish your first SSH connection and run a command.
Import the library
Import the SSHClient class into your React Native component:import SSHClient from '@dylankenneally/react-native-ssh-sftp';
Connect to your server
Use either password or key-based authentication to connect:const client = await SSHClient.connectWithPassword(
"10.0.0.10", // host
22, // port
"username", // username
"password" // password
);
const privateKey = "-----BEGIN RSA PRIVATE KEY-----\n...";
const passphrase = "optional-passphrase"; // optional
const client = await SSHClient.connectWithKey(
"10.0.0.10", // host
22, // port
"username", // username
privateKey, // private key
passphrase // passphrase (optional)
);
Execute a command
Run a command on the remote server:const output = await client.execute('ls -la');
console.log(output);
Clean up
Always disconnect when you’re done:
Complete example
Here’s a full React component that connects to an SSH server and executes a command:
import React, { useState } from 'react';
import { View, Button, Text, ScrollView } from 'react-native';
import SSHClient from '@dylankenneally/react-native-ssh-sftp';
export default function SSHExample() {
const [output, setOutput] = useState('');
const [error, setError] = useState('');
const runSSHCommand = async () => {
let client;
try {
// Connect to the server
client = await SSHClient.connectWithPassword(
"10.0.0.10",
22,
"username",
"password"
);
// Execute a command
const result = await client.execute('uname -a');
setOutput(result);
setError('');
} catch (err) {
setError(err.message);
setOutput('');
} finally {
// Always disconnect
if (client) {
client.disconnect();
}
}
};
return (
<View style={{ padding: 20 }}>
<Button title="Run SSH Command" onPress={runSSHCommand} />
{output && (
<ScrollView style={{ marginTop: 20, padding: 10, backgroundColor: '#f0f0f0' }}>
<Text style={{ fontFamily: 'monospace' }}>{output}</Text>
</ScrollView>
)}
{error && (
<Text style={{ color: 'red', marginTop: 10 }}>Error: {error}</Text>
)}
</View>
);
}
This example uses password authentication for simplicity. In production, consider using key-based authentication for better security.
Next steps
Now that you’ve successfully connected and executed a command, explore more features: