How to Use SSH and SFTP Client for Flutter

May 25, 2024 | Programming

As mobile developers increasingly embrace the power of cross-platform coding, integrating SSH and SFTP functionality into Flutter applications has become essential. Here we will cover the installation, usage, and common troubleshooting tips for the SSH and SFTP client wrapped around iOS’s NMSSH and Android’s JSch libraries. Let’s dive into the world of remote connectivity!

Installation

Getting started is simple. Add SSH as a dependency in your pubspec.yaml file:

dependencies:
  ssh: ^your_version_here

This enables your Flutter app to leverage the SSH capabilities effortlessly.

Known Issues

When deploying in Android’s release mode, you may encounter a platform exception regarding connection failure. This typically arises from a missing class during execution. Fortunately, there are two effective workarounds:

  • Disable shrink by running the command: flutter build apk --no-shrink.
  • Configure your ProGuard rules as detailed in this comment.

How to Use the SSH Client

Now that you’ve set up the package, let’s walk through how to create clients using both password and public key authentication.

Create a Client Using Password Authentication

import 'package:ssh/ssh.dart';

var client = new SSHClient(
  host: 'my.sshtest',
  port: 22,
  username: 'sha',
  passwordOrKey: 'Password01.',
);

Think of this as setting a key to your front door; using the password gives access to your virtual home securely.

Create a Client Using Public Key Authentication

import 'package:ssh/ssh.dart';

var client = new SSHClient(
  host: 'my.sshtest',
  port: 22,
  username: 'sha',
  passwordOrKey: 
    privateKey: '-----BEGIN RSA PRIVATE KEY-----\n......\n-----END RSA PRIVATE KEY-----',
    passphrase: 'passphrase-for-key',
);

Using a public/private key pair is like having a super secure vault with dual locks, ensuring only authorized access.

Handling OpenSSH Keys

If you work with recent versions of OpenSSH, remember to convert your keys to the PEM format using puttygen. Here’s how:

  1. Temporarily remove the passphrase: ssh-keygen -p -f id_rsa
  2. Convert to RSA PEM format: puttygen id_rsa -O private-openssh -o id_rsa_unencrypted
  3. Re-apply the passphrase: ssh-keygen -p -f id_rsa
  4. Apply passphrase to the converted key: puttygen id_rsa_unencrypted -P -O private-openssh -o id_rsa_flutter
  5. Remove the unencrypted version: rm id_rsa_unencrypted

Connecting and Executing Commands

Once your client is set up, you can connect, execute, and close connections smoothly:

await client.connect();
await client.disconnect();
var result = await client.execute('ls'); // Execute a command

Using SFTP

SFTP operations are straightforward. Here are a few common tasks:

await client.connectSFTP();
var array = await client.sftpLs('/home');
await client.sftpMkdir('testdir');
await client.sftpRename(oldPath: 'testfile', newPath: 'newtestfile');
await client.sftpRmdir('testdir');
await client.sftpRm('testfile');
var filePath = await client.sftpDownload(path: 'testfile', toPath: 'tempPath');
await client.sftpUpload(path: 'filePath', toPath: '.');
await client.disconnectSFTP();

Performing these tasks can be likened to handling files on your desktop – intuitive and efficient!

Troubleshooting

If any issues arise while implementing the SSH and SFTP client, here are some troubleshooting suggestions:

  • Ensure your SSH keys are correctly formatted and converted to PEM if using OpenSSH.
  • Check your host, username, and password values for any typos.
  • If the platform exception continues, revisit the ProGuard rule configurations.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Demo

For practical examples, refer to the demo example provided in the GitHub repository.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox