LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-27-2007, 06:26 PM   #1
gctaylor1
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Rep: Reputation: 0
logging, remote ssh, scripts, and the at command


When I run this script
Code:
#!/bin/bash
set -x
set -v

Log_file=/tmp/mylog.log

Remote=router

source /mnt/hdb1/home/user1/.keychain/little-sh

{
echo "Before"
} >> $Log_file

{
Hostos_via_ssh=$(ssh $Remote uname)
echo "$Hostos_via_ssh"
} >> $Log_file

{
echo "After"
} >> $Log_file
from the at command, I can't seem to get the "After" in to my log file. All I see in the log is

Code:
Before
Linux
I know it has something to do with my ssh remote command but I cannot figure it out. The script works fine when I run it from the command line and when I use a non-ssh-remote command in place of the Hostos_via_ssh line. I've tried using #!/bin/sh too but it doesn't help. At the bottom is the debug output mailed to me from at.

Code:
+ set -v

Log_file=/tmp/mylog.log
+ Log_file=/tmp/mylog.log

Remote=router
+ Remote=router

source /mnt/hdb1/home/user1/.keychain/little-sh
+ source /mnt/hdb1/home/user1/.keychain/little-sh
SSH_AUTH_SOCK=/tmp/ssh-TZIHuv7388/agent.7388; export SSH_AUTH_SOCK;
++ SSH_AUTH_SOCK=/tmp/ssh-TZIHuv7388/agent.7388
++ export SSH_AUTH_SOCK
SSH_AGENT_PID=7389; export SSH_AGENT_PID;
++ SSH_AGENT_PID=7389
++ export SSH_AGENT_PID

{
echo "Before"
} >> $Log_file
+ echo Before

{
Hostos_via_ssh=$(ssh $Remote uname)
echo "$Hostos_via_ssh"
} >> $Log_file
ssh $Remote uname
++ ssh router uname
+ Hostos_via_ssh=Linux
+ echo Linux
Any ideas?
 
Old 01-28-2007, 08:03 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,534
Blog Entries: 51

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
If your key is pass-phrased then you could check if making the ssh-agent vars available in the environment *before* running your at script works. You could set up your agent stuff outside the script and use something like this to check (Bash) and bail out if not set:
Code:
[ -z "$SSH_AUTH_SOCK" ] || { echo "No SSH_AUTH_SOCK, exiting." >/dev/stderr; exit 1; }
Doing so however provides easy entry to remote systems for anyone with access to the account. Could you explain why you need it? Maybe we can help find an alternative. BTW, is there any special need to use { something; } notation here?
 
Old 01-28-2007, 03:18 PM   #3
gctaylor1
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks for taking the time to respond.

My key is pass-phrased and I'm thinking that .keychain is taking care of that for me with this line:
Code:
source /mnt/hdb1/home/user1/.keychain/little-sh
Is this debug info an indicator of the same thing that you suggested? Or not?
Code:
SH_AUTH_SOCK=/tmp/ssh-TZIHuv7388/agent.7388; export SSH_AUTH_SOCK;
++ SSH_AUTH_SOCK=/tmp/ssh-TZIHuv7388/agent.7388
++ export SSH_AUTH_SOCK
The fact that I get results back (the word "Linux" in this case) from the remote machine leads me to think that the key part is working correctly, but maybe I mis-understand what you're trying to tell me.

My reason for using the {something} notation is that it seems like I have better results in capturing my output to logs. For example this gets the word Linux in my log file:
Code:
{
Hostos_via_ssh=$(ssh $Remote uname)
echo  "$Hostos_via_ssh" 
} >> $Log_file
But this doesn't:
Code:
Hostos_via_ssh=$(ssh $Remote uname)
echo  "$Hostos_via_ssh" >> $Log_file
And it seems more efficient, easier to read, etc., when I redirect the whole block.

Overall my goal is to backup my local machine to a remote machine using dar over ssh. This is a small snippet, just big enough to demonstrate the problem. Since I'm learning, I am redirecting every section of the script to a log file as it makes it easier for me to troubleshoot. Probably this could be done faster and easier if I would use some existing scripts that can be found on the www but I'm using this as a learning exercise as well as having practical value.
 
Old 01-28-2007, 06:24 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,534
Blog Entries: 51

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
You're right about the keychain/results thing, I should read more carefully. Using the ssh "-n" flag should do the trick.
 
Old 01-29-2007, 08:22 AM   #5
gctaylor1
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Original Poster
Rep: Reputation: 0
That works. Thank-you! You make it look so easy. I've been working on this for days.
 
  


Reply

Tags
at, cron, script, shell, ssh


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh command logging jcookeman Linux - Security 3 08-26-2008 02:50 AM
LXer: Adodb Multiple Test Scripts Remote Command Execution ... LXer Syndicated Linux News 0 01-09-2006 06:01 PM
SSH remote command not timing out tajsss Other *NIX 5 04-28-2005 06:57 AM
Adding users via ssh remote command enolc Linux - Software 2 05-27-2004 06:48 AM
remote command over ssh, password prompt linowes Linux - General 2 10-27-2002 08:22 PM


All times are GMT -5. The time now is 10:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration