Log in to Heroku via bash script - bash

I try to write a script in which I call some command from heroku toolbelt. Script works fine till I am login to heroku toolbelt. When I've tried to add heroku's login command during script execution I occured some problems - there is no in heroku toolbelt command such as (command with parameters):
heroku login -u email#mail.com -p 1234qwer
That why I have no idea how to execute heroku login command in bash script. Has anyone got some advice?

For these kinds of things I use expect.
You need to install expect first. If you're on Ubuntu run sudo apt-get install expect
Then in a script, let's call it heroku_login.exp, enter this with the relevant information:
#!/usr/bin/expect
spawn heroku "login"
expect "Email:"
send "YOUREMAIL";
send "\r"
expect "Password (typing will be hidden):"
send "YOURPASSWORD"
send "\r"
interact
Then run expect heroku_login.exp and you should be good to go.

An easier solution to this problem may be to just set the HEROKU_API_KEY environment variable. The Heroku toolbelt will then automatically log you in using that key.

Related

automation script for git cmds

i'm trying to write a bash script that will automate some of the operation that we are doing on daily basic:
1. clone different repos
2. checkout some specific commits
3. compile
4. etc.
my challenge is, that I want this script to be used by different users and I don't want the script to prompt for password for each git cmd.
I thought to get the username + pwd as arguments to the script and then
git clone https://username:pasword#gitlab.../repoName/usename/proj.git
but I always get an error of "TCP connection reset by peer".
obviously running ssh command or https command without the username builtin working for me fine
any idea?
If you must use "https" instead "ssh" try with expect script.
#!/usr/bin/expect -f
spawn git pull
expect "Username"
send "put here your username\r"
expect "Password"
send "put here your password\r"
interact
You can use bash and expect in the same script as well.

Linux expect script

I've spent the better part of 8 hours trying to figure this out with google, so I hope this warrants asking here.
I need a script that will auto-enter a password when I try to connect from my lubuntu image in vmware to a physical device connected by usb.
I've tried at least 50 different scripts I've found online, but none of them worked (or even recognised spawn as a command)
This is my script:
#!/usr/expect
spawn CPY2T_old.sh
expect "root#10.9.8.2's password:"
send "ThePassword"
expect eof
The contents of CPY2T_old.sh is
#!/bin/bash
cd hellolinux/src/Exercise$1
scp $2 root#10.9.8.2:
The above bash script works fine, but I have to enter the password, which is what I'm trying to avoid in the first place. The expect script gives the following when I execute in cmd:
spawn: command not found
couldn't read file "root#10.9.8.2's password:": no such file or directory
The program 'send' can be found in the following packages:
* mailutils-mh
* nmh
Try: sudo apt-get install <selected package>
couldn't read file "eof": no such file or directory
I've downloaded mailutils and nmh at least a dozen times by now as well. Elsewhere I read I need to #echo off at the top, but this command isn't recognised and gives an error.
EDIT: I can't do passwordless ssh to this device, so please don't suggest it.
I see 2 errors: first
#!/usr/expect
You want
#!/usr/bin/expect
That should have caused an error: how are you launching your expect script?
Second
send "ThePassword"
You forgot to hit enter
send "ThePassword\r"
#!/usr/bin/expect
set timeout 60
spawn ssh user#ip
expect "user#ip's password: "
send "Password\r"
interact
Note:Please be sure of all scripts are executable with command $ chmod +x #file_name or $ chmod 700 #file_name.
\r to execute
Github link:https://github.com/asarari207/Lunix_sh

Expect: Bash Scripting with Expect and Postgresql

