Linux - Cat command doesn't maintain the file format - linux

I have a Linux script that generates an HTML file with various outputs for various Linux commands.
Here is one of the outputs that creates an intrf.txt. I generated this file using this command
ip link show|sed '=;G'>intrf.txt
I did that as I want the lines of file to have line space between.
If I do the cat intrf.txt command on my shell I can see indeed the line spacing. If I run the script with the function below I see all lines of this file on my browser, but concatenated with no space between.
Maybe this is something simple, but I cannot figure it out.
function net_ifconfig
{
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "<h2 style="background-color:#00FF00"><font size="5"> CHECK 2. LIST OF AVAILABLE INTERFACE</h2>"
ip link show|sed '=;G'>intrf.txt
cat intrf.txt
else
echo "CHECK 1. INTERNET IF OFFLINE"
echo "<h2 style="background-color:#FF0000"><font size="5"> INTERNET IS NOT CONNECTED</h2>"
fi
}

Seems if I put the command between pre tags it works.
echo "<pre>"
ip link show|sed '=;G'>intrf.txt
cat intrf.txt
echo "</pre>"

Related

Capturing SSH Output in a variable in a bash script

I'm trying to SSH into another machine and caputre it's ip address and hostname into a variable.
However, the varaible seems to be empty when i echo it.
I have tried out answers from other posts, but it didnt solve my problem. I'm not able to figure out as what the problem is.
#!/bin/bash
FILE=/home/admin/Vishal/output.txt
input=host.txt
while IFS= read -r line
do
echo "$line"
if [ $line = $HOSTNAME ]
then
ip=`hostname -i`
domain=`hostname -A`
host=`hostname`
sudo echo $ip $domain $line localhost >> $FILE
else
output=$(ssh -i -t admin#$line << "ENDSSH"
ip2=`hostname -i`
domain2=`hostname -A`
host2=`hostname`
ENDSSH
)
echo $output
fi
done <"$input"
The input file contains a list of hostnames
The variable FILE contains the path of the file where the results are to be stored.
The varaible output is the one in which i want to store the results.
Note: The script works for first part of the if where ssh isnt required.
Ony this command is relevant for your quesion:
output=$(ssh -i -t admin#$line << "ENDSSH"
ip2=`hostname -i`
domain2=`hostname -A`
host2=`hostname`
ENDSSH
)
The command sets some variables, but doesn't produce any output, so it's expected that output does't contain your values.
If you really want three lines with hostname related values, then something simple like this should work
output=$(ssh admin#$line "hostname -i; hostname -A; hostname")

why ECHO does not come up?

I have this shell :
===
#!/bin/sh -e
LogFile=/home/pi/logs/prova.log # log file
test -e $LogFile || touch $LogFile # create it if non existent
echo "(1) ======== ======== ======== Inici de PROVA.SH" >> $LogFile
echo "(2) ping 1.2.3.4" >> $LogFile
# ping 1.2.3.4 -W 3 -c 2 >> $LogFile
echo "(3) start APP" >> $LogFile
echo "LOG file is" $LogFile
exit 0
===
The output is
1) one line to screen
2) three lines to file
But if the 8th line (ping 1.2.3.4) is un-commented,
the "echo's" after the 8th line do not get written,
neither to the screen, neither to the file.
I need to understand why, and how to solve it.
I guess is something related to the fact that "ping" runs in another shell,
so the "echo's" write there.
But I don't know how to fix it.
Any pointer or URL to documentation is welcome.
Sebastian.
ping -W 3 -c 2 1.2.3.4 >> $LogFile
Put the IP/Hostname after the ping options.
Most likely:
ping fails because of wrong order of arguments: destination should be last.
Your script runs with -e so it exits at first error, so it stops after ping fails.
You don't redirect standard error for ping : the error message is lost
If you remove -e, ping still fails, but the script continues, executes the last 2 lines and you get their output (but you do not get from ping because that goes to stderr)
Solution, 2 changes:
ping -W 3 -c 2 1.2.3.4 2>&1 >> $LogFile
^^^^^^^ ^^^^
I.P as last argument & Redirect stderr to stdout before redirecting to file

