Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #175111 > unrolled thread

How to get ssh to run in daemon script

Started byRussell Gadd <russ.mail.lists@gmail.com>
First post2016-11-27 12:10 +0100
Last post2016-11-28 22:30 +0100
Articles 5 — 3 participants

Back to article view | Back to linux.debian.user


Contents

  How to get ssh to run in daemon script Russell Gadd <russ.mail.lists@gmail.com> - 2016-11-27 12:10 +0100
    Re: How to get ssh to run in daemon script Nemeth Gyorgy <friczy@freemail.hu> - 2016-11-27 14:00 +0100
      Re: How to get ssh to run in daemon script Russell Gadd <russ.mail.lists@gmail.com> - 2016-11-27 22:10 +0100
    Re: How to get ssh to run in daemon script Richard Hector <richard@walnut.gen.nz> - 2016-11-28 13:30 +0100
      Re: How to get ssh to run in daemon script Russell Gadd <russ.mail.lists@gmail.com> - 2016-11-28 22:30 +0100

#175111 — How to get ssh to run in daemon script

FromRussell Gadd <russ.mail.lists@gmail.com>
Date2016-11-27 12:10 +0100
SubjectHow to get ssh to run in daemon script
Message-ID<sI5vj-3xy-5@gated-at.bofh.it>
I am trying to add a command into a script to shut down my NAS when the 
UPS detects a power loss. There is a daemon apcupsd which gets a signal 
from the UPS and runs various scripts which can be modified by the user.

I have a script doshutdown as follows

#!/bin/sh
  ... various messages and sleep delays
/usr/bin/ssh root@nasbox poweroff
  ... more messages and sleep delays

The ssh command in my script does not run in a power off test 
(everything else in the script works i.e. the messages and delays), but 
if I run the ssh command interactively from the usual graphical terminal 
(as root) it shuts down the NAS, so the NAS isn't stopping it working 
assuming its getting the appropriate handshaking from ssh.

My knowledge of how to get daemon scripts working and how to use ssh is 
almost nil. Also I can't find a way to get any error output which the 
ssh command might be issuing. I've tried enclosing the body of the 
script in brackets to redirect output to files:
{
} > /data/temp/shutdown-out.txt 2> /data/temp/shutdown-err.txt

I've looked in syslog, kern.log and auth.log without finding anything.

So I'm scrambling around in the dark. Currently my vague ideas as to 
what might be wrong are:

script doesn't know what nasbox is (it is defined in /etc/hosts)
ssh is being run without being associated as root
ssh cannot find root's keys
nas doesn't recognise login as authorised user

I'm also starting to think I am stressing the UPS too much by running 
too many power off tests, so I'd like to resolve this without too much 
more experimentation.

Grateful for any suggestions. I'm running Linux Mint 17.3 (based on 
Ubuntu 14.04, based on Jessie). Uses openssh (1:6.6p1-2ubuntu2.8)

[toc] | [next] | [standalone]


#175116

FromNemeth Gyorgy <friczy@freemail.hu>
Date2016-11-27 14:00 +0100
Message-ID<sI7dM-4ot-7@gated-at.bofh.it>
In reply to#175111
2016-11-27 11:55 keltezéssel, Russell Gadd írta:
> I am trying to add a command into a script to shut down my NAS when 
> the UPS detects a power loss. There is a daemon apcupsd which gets a 
> signal from the UPS and runs various scripts which can be modified by 
> the user.
>
> I have a script doshutdown as follows
>
> #!/bin/sh
>  ... various messages and sleep delays
> /usr/bin/ssh root@nasbox poweroff
>  ... more messages and sleep delays
>
> The ssh command in my script does not run in a power off test 
> (everything else in the script works i.e. the messages and delays), 
> but if I run the ssh command interactively from the usual graphical 
> terminal (as root) it shuts down the NAS, so the NAS isn't stopping it 
> working assuming its getting the appropriate handshaking from ssh.
>
> My knowledge of how to get daemon scripts working and how to use ssh 
> is almost nil. Also I can't find a way to get any error output which 
> the ssh command might be issuing. I've tried enclosing the body of the 
> script in brackets to redirect output to files:
> {
> } > /data/temp/shutdown-out.txt 2> /data/temp/shutdown-err.txt
>
> I've looked in syslog, kern.log and auth.log without finding anything.
>
> So I'm scrambling around in the dark. Currently my vague ideas as to 
> what might be wrong are:
>
> script doesn't know what nasbox is (it is defined in /etc/hosts)
> ssh is being run without being associated as root
> ssh cannot find root's keys
> nas doesn't recognise login as authorised user
>
> I'm also starting to think I am stressing the UPS too much by running 
> too many power off tests, so I'd like to resolve this without too much 
> more experimentation.
>
> Grateful for any suggestions. I'm running Linux Mint 17.3 (based on 
> Ubuntu 14.04, based on Jessie). Uses openssh (1:6.6p1-2ubuntu2.8)

