F
F
fokin_nikolay19892019-01-16 09:08:08
linux
fokin_nikolay1989, 2019-01-16 09:08:08

How to make sh work?

Good day!
I wrote a code for connecting to remote workstations and executing commands on it, but not everything works so smoothly, I don’t know how to make it read ip from a file and how to make it execute " grep DOMAIN=..."on a remote host

#!/bin/bash
clear
echo "Подключаемся к хосту"
ssh [email protected] << EOF
yum -y screen
grep 'DOMAIN=' /etc/sysconfig/network-scripts/ifcfg-eth0 &> /dev/null; if [ $? -eq 0 ]; then export DOMAIN=locl.dom && sed -i '' -e "s|DOMAIN=.*$|DOMAIN=$DOMAIN|" /etc/sysconfig/network-scripts/ifcfg-eth0; else echo "DOMAIN=locl.dom" >> /etc/sysconfig/network-scripts/ifcfg-eth0; fi
service network restart

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Ratkin, 2019-01-16
@fokin_nikolay1989

If I correctly understood all your wishes for this crutch, then this is what happened:
The script itself:

#!/usr/bin/env bash

FILE="/path/to/file_with_ip/ip"
USER="root"

while read -r ip_addr; do
    
ssh "$USER"@"$ip_addr" << "EOF"
    interface=$(/usr/sbin/ifconfig -a | grep "UP" | grep -v "lo" | awk -F ": " '{ print $1 }')
    if grep 'DOMAIN' /etc/sysconfig/network-scripts/ifcfg-$interface &>/dev/null; then export DOMAIN=SIZ37.LAN && sed -i -e "s|DOMAIN=.*$|DOMAIN=$DOMAIN|" /etc/sysconfig/network-scripts/ifcfg-$interface; else echo "DOMAIN=locl.dom" >> /etc/sysconfig/network-scripts/ifcfg-$interface; fi
    service network restart
EOF

done < <(cat $FILE)

The file of IP addresses must end with an empty line, otherwise you will lose the last address:
$cat /path/to/file_with_ip/ip
172.16.0.1
172.16.0.2

But, I would add some logging, at least to the console. And then there are several MB interfaces. And everything else. How then to catch on which servers something went wrong without logging is an open question.

I
Ivan Shumov, 2019-01-16
@inoise

Ansible is in your hands with so many stations

R
Radjah, 2019-01-16
@Radjah

Throw the script with scp and execute it without all this.
Logging in as root via ssh is a bad practice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question