M
M
mirus2014-06-10 21:54:12
bash
mirus, 2014-06-10 21:54:12

How to write a loop for reading data from a file, splitting the data into variables and making a connection?

There is a file with content

37.33.61.72;root;123456
46.55.118.33;root;root
62.21.72.67;root;12345
87.25.74.122;root;root
89.28.30.43;root;root

Here you need to read it from the file and output it in one line in the form $host $login $pass
then insert this data into a line in this format
sshpass -p $pass ssh [email protected]$host
and if everything is fine, output Ok, and if the data is not correct, then output bad
, I can’t read from lines and break data into variables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2014-06-10
@merryjane

For example, how you can get the desired command from each line of the list:

#!/bin/bash
while read -r LINE;
do
  awk -F';' '{print "sshpass -p "$3" ssh " $2"@"$1}'
done < ${1}

The script itself is launched like this:
The output will be:
sshpass -p 12345 ssh [email protected]
sshpass -p root ssh [email protected]
sshpass -p root ssh [email protected]

Next, execute the received command and parse some sign to determine whether the connection passed or not and issue OK or BAD depending on this.

X
xotkot, 2014-06-10
@xotkot

If I understand correctly, it will look something like this:
where x.log is your file from which the data is read
> "and if everything is fine output Ok"
where to output?) if successful, you will log in to the remote machine and you will have another console in front of you.
> "and issue depending on this OK or BAD"
on failure, you will be given something like this:
agree that this is a little more informative than just BAD, at least it’s clear which address has failed, and it’s probably more correct to throw the error output into the log file so that you can then analyze if anything

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question