1

ok i seen the other questions and i don't think my question applies so here i go...... i'm a programmer just new to bash scripting i want to ssh into my amazon instance and then start my own .sh file pseudo:

{
ssh -i ...
cd /../
./blah.sh
profit!
}

I have multiple(5+) instances saved in a txt file that i would like to loop in so ultimately every instance would have its own ssh dialog.... the text file has the whole command

"ssh -i blah.pem ubuntu@ec2-blah.amazonaws.com"

UPDATE:

#!/bin/bash
while read LINE
    do
[ ! -f /tmp/$(basename $0) ] && cp $0 /tmp/ && konsole -e $0 && exit
rm /tmp/$(basename $0) # open separate window for code to run
    $LINE << EOF # read line (ssh cmd)
    cd /st/task/ #commands during ssh
    ./start.sh #need something to let this run and go back to beginning of loop 
EOF
done < pdns.txt

With your help i had gotten so far and this works... So my new problem is that after the first ssh operation executes it doesn't cycle to the next ssh operation because the start.sh file doesn't end (by design). Need help!

Hn Nm
  • 19
  • 6
  • What is your question? Removing the quotes from start and end of line is the only part which isn't trivial. You don't need to know how many lines there are, just read one at a time until you get nothing. `while read -r line; do : something with "$line"; done – tripleee Dec 17 '18 at 16:17
  • Is `blah.sh` really in the parent directory of your home directory? That should normally be a read-only directory which you can't put files in. – tripleee Dec 17 '18 at 16:19
  • first of all thank you for your response.......and i understand – Hn Nm Dec 17 '18 at 17:03

1 Answers1

0

I guess you are probably looking for something like

while read -r ignore stuff pem host; do
    ssh -i "$pem" "${host%\"}" 'cd .. && ./blah.sh' </dev/null
done <input.txt

If you could remove the quotes and the other junk from the file input.txt so that it only contains the host name and the PEM, and perhaps even avoid storing this information in a separate file, this could be simplified to

while read -r host pem; do
    ssh -i "$pem" "$host" 'cd .. && ./blah.sh' </dev/null
done <<\____HERE
    server1.example.com  server1.pem
    server2.example.net  second.pem
    third.example.org    number3.pem
____HERE

One common source of errors is having ssh gobble up the input you intended for while read -r to read; the redirection </dev/null prevents that from happening.

As an aside, many scripts do not need to run in the directory where they are stored. Perhaps simply ../blah.sh would work just fine as well. Also, tangentially, the script file should probably not have an .sh extension.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • ok thanks again i'm humbled by your knowledge.....(wow this place is awesome) ok so i need talk this out because i don't quite understand your code and im here to learn not just copy someone else work.......so my goal is for the script to run the ssh -i blah.pem user@amazon.aws (which i need to change per iteration) sourced from txt file in spawned in seperate terminals accompanied with the change directory command and finalized by the execution of the script (which extention do you recommend, btw?) – Hn Nm Dec 17 '18 at 20:16
  • The overarching goal is to automate initiating a ssh connection along with a cd command to get to the script i want to run. the ssh cmd in the text file is functional if applied in a bash environment so i'm confused by the modifications....Am i just thinking about this problem wrong logically? – Hn Nm Dec 17 '18 at 20:37
  • You are mixing code and data, the data is the host name and the PEM and the command is `ssh`. The quotes (if indeed they are there, as shown in your example) are definitely a problem. – tripleee Dec 18 '18 at 05:45
  • Scripts should not usually have an extension in their filename at all. It's not too uncommon to want to reimplement a script in another language and then the extension is just wrong. – tripleee Dec 18 '18 at 05:46
  • the first line of code is IN the text file is what i'm saying....i just want to add the last two lines in the while loop now and my new issue is that i'm having trouble running the inputted line of data as code. ` #!/bin/bash while read x do x wait [x] cd /st/task/ ./start done < pdns.txt` – Hn Nm Dec 18 '18 at 16:16
  • Your `wait` will recommence ony after the `ssh` command exits. See https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc – tripleee Dec 18 '18 at 16:18
  • You could modify the text file but again, that's crazy. Separate code from data, put all the code in the script itself. `while read -r junk ignore pem host; do ssh -i "$pem" "${host%\"}" 'cd /st/task && ./start' – tripleee Dec 18 '18 at 17:35
  • what i am not communicating clearly is that the text file has the ssh code in its entirety......so ssh -i user@server `#!/bin/bash while read LINE do [ ! -f /tmp/$(basename $0) ] && cp $0 /tmp/ && konsole -e $0 && exit rm /tmp/$(basename $0) $LINE + " bash -s" << 'ENDSSH' cd /st/task/ ./start ENDSSH done < pdns.txt` what do you think? – Hn Nm Dec 18 '18 at 17:49
  • Like I said, modifying the code in the text file from your script and then executing the text file is technically possible, but brittle and prone to errors and security problems. Putting code in comments doesn't really work; could you perhaps edit your question to post the cnle as well as an explanation of what you hope it would dn if it was correct? – tripleee Dec 18 '18 at 17:55
  • If you are hoping for the code to run *in* the `konsole` window, it's the same question over again. I'm marking this as a duplicate now. – tripleee Dec 19 '18 at 11:52
  • wait a moment....i have already completed that task.... my question is as marked in the code above – Hn Nm Dec 19 '18 at 13:13
  • Then it's really not clear what you hope to accomplish and which part of it isn't working. Code which doesn't do what you want is not a very good indicator of what you do want. But maybe mull over this some more and perhaps eventually post a new question with a more well-defined question. See also the guidance for creating a [mcve]. – tripleee Dec 19 '18 at 13:18
  • The code which runs `$0` in particular seems unrelated to your stated goal, but almost certainly doesn't do what you want. – tripleee Dec 19 '18 at 13:19