2

Decryption of what I do / what I want:

Remotely run script on our servers (from Ubuntu 14.04, Python 2.7.6, I would like execute scripts on Ubuntu 12.04 & Ubuntu 14.04). This script will mount encrypted volumes and set correct hostname and confirm that servers are in staging environment.

What I did:

  1. I created EXPECT script (scripts/auto_mounth.sh - Thank to this I am able to run interactive script what will automatically type mount partion's Password, call the hostname and type it and confirm that servers are in staging environment

  2. I created FABRIC function

    a. In this function I set env.user & env.password

    b. This function runs update & upgrade of OS, and copy and run EXPECT script (for mount encrypted volume)

     import os
     import sys
     import time
     import boto
     import deployment
     import getpass
     import sys
    
     env.user = 'my_user'
     env.password = 'my_sudo_pass"
    
     def staging_auto_mount():
         # install expect
         sudo('apt-get update', pty=False)
         sudo('apt-get -y dist-upgrade', pty=False)
         sudo('apt-get -y autoclean', pty=False)
         sudo('apt-get -y install expect', pty=False)
         # auto mount EBS volume
         sudo('mkdir -p /scripts/', pty=False)
         put('scripts/auto_mount.sh', '/scripts/', use_sudo=True, mode=0755)
         run('/scripts/auto_mount.sh')
         sudo('rm -rf /scripts/')
    
  3. I created BASH script ( /home/scripts/staging_server01.sh ) - I just put in this script command for run FABric function

         cd /scripts/ && /usr/local/bin/fab staging_auto_mount -H XXX.xxx.XXX.xxx
    

My issue is following:

When I run it manually everything works fine and I can run just BASH Script (ad 3.) like

/home/scripts/staging_server01.sh

Or I can run FABric function

cd /scripts/ && /usr/local/bin/fab staging_auto_mount -H XXX.xxx.XXX.xxx

But when I put it into crontab I have problem

How I set crontab job, I tried several things

08 07 * * * cd /scripts/ && /usr/local/bin/fab staging_auto_mount -H XXX.xxx.XXX.xxx --password='my_SUDO_PASS'
08 07 * * * cd /scripts/ && /usr/local/bin/fab staging_auto_mount -H XXX.xxx.XXX.xxx
08 07 * * * /home/scripts/staging_server01.sh

(on my local environment I use same user with same sudo permission like on staging servers. Both users have same PASS for my testing. I also tried run commands in crontab job like specific user)

In log I see this:

/usr/lib/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.

I was thinking to use SSL and run via SSL remote commands, but we often recovery servers, so I would have to very often generate new keys. So this is not way.

I did some investigate and I rewrote my FABric function and I am stuck on this too. and honestly, I have with code bellow much bigger problem [I am not able to create directory & copy file (permission issue, or log told me that directory is already exists) neither when I ran it manually or via crontab]

import os
import sys
import time
import boto
import deployment
import getpass
import sys
import shutil
import os.path

env.user = 'my_user'
env.password = 'my_sudo_pass'
path = "/home/scripts/"

def staging_auto_mount():
    # install expect
    os.system("sudo -p " + "env.password" + " apt-get update")
    os.system("sudo -p " + "env.password" + " apt-get -y dist-upgrade")
    os.system("sudo -p " + "env.password" + " apt-get -y autoclean")
    os.system("sudo -p " + "env.password" + " apt-get -y install expect")

via crontab I can get this error

Message-Id: <20151215212901.3404CA0B02@vm01-ubuntu>
Date: Tue, 15 Dec 2015 15:29:01 -0600 (CST)

    sudo: no tty present and no askpass program specified
    sudo: no tty present and no askpass program specified
    sudo: no tty present and no askpass program specified
    sudo: no tty present and no askpass program specified
    [XXX.xxx.XXX.xxx] Executing task 'staging_auto_mount'

About the error above I tried to set specific NOPASSWORD permission in visudo, but the issue still existed

I will be very grateful for any help

nex.cz
  • 154
  • 1
  • 3
  • 11

1 Answers1

0

here the -S option is suggested:

echo <password> | sudo -S apt-get smth

sudo -S reads the password from stdin.

here is an advice about removimg Defaults requiretty from the /etc/sudoers file

Community
  • 1
  • 1
Hln
  • 749
  • 5
  • 15