You are not logged in.

#1 2009-10-24 09:40:24

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[Solved] bash: script inside a script?

Hello,

I got a bash script that I'd like have as one script and not have to spawn another script in the shell and was wondering if there was a way to do this.  I'll just post the script here so you have an idea what I'm doing.

I'm trying to run a github sync script.  To be able to do this though, I need to run a another script that runs 'expect'.  Expect is a program that read terminal input and then responds when a certain field is read.  In this case, it run ssh-add and then automatically enters my ssh passphrase:

#!/usr/bin/expect
# ssh-agent-pass - Add rsa passphrase to ssh-agent

spawn ssh-add /home/gen2ly/.ssh/id_rsa
expect "id_rsa':"
# send reply (takes about five seconds)
send "thisisreallymysecretpassphrase;)\n"
expect eof
exit

and the github-sync script:

#!/bin/bash
# github-sync - sync files to github

locrepohome="~/.github-home/"

cd $locrepohome
git add $locrepohome  # take snapshot of all contents in current dir
git commit -a -m "weekly update - $(date "+%F")"
eval `ssh-agent`    # Start ssh-agent and enter passphrase automatically
ssh-agent-pass
git reset --hard    # delete remote repository for matching sync
git push            # push changes to remote repository

I'd like to simply things and have everything in one script.  Is this possible?

Last edited by Gen2ly (2009-10-24 20:22:22)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#2 2009-10-24 09:50:35

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: [Solved] bash: script inside a script?

Just a guess:

#!/bin/bash
# github-sync - sync files to github

locrepohome="~/.github-home/"

cd $locrepohome
git add $locrepohome
git commit -a -m "weekly update - $(date "+%F")"
eval `ssh-agent`

echo "spawn ssh-add /home/gen2ly/.ssh/id_rsa\nexpect \"id_rsa':\"\nsend \"thisisreallymysecretpassphrase;)\\n\"\nexpect eof\nexit" | expect

git reset --hard    # delete remote repository for matching sync
git push            # push changes to remote repository

Offline

#3 2009-10-24 19:28:19

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Re: [Solved] bash: script inside a script?

Try

#!/bin/bash
# github-sync - sync files to github

locrepohome="~/.github-home/"

cd $locrepohome
git add $locrepohome  # take snapshot of all contents in current dir
git commit -a -m "weekly update - $(date "+%F")"
eval `ssh-agent`    # Start ssh-agent and enter passphrase automatically

expect <<EOF
spawn ssh-add /home/gen2ly/.ssh/id_rsa
expect "id_rsa':"
# send reply (takes about five seconds)
send "thisisreallymysecretpassphrase;)\n"
expect eof
exit
EOF

git reset --hard    # delete remote repository for matching sync
git push            # push changes to remote repository

Last edited by some-guy94 (2009-10-24 19:32:24)

Offline

#4 2009-10-24 20:20:50

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] bash: script inside a script?

Barrucadu, that's awesome!  I had a lingering thought that it might not work, I thought that only certain especially commands could be piped too.  I did have to tell expect to... expect a file smile:

eval $(ssh-agent)
entpass=$(echo "spawn ssh-add /home/gen2ly/.ssh/id_rsa
expect \"Enter passphrase for key.*\"
send \"realunknownpass\\n\"
expect eof" | expect -f -)
echo $entpass

And everything is working good now.  !!!

Someguy.  I tried yours too but the command exited before it could do anything:

Could not open a connection to your authentication agent.

Thanks for the help guys.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#5 2009-10-25 08:56:48

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [Solved] bash: script inside a script?

why don't you just use an ssh-agent which works on your entire desktop session? i just unlock my keys when i login, so i can use my keys the whole time.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#6 2009-10-25 15:19:39

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] bash: script inside a script?

@ Dieter@be - If I did that though, wouldn't I have to type in a password every time I logged into a bash shell?  If I remember correctly, I'd have to put this in my .bashrc, right?


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#7 2009-10-25 15:25:38

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [Solved] bash: script inside a script?

Gen2ly wrote:

@ Dieter@be - If I did that though, wouldn't I have to type in a password every time I logged into a bash shell?  If I remember correctly, I'd have to put this in my .bashrc, right?

once you unlock your keys your agent will remember then.  it's then just a matter of making sure the "scope" of your agent is correct, and not launching new agents.

have a look at http://bbs.archlinux.org/viewtopic.php?id=81622, there are also various resources about this on the interwebs.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

Board footer

Powered by FluxBB