1

So I'm at my wits end with this, I'm not a complete n00b with Linux but I know enough to get around etc and change other peoples scripts.

Here's what I am trying to do. Firstly I wanted to enable passwordless ssh into my pi2 using public/private keys but that seems to fail no matter what I do. So, I created an expect script to log into my pi2 whilst specifying the password within the script. Completely unsecure I know but all I'm using ssh for in this case is for forward a specific port (that runs apache2 - Owncloud) through the ssh tunnel so I can access it from the internet. Reason being is that my Elementary OS (Ubuntu 16.04 based) is permanently connected to a VPN and without this port I cannot use my home cloud at work or anywhere away from home. There is no easy way to do a split tunnel with this VPN service, especially if you have a dynamic IP (btw, I'm a network engineer by trade, so this is my strong point).

Now that that's out of the way. The expect script I'm running is as follows.

#!/usr/bin/expect

>set timeout 20

set ip [lindex $argv 0]

set user [lindex $argv 1]

set password [lindex $argv 2]

spawn ssh -fN -R [remote port]:localhost:[local port] "$user\@$ip"

expect "username@x.x.x.x's password:"

send "$password\r";

interact

Then I specific the following in terminal.

/usr/local/bin/ssh.exp [ip address] [username] [password]

This works perfectly fine and I can execute the script after I've logged in.

I'd like to get this working automatically on boot, and so far nothing has worked. Adding this to crontab with @reboot fails, adding this to rc.local also fails. I'm wondering whether it's because it trys to run it in the foreground or because its an expect script, not bash.

Completely at a loss here people, some advice would be greatly appreciated. On a side note, if I can get passwordless ssh working at the same time that would be great.

  • Who or what do you think is going to interact with your remote session at boot time? Shouldn't you be starting this as a autostart job in your user session? – steeldriver Feb 14 '17 at 12:19
  • Nothing is going to interact with the remote session straight away. It doesn't matter in this case, if I run the script directly from a terminal it stays open permanently with no prompt due to the various ssh flags. After that I will be syncing OwnCloud through the ssh session. I've already tested that it works, I just can't get the script to run as an autostart job. No matter how I approach it it doesn't want to run. Thanks for the reply btw. – Sephzer Feb 15 '17 at 9:25
0

Ok so I've figured this out after some tinkering around.

Permissions on the .ssh folder and authorized_keys file on the pi needed to be set to 655 on both and the user needed to be changed to pi not root. This managed to fix my issue with passwordless login.

From this point I could just issue a standard ssh command in rc.local, though I found out that this executes as root. In which case I needed to specific the following.

ssh -fN -i /home/user/.ssh/id_rsa -R 9000:localhost:5253 pi@x.x.x.x

Sure enough it now persists on reboot and OwnCloud is now reachable via the net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.