How can I run a command at successful ethernet connect in Arch?

1

I have a Raspberry Pi running ArchARM, and would like to make a reverse SSH tunnel as soon as it connects to the network via ethernet.

I made an executable (+x) shell script in /usr/bin, and would like that to be run whenever the default network manager acquires an IP on the eth0 interface.

Alternatively, what is the best way I could do this without an SSH tunnel or opening a port on my router?

tekknolagi

Posted 2013-06-01T05:46:53.017

Reputation: 1 130

Answers

2

you can make a reverse SSH with the -R arg (i wrote an article in french about this here ) and cron or make a service with.

here the stuff translated

user@mynewRaspberry :~$ ssh -R 61337:localhost:22 z8po@z8po.dyndns.info 

after you can login it from another place, here in example from z8po.dyndns.info

z8po@hive :~$ ssh z8po.dyndns.info -p 61337

Next let's use a while loop in your script to check the ssh is forever relaunch.

while true do ssh -R 61337:localhost:22 z8po@z8po.dyndns.info done

you can maintain your connection with a keep alive on a ssh client or a server, add to /etc/ssh/sshd_config or ~/.ssh/config the folowing line

 ClientAliveInterval 60

Don't forget to use rsa key insteed of paswsrod for auto login without prompt, or if you really want to use password, change the ssh command with user:password@ip but it is quite less secure.

First method Cron it

make a script checking if allready launched, in /home/USER/autolauncher.sh

#!/bin/sh
if ps -ef | grep -v grep | grep yourscript.sh ; then
        exit 0
else
        while true do ssh -R 61337:localhost:22 z8po@z8po.dyndns.info done
        exit 0
fi

and edit you crontab

crontab -e

add it after the other crons jobs; every 5 min launch the previous script (which will not work if already running)

   */5 * * * * /home/USER/autolauncher.sh

Second method : service at startup

create or copy the script in init.d add the commands in to it using vi:

  sudo nano /etc/init.d/autolauncher.sh

make it executable

  sudo chmod +x /etc/init.d/autolauncher.sh

update-rc.d to create and configure start:

  sudo update-rc.d autolauncher.sh defaults

now you have two methods to make your rasperry trying to connect

z8po

Posted 2013-06-01T05:46:53.017

Reputation: 66

Though I don't understand... why is there a while loop in the cron script? – tekknolagi – 2013-06-01T17:51:24.070

1because of service at startup, it will launch one time, and if crash, relaunch it etc... – z8po – 2013-06-02T14:21:59.800