How do I wrtite the current IP of tun0 into a txt file with a shell script? - linux

#!/bin/sh
/sbin/ifconfig tun0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}' > ./tun0_ip.txt
This works very good when I put it into the command line.
However it does not work with a sh script.
What am I doing wrong?
Why do some commands not work with a sh script?
~$ bash -x ./reset.sh
+ echo /sbin/ifconfig tun0
+ grep 'inet addr:'
+ cut -d: -f2
+ awk '{print $1}'

It is strange that you are testing with bash, while giving #!/bin/sh as shebang line.
However this is probably not the reason this does not work. Definitely, whether you run some commands from the command line or from a script with the exact same environment, they should not give you a different result.
Maybe the user that calls the script has a different locale, so the output of ifconfig is no more 'inet addr' but something else? Or some environment variable that changes the form of ifconfigs output is not exported?
You can find out only, by going step by step. Looking at the script it should be some problem with ifconfig not giving you, what you expect. So simply take a look at the output of that from the script, unchanged.

Related

Calling a command variable on Bash [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
Im having trouble calling a variable that should bring out the output of a command.
#!/bin/bash
ipAddresses = 'ifconfig | awk -v OFS=": " -v RS= '$1!="lo" && split($0, a, /inet addr:/) > 1{sub(/ .*/, "", a[2]); print $1, a[2]}''
echo -e "Sus direcciones IP son: \n " $(ipAddresses)
Appreciating any advice
Variable assignments cannot have space around the = in the shell. Also, you don't want single quotes there, you want either backticks or $(). The single quotes should only be for your awk command. Your awk is needlessly complicated as well, and you are using command substitution ($()) when printing, but ipAdresses is a variable, not a command.
Try something like this:
#!/bin/bash
ipAddresses=$(ifconfig | sed 's/^ *//' | awk -F'[: ]' '/^ *inet addr:/{print $3}')
printf 'Sus direcciones IP son:\n%s\n' "$ipAddresses"
But that is really not portable. You didn't mention your OS, but I am assuming it's a Linux and the output suggests Ubuntu (I don't have addr after inet in the output of ifconfig on my Arch, for example).
If you are running Linux, you could use grep instead:
ipAddresses=$(ifconfig | grep -oP 'inet addr:\K\S+')
ip is generally replacing ifconfig, so try this instead:
ipAddresses=$(ip addr | awk '/inet /{print $2}')
or
ipAddresses=$(ip addr | grep -oP 'inet \K\S+')
Or, to remove the trailing /N:
ipAddresses=$(ip addr | grep -oP 'inet \K[\d.]+')
And you don't need the variable anyway, you can just:
printf 'Sus direcciones IP son:\n%s\n' "$(ip addr | awk '/inet /{print $2}')"
I am not sure about your intention, since they are not stated, so I am trying to guess them from the script.
Option 1: you are trying to get IP address to into the variable ipAddresses and that is not happenning.
Start by changing single quotes around the long command and debug the command.
Option 2: you are storing a command in variable ipAddresses that you want to execute on the second line.
For both of the options you need to use the the value of the variable through $ipAdresses on the second line.
Also fix the assignment to following formart:
varName="value" # Note no spaces around = sign
Replace the final $(ipAddresses) with ${ipAddresses} or just "$ipAddresses", but also save the output of your command using $().
Check Difference between ${} and $() in Bash.
A basic example:
#!/bin/sh
OUTPUT=$(uname -a)
echo "The output: $OUTPUT"

How to turn off auto add escape character feature in Linux

I am trying to pass a command to a remote server using ssh. while my commands have some characters like ", $, ', \ which often requires a backslash as a escape character except ' (single quote), but the system is automatically taking an escape character \ before the single codes while execution. Can some one help me how to turn off this.
OS : RHEL
my Code :
ssh -q $server "ps -ef | grep mongo | grep conf | awk '{print \$(NF-2)}'
While execution, the code becomes
ssh -q $server "ps -ef | grep mongo | grep conf | awk \'{print $(NF-2)}\'"
I need to turn off this feature
Your analysis isn't really correct. Anyhow, there is no particular reason to run Awk remotely, or grep at all here (because Awk does all of that nicely and efficiently with a very minor refactoring):
ssh -q "$server" ps -ef |
# This runs locally instead
awk '/mongo/ && /conf/ {print $(NF-2)}'

Unsing integer Variable to process linux cut command fields

The following command below does not succeed.
for i in {1..5} ; do cat /etc/fstab | egrep "(ext3|ext4|xfs)" | awk '{print $2}' | cut -d"/" -f1-$i ; done
It seems that $i is ignored completely. It always returns instead result of
cut -d"/" -f1-
Any idea why it fails?
Thanks in advance!
The command itself is a part of a script that should help me to auto re-arrange fstab lines to match the right mount order (like /test/subfolder must come after /test was mounted and not before).
I tried and it didn't work for zsh shell. BUT I tried it in bash and it does work, so if you are using zsh just run the command with bash and it should work ;)

Combining two bash commands

If found this code
host raspberrypi | grep 'address' | cut -d' ' -f4
which gives pi Ip address
and this
wget --post-data="PiIP=1.2.3.4" http://dweet.io/dweet/for/cycy42
which sends 1.2.3.4 off to dweet.io stream
How can I get the output from 1st to replace the 1.2.3.4 in second please?
Save the output of the first command in a variable:
ip=$(host raspberrypi | grep 'address' | cut -d' ' -f4)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42
Btw, if your raspberrypi is running raspbian,
then a much cleaner way to get the IP address:
hostname -I
Simplifying the commands to:
ip=$(hostname -I)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42
Making that a one-liner:
wget --post-data="PiIP=$(hostname -I)" http://dweet.io/dweet/for/cycy42
UPDATE
So it seems hostname -I gives a bit different output for you.
You can use this then:
ip=$(hostname -I | awk '{print $1}')
To make it a one-liner, you can insert this into the second line just like I did in the earlier example.

How can use one ssh for two commands to save 2 different files

How can use one ssh for two commands to save 2 different files. Like one command is ps -ef | grep Consumer | cut -f6 -d' ' and save this output in file.log, second command is ps -ef | grep Test | cut -f7 -d' ' and save output in test.log
Only ps -ef needs to be run on the remote system. Parsing the output can happen at local system.
& It's easier with awk. Needs just single ssh session & ps -ef command snapshot:
ssh user#host ps -ef | awk -F' ' '/Consumer/{print $6 > "file.log"}; /Test/{print $7 > "test.log"}'
grep+cut can happen within awk - '/pattern/{print $n}'
File redirection can also happen easily with awk. Check the syntax in above answer.
I would rather prefer to do parsing on remote system only as ps -ef gives a big output and if we don't parse and cut it over there then entire output is transferred from remote system to local system over network. It can take more time if output size increases. And we don't even need entire output on local system too so it is better parse it on remote system only.
ssh user#host ps -ef | grep Consumer | cut -f6 -d' ' > file.log; ps -ef | grep Test | cut -f7 -d' ' > test.log

Resources