What user runs the daemon? If it is a separate user then run the ssh 
with that user interactively. If the user does not have the 
~/.ssh/known_hosts file or the destination host is not in it, then you 
will have problems.

[toc] | [prev] | [next] | [standalone]


#175124

FromRussell Gadd <russ.mail.lists@gmail.com>
Date2016-11-27 22:10 +0100
Message-ID<sIeRX-1cD-7@gated-at.bofh.it>
In reply to#175116
On 27/11/16 13:00, Nemeth Gyorgy wrote:
> ...
> What user runs the daemon? If it is a separate user then run the ssh
> with that user interactively. If the user does not have the
> ~/.ssh/known_hosts file or the destination host is not in it, then you
> will have problems.

I've now discovered that the daemon is run by root.

As I said in my post the ssh command does run successfully when run 
interactively "but if I run the ssh command interactively from the usual 
graphical terminal (as root) it shuts down the NAS". 
/root/.ssh/known_hosts does exist. As it does work interactively, 
presumably root must have the correct entries in the known_hosts file.

One problem is that when the daemon runs the script the ssh command 
fails and I don't know if there was any error message, and if so where 
to find any error messages after I reboot.

[toc] | [prev] | [next] | [standalone]


#175144

FromRichard Hector <richard@walnut.gen.nz>
Date2016-11-28 13:30 +0100
Message-ID<sIteh-22H-17@gated-at.bofh.it>
In reply to#175111

[Multipart message — attachments visible in raw view] — view raw

On 27/11/16 23:55, Russell Gadd wrote:
> So I'm scrambling around in the dark. Currently my vague ideas as to
> what might be wrong are:
> 
> script doesn't know what nasbox is (it is defined in /etc/hosts)

You could test that by using the IP address

> ssh is being run without being associated as root
> ssh cannot find root's keys

This seems the most likely to me - if $HOME isn't set in the script's
environment, for example. You could explicitly tell ssh where to find
the required key, with the -i option - eg:

/usr/bin/ssh -i /root/.ssh/id_rsa root@nasbox poweroff

> nas doesn't recognise login as authorised user
> 
> I'm also starting to think I am stressing the UPS too much by running
> too many power off tests, so I'd like to resolve this without too much
> more experimentation.

You could do your testing by sshing to a different box (even localhost)
and/or running a different command?

eg "ssh root@localhost whoami > /root/test.out" ... and have a look at
the file afterwards

Richard


[toc] | [prev] | [next] | [standalone]


#175201

FromRussell Gadd <russ.mail.lists@gmail.com>
Date2016-11-28 22:30 +0100
Message-ID<sIBER-7sc-9@gated-at.bofh.it>
In reply to#175144
On 28/11/16 12:30, Richard Hector wrote:
> ...
>
>> nas doesn't recognise login as authorised user
>>
Turns out this was the issue - problem solved.For some reason the 
interactive successful use of ssh must have used my non-root users key 
even when run by (su) root in a graphical terminal. I needed to send a 
root public key to the NAS to get it to accept the connection when run 
from the daemon. /root/.ssh/authorized_keys in the NAS (running some 
version of Linux) only had the normal user public key.

Thanks to all for the replies.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web