Shell script not able to parse host list and host unknown - linux

I have a script that I am trying to run. The purpose of this script is to run single or multiple commands on list of servers. When running it, the script is not able to parse the hosts file and gives an error:
ssh: Could not resolve host name nodename nor servname provided, or not known
while read host;
do
echo server: $host
sshpass -p 'password' ssh -o "StrictHostKeyChecking no" admin#$host 'command'
done < /path/to/hosts.txt
hosts.txt contains list of IP addresses of the hosts that need to connect in the following format:
server 1
server 2
server 3
I have tried putting the server IPs in "" and '' and ; and ,, but they all give the same error.
Can someone explain what I am doing wrong here?

Please check if $host variable is getting parsed properly. Also, in your hosts.txt, there are two entries in each row. What does it signify?
I have created a sample script to parse space-separated arguments
Input file: hosts.txt
Script output:
#!bin/sh
while IFS=' ', read hostname ipaddress;
do
echo server: $ipaddress
done < hosts.txt

#Cyrus and #Gordon Davisson thank you -n resolved the issue. such a simple fix i am surprised i didnt see that post here before despite of lot of searching. guess i wasnt wording it the right way. Thank all for the input.

Related

bash script to check connectivity in multiple servers (avoid ECDSA key are you sure you want....)

not an linux expert but I´m trying to see a way of using a bash script to check connectivity tu multiple servers and avoid any human intervention.
I have something like this, so that I can connect to a host, execute hostname and put that info in a log
#!/bin/bash
for host in $(cat server.txt)
do
ssh -q -o ConnectTimeout=3 "$host" "hostname"
done > servers.log
Is this possible to execute and avoid the
ECDSA key fingerprint is a3:eb:a8:4d:90:1d:ae:83:bc:7b:a9:47:69:67:67:7f.
Are you sure you want to continue connecting (yes/no)?
?
or just to force toy say YES?
thank you
To suppress this warning in ssh command use:
ssh -o "StrictHostKeyChecking=no" user#host
Also use while read loop instead of for loop like this:
while read -r host; do
ssh -tt -oBatchMode=yes -o "StrictHostKeyChecking=no" "user#$host"
done < server.txt > servers.log

How to create an effective WHOIS Script in Linux

Disclaimer: I'm a noob to linux and scripting, please don't heckle me.
GOAL: Feed my linux system a .txt file full of IP Addresses and Perform a WHOIS look on each IP and show me specific fields (grep) such as the Organization Field. Bonus points if someone can help me figure out how to use my API key to check my IP list against abuseipdb.com
I've created an ip.txt file with my IP Addresses and I've tried using the following syntax. I'm using Kali Linux to perform this, but it worked on my friend's Fedora system.
sudo for ip in $(cat ip.txt); do whois $ip | echo "$ip $(grep -e 'Organization' | grep -v 'Verizon')"; done > whois.txt
My output is: -bash: syntax error near unexpected token 'do'. Remove "do" from my command and then I receive -bash: syntax error near unexpected token 'done'
Remove the "done" and the pipe out and then it's just pissed..
Please help a linux n00b :)
You are attempting to invoke sudo on the command for ip in $(cat ip.txt), and then running the syntactically invalid commands do whois .... (Note that for ip in $(cat ip.txt) is also syntactically invalid, but sudo is never invoked so no shell ever tries to parse that string as a command). You need to narrow the scope of sudo. eg
while read ip; do sudo whois "$ip" | ... ; done > whois.txt < ip.txt

Need help for simle bash script

Can someone help me with the syntax for a simple bash script that trying to write:
echo ping -c 1
echo nslookup
Basically I want to receive output of one line from the ping and the nslookup information for a domain that I'm checking. Unfortunately I'm unable to get this correctly.
P.s. this is basically the first thing that I'm trying to accomplish in bash.
Thank you in advance!
Thank you for the provided information on the matter. I felt little ashamed from the nature of my question so I spent little more time to read. The solution that I found is the following:
#!/bin/bash
for i in $*;do
ping -c 1 $i &
nslookup $i &
done
#
'
Once I added the scrit to the /bin folder I used the commands:
chmod +x "script name"
dos2unix ""scrit name"
so not I'm able to use it only by typing the name of the script.
hi after writing and saving script and giving permission to the script
just go the folder and
./
of else you just can use
sh
Hi i would be better if you elaborate your problem, if you want one line output for ping command filter using head
eg
ping www.google.com | head -n 1
or if you are thinking of sending one packet of data to the server
ping -n 1 www.google.com
nslookup www.google.com
and if your are writing Bash Script
#!/bin/bash
ping -n 1 www.google.com
nslookup www.google.com
save the file
give execute permission and run
Please let me know i answered your question

Using SSH to grep keywords from multiple servers

I am completely new to scripting and am having some trouble piecing this together from some other online resources.
What I want to do is run a bash script that will grep for a keyword domain in the /etc/hosts file on multiple servers. In the output file, I am looking for a list of the servers that contain this keyword but am not looking to make any changes. Simply looking for which machines have this value. Since there are a bunch of machines in question, listing the servers I am looking to search for won't work, but the machine I am doing this from does have SSH keys for all of the ones in question.
I have a listing of the servers I want to query in three files on the machine (one for each environment) I am going to run this script from.
Linux.prod.dat
Linux.qa.dat
Linux.dev.dat
Each file is simply a list of server names in the environment. For example..
server1
server2
server3
etc...
I am totally lost here and would appreciate any help.
Here is an example:
KEYWORD=foo
SERVERLIST=Linux.prod.dat
OUTPUTLIST=output.dat
for host in $(cat ${SERVERLIST}); do
if [[ -n "$(ssh ${host} grep '${KEYWORD}' /etc/hosts && echo Y)" ]]; then
echo ${host} >> ${OUTPUTLIST}
fi
done
Try GNU parallel
parallel --tag ssh {} grep -l "KEYWORD" /etc/hosts :::: Linux.prod.dat
parallel run command multiple times substituting{}with lines from Linux.prod.dat file.
--tag switch adds value from the Linux.prod.dat on the beginning of the file. So, the output of the command will look like:
server1 /etc/hosts
server5 /etc/hosts
server7 /etc/hosts
Where server1, server5, etc. will be names of the servers where /etc/hosts contains KEYWORD

Executing two command at remote server and storing the output to local server using shell scripting

I'm trying to clean up my shell script. I am trying to read the list of server for
a text file "serverlist" and shh each server to get the outcome of the command and store in to a file "result_segfault". I managed to do that however for each server i wanted to list the name of the server and the output below:
Example :
-----------------
Servername
-----------------
Output of the command
----------------
Servername2
------------------
Output of the command
This is my code
#!/bin/bash
for HOSTNAME in $(cat serverlist);
do
SCRIPT="cat /var/log/messages | grep 'segfault at 0000000000000098'"
for HOSTNAME in ${HOSTNAME} ;
do
ssh ${HOSTNAME} "${SCRIPT}" >"result_segfault"
done;
done
I didn't know how to add the Server name and the separator
{
echo "----------------"
echo "$HOSTNAME"
echo "----------------"
ssh "$HOSTNAME" "${SCRIPT}"
} >>"result_segfault"
The braces {...} group statements together so that their stdout can be collected with a single redirect. I used ">>" in place of ">" for the redirect because it appeared from your sample that you wanted to append together the results from each host rather than overwrite it each time a new host is queried.
With various stylistic fixes;
#!/bin/bash
while read HOSTNAME
do
ssh ${HOSTNAME} grep 'segfault at 0000000000000098' /var/log/messages
done <serverlist >result_segfault

Resources