I am trying to run a for loop which looks something like this
cat all_data |
awk '{print $1 " "$2" " $3}' |
while read inst type serial ; do
echo $inst
if [ `echo ${inst} | cut -d '-' -f2` = H3 ]; then
ssh -t username@hostname3 "sudo ls"
elif [ `echo ${inst} | cut -d '-' -f2` = H2 ] ; then
ssh -t username@hostname2 "sudo ls"
elif [ `echo ${inst} | cut -d '-' -f2` = H1 ]; then
ssh -t username@hostname1 "sudo ls"
fi
done
I am not able to run the ssh -t part,it says psuedo terminal will not be allocated becasued stdin is not a terminal sudo: no tty present and no askpass program specified I have tried with -t -t and -n, no luck. I cant edit the visudo file from the host i am running this command from.
Edit
$ cat all_data
ABC-DIF2 DELL-800 60999
ABC-DIF3 HP-DL340 J0777
ABC-DIF4 DELL-800 P0087
.
.
.
The all_data contains 1000's of the similar entries. What i am trying to do it as follows. 60999,J0777... are the entities on which i need to run commands on.
So i am trying to read each line, split up and check what is the DIF number corresponding to the entity.
eg If i read DIF2, it means that i need to ssh into hostname2 and run the command to get data on the 60999
If if i read DIF3, it means for the entity J0777 can only be run from hostname3.
ssh -o Batchmode=yes
instead ofssh -t
. BTW, your script repeats itself a lot. e.g. instead of running theecho | cut
three times, run it once and assign the output to a variable, then use the variable in the test. e.g.inst2="$(echo "$inst" | cut -d- -f2)"
andif [ "$inst2" = H3 ] ; then ...
. The if/elif/elif is also redundant if you're using bash, it can be done in one line[[ "$inst2" =~ H3|H2|1 ]] && ssh ...
– cas Jul 23 '17 at 01:01ssh-agent
– cas Jul 23 '17 at 01:04all_data
file, and a description of what it is you're actually trying to do? My guess is that what you want is probably quite simple but you're over-complicating the procedure. BTW, i just noticed that "H2" triggers an ssh to a different hostname (i initially thought they were all the same). – cas Jul 23 '17 at 02:08"sudo ls"
to"echo PASSWORD | sudo -S ls"
. Where PASSWORD is username's password on the host for sudo. Even in a Perl or Python script, you would likely need the password for sudo in cleartext in the script. – Deathgrip Jul 23 '17 at 04:10