2

This question is in a direct relation to this one: ssh fails to execute remote command when run from cron bash script - works from CLI

I'm not able to comment on the accepted answer as I don't have enough rep so please bear with me.

I'm running a script on a linux PC machine and the host i'm trying to get the output from is a router with its OS so it's nothing I can influence in terms of configuring the console. Basically executing this under cron: OUT=$(ssh -tt -vv user@host.com "remote command") gets me an empty variable.

debug1: Sending command: remote command
debug2: channel 0: request exec confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 2621440 rmax 262144
debug2: channel 0: read<=0 rfd 4 len 0
debug2: channel 0: read failed

If I'm executing this outside cron, i.e. in the CLI i'm getting output as expected. As you can see the -tt option to force pseudo-tty allocation doesn't help.

Any solutions for this to help cron overcome the buggy remote console?

vobelic
  • 183
  • 1
  • 5
  • 15
  • If we could help it might be a good idea to provide the specific router vendor, fw version, etc ... you are working with.... also provide what troubleshooting you have performed... – mdpc May 24 '14 at 17:08
  • Is this for a home system? – mdpc May 24 '14 at 17:08
  • Router in question is a MikroTik 2011UAS with ROS 5.24. The problem isn't present in e.g. 5.14 or 6.x but an upgrade currently is not an option. This question was purely if there's a workaround from a shell scripting point of view. – vobelic May 24 '14 at 17:16
  • Edited the question to give some more info. – vobelic May 24 '14 at 17:19
  • 2
    You need to capture the output from stderr of the ssh command `OUT=$(ssh -t user@host.com "remote command" 2>/tmp/ssh.out)` hopefully it will reveal the problem. – user9517 May 24 '14 at 17:29
  • The stderr is from ssh itself which works - connection is established towards the router and the exit code is 0. Here the relevant debug info: http://pastebin.com/DThWHt9h – vobelic May 24 '14 at 18:11
  • What is the exact remote command ? – user9517 May 24 '14 at 18:39
  • Just a simple "/export compact" – vobelic May 24 '14 at 22:03
  • 1
    Possible duplicate of [Why is my crontab not working, and how can I troubleshoot it?](http://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it) – Jenny D Dec 25 '15 at 11:31
  • tried all sorts of things but only @Thetimehascome 's comment here works! Cheers! – OZZIE Apr 04 '17 at 09:44

3 Answers3

2

Your command print results stderr or stdout?

You can reroute stderr to stdout using

OUT=$(ssh -tt -vv user@host.com "remote command" 2>&1 )
Jakuje
  • 9,145
  • 2
  • 40
  • 44
goakgun
  • 31
  • 2
0

Did you consider to use fully qualified paths for the cronjob?

Use /usr/bin/ssh user@host instead of ssh user@host

user218671
  • 31
  • 3
-1

Some routers or embedded devices have very bad SSH implementations. I've seen cases where they only know how to read commands from the console, and cannot deal with commands passed on the command line. If this is the case you have, you may want to try piping the remote command into the remote ssh.

echo remote command | ssh user@host

If this helps, you may have better luck trying to code something with expect.

chutz
  • 7,569
  • 1
  • 28
  • 57