Necessary Info:
I'm trying to use expect to automate a password involving a command with PostgreSQL in a BASH script. I've tried mimicking several examples I found here on StackOverflow.com and also other resources on the internet with no success.
Issue: I'm still getting prompted for input.
Code:
Extra Info: password is a global variable not shown in this snippet
database_work(){
sudo -u postgres createdb tinyows
sudo python /$1/database.py
expect <<- DONE
spawn sudo -u postgres psql -U postgres -d tinyows < `pg_config --sharedir`/contrib/postgis-2.1/postgis.sql
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
expect eof
DONE
expect <<- DONE
spawn sudo -u postgres psql -U postgres -d tinyows < `pg_config --sharedir`/contrib/postgis-2.1/spatial_ref_sys.sql
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
expect eof
DONE
}
Ending Note: I appreciate any help and advice anyone gives, thank you.
You're trying to solve your carpentry problem with a welder.
This won't work for a number of reasons.
sudo intentionally prevents you from redirecting its stdin, instead reading it from the terminal. This is partly to make password stealing wrappers harder, but is mostly useful so that things like some-command | sudo othercommand work even if sudo has to prompt for a password. To disable this you must pass the --stdin flag to sudo, explicitly telling it you want it to read a password from stdin.
sudo may or may not actually prompt for a password at each invocation, depending on its settings, whether the user ran it recently, etc. expect won't like that.
The better way to do this is to set up sudo so that you don't have to supply a password to perform the desired action, or get the user to run your whole script under sudo first so they can interactively respond to the prompt. Then use a regular shell script, or preferably an automation tooling framework like Ansible or Puppet.
BTW, on anything except ancient PostgreSQL releases you should be using CREATE EXTENSION postgis; not running a loader script through psql.

heroku pg: pull not fetching tables from heroku database

I'm trying to pull a heroku database to my local Windows computer by using heroku bash command
heroku pg:pull HEROKU_POSTGRESQL_COLOR mydatabase --app appname,
when I running above command I get the following error:
'env' is not recognized as an internal or external command, operable program or batch file.!
But local database 'mydatabase' is created, but without any tables.
My heroku app's database has a table in it, but it is not getting pulled to my local database.
Help me to solve it.
a couple of things:
1.When there is an error such as "'env' is not recognized as an internal or external command, operable program or batch file" it means that the system is trying to execute a command named env. This has nothing to do at all with setting up your environment variables.
Env is not a command in windows, but in unix. I understand that you have a windows machine though. What you can do is run "git bash". (You could get it by itself but it comes with Heroku's CLI).
This gives you a unix-like environment where the "env" command is supported, and then you can run the actual heroku pg:pull command.
2.If that still doesn't work, there is a workaround which works,without installing anything extra. Actually this is based on a ticket which I submitted to Heroku so I'm just going to quote their response:
"The pg:push command is just a wrapper around pg_dump and pg_restore commands. Due to the bug you encountered, it sounds like we should go ahead and do things manually. Run these using cmd.exe (The Command Prompt application you first reported the bug). First grab the connection string from your heroku application config vars.
heroku config:get DATABASE_URL
Then you want to pick out the username / hostname / databasename parts from the connection string, ie: postgres:// username : password # hostname : port / databasename. Use those variables in the following command and paste in the password when prompted for one. This will dump the contents of your heroku database for a local file.
pg_dump --verbose -F c -Z 0 -U username -h hostname -p port databasename > heroku.dump
Next you will load this file into your local database. One thing that the CLI does before running this command is to check and make sure the target database is empty, because running this against a database with real data is something you want to avoid so be careful with pg_restore. When running this manually you run the risk of mangling your data without the CLI check, so you may want to manually verify that the target database is empty first.
pg_restore --verbose --no-acl --no-owner -h localhost -p 5432 -d mydb2 < heroku.dump
I am sorry this is not a better experience, I hope this will help you make progress. We are in the process of rewriting our pg commands so that they work better on all platforms including windows, but there is no solid timeline for when this will be completed."
For taking backup like dump file in heroku firstly you need the backups addon, installing..
$heroku addons:add pgbackups
Then running below command will give you dump file in the name of latest
$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
or
wget "`heroku pgbackups:url --app app-name`" -O backup.dump
Edited:(After chatting with user,)
Problem: 'env' is not recognized as an internal or external command, operable program or batch file.!
I suspected that one of the PATH variable to particular program is messed up. You can double click and check that in WINDOWS\system32 folder.
Ok so How to edit it:
My Computer > Advanced > Environment Variables
Then choose PATH and click edit button

Terminal login-credentials to heroku with bash-script?

I'm trying to write a bash script that logs in and creates some stuff on Heroku. Now I have to supply username and password every time. Is it possible to do this automatically, either with some bash-magic or passing variables with the login-function?
PS I'm mostly interested in supplying the e-mail-adress, not the password.
this is what it looks like:
$ heroku auth:login
Enter your Heroku credentials:
Email: email#example.com
Password (typing will be hidden):
Authentication successful.
I would look at using the Heroku API here instead of the CLI. For instance Heroku.rb is probably a good start if you're happy with using Ruby.
https://github.com/heroku/heroku.rb
By writing it as a Ruby script you can then wrap it up in bash or whatever.

Resources