LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-09-2013, 11:28 PM   #1
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Rep: Reputation: Disabled
crontab issue on linux


ssh -n host1 "crontab << */10 * * * * /usr/local/script.sh > /logs/output.${HOSTNAME}"

I have around 100 hosts and need to populate(add if there are more entries) this cron entry on them but the variable $HOSTNAME is expanding to the name of the admin box I am trying to deploy from.

How can I fix this. Please advise.

Thanks,

Allan.
 
Old 05-09-2013, 11:43 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
How about using single quotes; they usually prevent interpolation.
 
Old 05-09-2013, 11:52 PM   #3
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
thanks for the reply!

even with single quotes I am getting -

ssh -n host1 echo '*/10 * * * * /usr/local/script.sh > /logs/output.${HOSTNAME}' >> /tmp/cron-list

HOSTNAME: Undefined variable.
 
Old 05-10-2013, 12:49 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quick test locally (non-ssh)
Code:
echo $HOSTNAME
boole

echo \$HOSTNAME
$HOSTNAME
the old backslash standby.
Works same even if you have those {}'s.
 
Old 05-10-2013, 12:57 AM   #5
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
ssh -n user@host1 "echo */10 * * * * /script.sh '>' /logs/output.\$HOSTNAME '>' /tmp/cron-list"

HOSTNAME: Undefined variable.
 
Old 05-10-2013, 01:36 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
OK; I spun up a VM to test
Code:
# host
ssh -n vm1 "echo '*/10 * * * * /usr/local/script.sh > /logs/output.\${HOSTNAME}' >/root/t.t"

# on VM
 cat t.t
*/10 * * * * /usr/local/script.sh > /logs/output.${HOSTNAME}
Sheesh ...
 
Old 05-10-2013, 01:48 AM   #7
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
thanks that worked! I figured the solution a little before I saw your reply, but thanks again.
 
Old 05-10-2013, 03:13 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yeah, well I knew I was on the right track, but I rarely have to get all the quotes and backslashes down to that level of sophistication.
 
Old 05-11-2013, 01:18 PM   #9
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi Chris,

Another issue I wanted to solve is that I dont have sshkeyless setup to the hosts and dont have expect installed on the admin box, how do I remote ssh without(or automatically) feeding in the password each time for all 100 hosts?

Thanks,
Allan.
 
Old 05-11-2013, 03:14 PM   #10
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
For this many machines, you really want to look at a configuration management system (e.g. Puppet, Chef, CFEngine). Doing this by hand is just horrific and error prone (been there, done that).

Can you not install expect on the admin host? Even if so, having passwords in an expect file is terribly insecure.

This sounds like a large installation. Are you the administrator of it, or just a regular user? If the latter, ignore my advice about configuration management (should be set up by the administrator), but ask your administrator to populate your account on all machines with a public key that you send him. Then use key based authentication.
 
Old 05-11-2013, 06:52 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I would go with ssh auth keys; much safer than expect and much simpler than Puppet etc (unless its already setup).
 
Old 05-12-2013, 02:27 PM   #12
era31415
LQ Newbie
 
Registered: May 2013
Posts: 4

Rep: Reputation: Disabled
ssh does not allow redirecting the password response from a file but you might be able to use...

Sshpass is a tool for non-interactivly performing password authentication with SSH
http://sourceforge.net/projects/sshp...urce=directory

Perhaps you should look into passwordless logins for the ssh accounts using .ssh/authorized_keys file.

You could then use sshpass and "ssh-copy-id (1) - install your public key in a remote machine's authorized_keys" to
update them all to passwordless logins.
 
Old 05-18-2013, 03:55 PM   #13
allanm78
LQ Newbie
 
Registered: May 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks era31415 - worked like a charm, awesome!
 
  


Reply



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
[SOLVED] Crontab issue pinga123 Linux - General 2 03-17-2011 01:56 AM
crontab issue,need help aditya_moon Linux - Newbie 2 02-07-2011 10:54 PM
crontab issue adnanm Linux - Newbie 1 03-16-2007 02:28 AM
crontab issue houssamfarag Red Hat 2 06-27-2006 11:56 PM
crontab issue lappen Linux - Newbie 6 05-24-2004 05:47 PM

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

All times are GMT -5. The time now is 06:12 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