0

I am currently looking for a solution for executing remote commands on multiple local servers from an input file containing the 'user : password' in the following format:

jboss5:manager:192.168.1.101
database1:db01:192.168.20.6
server8:localnet:192.168.31.83
x:z:192.168.1.151
test:mynet:192.168.35.44
.... and others

Some commands I wish to execute remotely:

cd $HOME; ./start_script.sh; wget 192.168.1.110/monitor.sh; chmod +x monitor.sh; ./monitor.sh

I know there is a utility called "sshpass" but not sure how I could apply this utility for my needs.

I am open to any ideas in order to fulfill my need, any help would be very appreciated!

Thanks

John Doe
  • 9
  • 1
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Aug 31 '18 at 19:28
  • 1
    isn't this question related to some potentiel bash programming? The solution could be a bash script I believe – John Doe Aug 31 '18 at 19:53
  • IMHO your question is on topic for SO. That being said, it is greatly lacking what is called a "good question". Please read [ask] and [mcve]. It is expected that you do research before posting *your code*, and ask for help solving a *specific issue*. In your research, look at `read`, `cut`, reading a file line by line (`while`), `ssh`, `ssh-agent`. `sshpass` is good as well, it tricks the ssh server into thinking someone typed the password. – Nic3500 Sep 01 '18 at 01:26

1 Answers1

0

Did you think about using ssh-keys (check man ssh-keygen)? You will be able to connect without password input ...

However if you cant, just try:

for i in $(< your_file); do
  user=$(echo $i | cut -d: -f1)
  pass=$(echo $i | cut -d: -f2)
  ip=$(echo $i | cut -d: -f3)
  sshpass -p $pass ssh $user@$ip bash -c "you commands &"
done

Instead of using cd $HOME use the full path with yours scripts names.And dont forget the & for send the process in background...

  • See [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor), and ensure that you're quoting correctly (running code in your answer through http://shellcheck.net/ will help); the current code will behave badly with passwords or other values with spaces. – Charles Duffy Sep 01 '18 at 16:42
  • See [BashFAQ #1](http://mywiki.wooledge.org/BashFAQ/001), describing `while IFS=: read -r user pass ip _; do` as a safer practice (and **much** faster than running three copies of `cut`, each in a subshell, for each line). – Charles Duffy Sep 01 '18 at 16:44
  • ...also, [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo) re: `echo` with unquoted arguments – Charles Duffy Sep 01 '18 at 16:46
  • In general, if a question is too broad to answer comprehensively, the right thing is to vote to close it as too broad (or as duplicate of a series of questions each covering the narrower topics individually), rather than to attempt an answer less complete and canonical than what a narrower, better-defined question could receive. – Charles Duffy Sep 01 '18 at 16:47