1

I have a large setup program that saves hours and hours when I spin up a new machine, but I cannot get one thing to work right now. I want to fill a TD driver required config file, and since it is a special protected file I'm getting a hassle. My python looks like:

#configure webapi driver
ODBC_PATH       = os.path.join(HOMEPATH, '/etc/odbcinst.ini')
ODBC_SETTINGS   = """\
[FreeTDS]
Description = TD Driver (MSSQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1"""

call_sp('sudo echo "{}" > {}'.format(ODBC_SETTINGS, ODBC_PATH))

where call_sp is just my way of calling a subprocess but allowing args, like Popen does. I was clueless, because it's failing silently (I don't know till I try to run server) and in the shell I don't get prompted for my pw:

cchilders:~/scripts/setup_scripts/dimensional [master]$ sudo echo "[FreeTDS]
Description = TD Driver (MSSQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1" > /etc/odbcinst.ini 
bash: /etc/odbcinst.ini: Permission denied


cchilders:~/scripts/setup_scripts [master]$ sudo echo "somethin" > /etc/odbcinst.ini 
bash: /etc/odbcinst.ini: Permission denied

but sudoing into nano works and I can do it manually:

cchilders:~/scripts/setup_scripts [master]$ sudo nano /etc/odbcinst.ini 
[sudo] password for cchilders: 

Why can't I fill this ini file with text from terminal, and if it isn't possible, how can I automatically fill in python script? Ty

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • 2
    Similar to http://stackoverflow.com/questions/84882/sudo-echo-something-etc-privilegedfile-doesnt-work-is-there-an-alterna. In short, please try `sudo sh -c "echo 'something' > /etc/odbcinst.ini"` – Eric Jan 21 '16 at 20:18

1 Answers1

1

I believe this is essentially a duplicate of this thread.

Have you tried using tee instead?

$ echo '{}' | sudo tee --append //etc/odbcinst.ini 
Community
  • 1
  • 1
earnshae
  • 169
  • 13