Bash - need to write data to a file based on output of a command in bash

I have a bash script which checks IP Addresses in a file line by line and executes a command with that IP saved in a variable .
I wanted to add a few lines of bash which would write that IP to a new file when the output of the command is something specific.
I would be grateful if anyone shed some light on this matter.
Assuming you stored the output of the command to a variable named output, you can do something like:
# $output is the output you got
# $expected is the expected value
# $IP is IP you just checked
if [ "$output" == "$expected" ]; then
echo $IP >> file.txt
fi
replace file.txt with the name of the file name you prefer.

script to compare text in different files

i've a less knowledge about linux.
i 've a file "iplast.txt" and i need that when the text into this file is not equal to the result of this command http://ipinfo.io/ip the script send me a mail.
i've tried like this
if
[ 'cat iplast.txt' = 'curl http://ipinfo.io/ip' ]
then
echo 'ip same'
else
#send mail command that i already know
fi
but the cat command compare not the file iplast.txt but the word "iplast.txt" whit the curl command.
last thing, it need to work with FFP(Funz Fun Plug)
i tried three day but as i already said i know linux just a little. so pls help me tnk!
Try this:
#!/usr/bin/env bash
curl http://ipinfo.io/ip > ipnow.txt
OUTPUT="$(diff iplast.txt ipnow.txt)"
if [ "$OUTPUT" = ""]; then
echo "ip same"
else
echo "ip different"
fi
I also suggest using python rather than bash scripting if possible. This bash stuff is quite cryptic.

bash - wget -N if else value check

I'm working on a bash script that pulls a file from an FTP site only if the timestamp on remote is different than local. After it puts the file, it copies the file over to 3 other computers via samba (smbclient).
Everything works, but the file copies even if the wget -N ftp://insertsitehere.com returns a value that the file on the remote was not newer. What would be the best way to check the output of the script so that the copy only happens if a new version was pulled from FTP?
Ideally, I'd like the copy to the computers to preserve the timestamp just like the wget -N command does, too.
Here is an example of what I have:
#!/bin/bash
OUTDIR=/cats/dogs
cd $OUTDIR
wget -N ftp://user:password#sitegoeshere.com/filename
if [ $? -eq 0 ]; then
HOSTS="server1 server2 server3"
for i in $HOSTS; do
echo "Uploading to $i..."
smbclient -A /root/.smbclient.authfile //$i/path -c "lcd /cats/dogs; put fiilename.txt"
if [ $? -eq 0 ]; then
echo "Upload to $i successful..."
else
echo "There was an issue uploading to host $i..."
fi
done
else
echo "There was an issue with the FTP Download...."
exit 1
fi
The return value of wget is different than 0 only if there is an error. If -N is in use and the remote file is older than the local file, it will still have a return value of 0, so you cannot use that to check if the file has been modified.
You could check the mtime of the file to see if it changed, or the content. For example, you could use something like:
md5_old=$( md5sum filename.txt 2>/dev/null )
wget -N ftp://user:password#sitegoeshere.com/filename.txt
md5_new=$( md5sum filename.txt )
if [ "$md5_old" != "$md5_new" ]; then
# Copy filename.txt to SMB servers
fi
Regarding smbclient, unfortunately there is no way to preserve timestamps in either get or put commands. If you need it, you must use some different tool (scp -p, rsync -t...)
touch -r foo.txt foo.old
wget -N example.com/foo.txt
if [ foo.txt -nt foo.old ]
then
echo 'Uploading to server1...'
fi
"Save" the current timestamp into a new empty file
Use wget --timestamping to only download the file if it is newer
If file is newer than the "save" file, do stuff

Resources