LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2017, 06:11 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 762

Rep: Reputation: Disabled
Adding script to rc.local


How come I can execute /usr/local/bin/mytunnel.sh (script shown below) directly from the cli and it will spawn a ssh socket process, yet adding /usr/local/bin/tempaccess.sh to /etc/rc.local will not?

Code:
#!/bin/sh
ip=12.34.567.89
while [ true ] ;
do
    if [ $(ss dst $ip | grep -q '$ip:ssh') ]
    then
        sleep 4
    else
        #Note that following is a blocking command given server configuration
        ssh -R 2220:localhost:22 myuser@$ip
    fi
done
 
Old 06-09-2017, 06:19 AM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,787

Rep: Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699
Quote:
Originally Posted by NotionCommotion View Post
How come I can execute /usr/local/bin/mytunnel.sh (script shown below) directly from the cli and it will spawn a ssh socket process, yet adding /usr/local/bin/tempaccess.sh to /etc/rc.local will not?

Code:
#!/bin/sh
ip=12.34.567.89
while [ true ] ;
do
    if [ $(ss dst $ip | grep -q '$ip:ssh') ]
    then
        sleep 4
    else
        #Note that following is a blocking command given server configuration
        ssh -R 2220:localhost:22 myuser@$ip
    fi
done
There could be many possible reasons, but for one you have set no path. Since anything run from the rc.local runs as root but before any logon, the path is not set. You must either call each external executable using the full path name or set a path variable that contains all of the paths to the different executables you will call.

For another, the rc.local is NOT executable by default on most modern distributions. If you are going to modify it to call another script, make sure that both scripts are executable. (permission 755 will do, or 500 if you want it to be secure, I use 750 on mine for convenience)
 
Old 06-09-2017, 09:05 AM   #3
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 762

Original Poster
Rep: Reputation: Disabled
Thanks wpeckham,

Yeah, multiple reasons!

Not path as they were being resolved.

Didn't have pki keys for root, only my normal user.
 
Old 06-09-2017, 09:49 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
If you want it to reconnect automatically you can put the SSH client itself inside a loop:

Code:
while ! ssh -o ServerAliveInterval=20 -i ./some.key.rsa -R 2220:localhost:22 myuser@$ip; do sleep 2; done;
Also, pretty much all of those options could be stored in ~/.ssh/config to make it shorter. Put it in at the top of the configuration file.

Code:
Host myshortcut
        HostName 203.0.113.111
        User myuser
        IdentityFile /home/me/some.key.rsa
        ServerAliveInterval 20
        RemoteForward localhost:2220 localhost:22
So to use that, it'd be the following:

Code:
while ! ssh myshortcut; do sleep 2; done;
See "man ssh_config" for all the options.
 
2 members found this post helpful.
Old 06-10-2017, 08:32 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,907

Rep: Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510
Quote:
Originally Posted by NotionCommotion View Post
How come I can execute /usr/local/bin/mytunnel.sh (script shown below) directly from the cli and it will spawn a ssh socket process, yet adding /usr/local/bin/tempaccess.sh to /etc/rc.local will not?

Code:
#!/bin/sh
ip=12.34.567.89
while [ true ] ;
do
    if [ $(ss dst $ip | grep -q '$ip:ssh') ]
    then
        sleep 4
    else
        #Note that following is a blocking command given server configuration
        ssh -R 2220:localhost:22 myuser@$ip
    fi
done
If your distribution is using systemd, verify that "NetworkManager-wait-online" is enabled.

One way for the failure to occur is that the network is not yet ready when rc.local is run.
 
Old 06-11-2017, 07:45 AM   #6
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,787

Rep: Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699Reputation: 1699
Another good idea is to trap and redirect the STDOUT and STDERR to a couple of files so you can READ them and see what messages they log.
That might look something like
Code:
/usr/local/bin/mytunnel.sh >/tmp/mytun.out 2>/tmp/mytun.err
 
Old 06-11-2017, 08:00 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,907

Rep: Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510Reputation: 1510
Systemd is SUPPOSED to catch all the error messages and log them.
:-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe output of local script, ran on remote machine, to local python script trendal Programming 17 07-04-2016 01:56 AM
shell script to compare filese b/w local and remote and delete files from local serve dsids Linux - Networking 9 08-23-2006 07:20 AM
Adding Local Users mslatter Linux - General 6 10-19-2005 09:20 AM
Adding a local printer harperonline Linux - Hardware 3 08-12-2003 10:39 AM
adding local users w/ only text PlatinumRik Linux - General 9 04-09-2003 03:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration