Run a command over resin ssh and exit / push file to device

using ssh I could

ssh -t user@server "<cmd>"

to run a cmd on server without typing it in the remote shell.
Is there a way to do that with resin ssh. (without installing a sshd in a user container)

edit:
What I want to do is trigger a scp command to pull a file into the device.
Maybe there is a better way to do that…? We need to add some project specific files to the device before shipping. The devices are preloaded (resin preload)

@seb welcome to the forums!

I don’t think there’s currently a way to do this with resin ssh but I am sure one of my colleagues will correct me if there is!

Is there a reason why you couldn’t pull the files to the device as part of your container setup? Although you have preloaded, the devices will update to the latest version when they connect if there’s one available. Or are you trying to add files to the host? If you could explain your scenario in a bit more detail we’ll try and help with a solution.

The files I want to push are project (==device) specific. The device is a Gateway to the KNX bus. It provides a visualisation of the connected KNX datapoints. These datapoints differ from device to device .
So I’d like to take one of the preloaded devices and add the device specific stuff in one short step by running a script.

OK so this definitely is not supported by resin ssh at this time but it is something that has been requested by a number of users; so it is on the radar although I can’t give an ETA. I have added this thread to our internal notes for this feature request.

In the meantime I personally would be trying to approach it from the other side. What about setting a device variable that identifies your device with the device specific stuff you mention, then adding a generic script to the container to read that variable and automatically pull your files from whatever source you have?

its good to know, that this is on your radar. Is this already on the public roadmap?

Do you also consider, that it would be helpful for this to have a param for resin local scan to just get the device ID of a single connected device? So instead of

$ sudo resin local scan | grep host: | sed 's/host: *\(.*\)\.local/\1/g'
b2ff972

I could do

$ sudo resin local scan --short
b2ff972

So in combination with an resin scp - or whatever you will create - I could write a simple script to do the provisioning like this:

# only works if scan finds only one device
DEVICE_ID = "$(sudo resin local scan --short)"
# here I've invented resin scp which takes a container name in addition to target path
resin scp my_config.json ${DEVICE_ID}:my_container_name/my_target_dir
# this already exists
resin env add MY_ENV_VAR value --device ${DEVICE_ID}

Having something like this would be very handy.

In the meantime I’ll add a sshd to the user container that needs the data.

thanks for your help.

It’s not on the public roadmap as far as I can see but it is logged on our internal balena issue tracker with the other threads that mention it so it won’t be lost!

Hopefully installing sshd works OK for you in the meantime. :+1:

Note that balenaOS includes a standard ssh server, and the standard ssh and scp tools can be used to access the host OS (and from there, access an app container). It works with both development and production balenaOS images. Production images require a ssh key to be added to the config.json file – see sshKeys section of the meta-balena README file. Development images don’t require a ssh key to be added.

The ssh server on a device listens on TCP port number 22222. This port is not blocked by the device host OS, not even in production. If this port is blocked by a firewall or router on the device’s local network, the balena tunnel (balena CLI) command can be used as in the examples below.

Example 1 (port 22222 not blocked in the device’s local network)

# run these commands on your laptop/desktop computer

ssh -p 22222 root@<device_ip_address>
scp -P 22222 my_local_file root@<device_ip_address>:/mnt/data/

Example 2 (port 22222 is blocked in the device’s local network)

# run these commands on your laptop/desktop computer

balena tunnel <deviceUUID> -p 22222:4321
ssh -p 4321 root@127.0.0.1
scp -P 4321 my_local_file root@127.0.0.1:/mnt/data/

To open a shell in an app container, once you’ve got a shell to the host OS, you can run:

# run these commands on the host OS

balena-engine ps  # to list the running containers
balena-engine exec -it <containerID> /bin/sh

To scp files directly in the filesystem of an app container, use its shared persistent storage folder, for example:

# run this command on your laptop/desktop computer

scp -P 22222 resin-my_local_file root@<device_ip_address>:/var/lib/docker/volumes/<appID>_resin-data/_data/