Bash Script to remotely collect hostname, IP and host total memory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to write a scrip that will collect the hostname, IP and total memory installed on remote hosts from a list I feed into the script. The script will collect information from Redhat and Solaris machines.Below is what my script looks like:



#!/bin/bash
echo > ip_info.output
echo -e "n"
for host in `cat ip_adds`
do
echo "Hostname:" $host
sudo ssh -o BatchMode=yes -o ConnectTimeout=5 $host "echo IP Address:; ip route get 1 | awk 'print $NF;exit'; free -m | grep Mem | awk 'print $1,$2'"
echo -e "n"
done


When I run the script I get error below:



awk: print ,
awk: ^ syntax error
awk: print ,
awk: ^ syntax error
awk: cmd. line:1: print ,
awk: cmd. line:1: ^ unexpected newline or end of string


I think the problem is with free -m | grep Mem | awk 'print $1,$2 but not sure how to rectify it. If I run free -m | grep Mem | awk 'print $1,$2 directly into the shell I have no issues. Just inside the script.










share|improve this question









New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • What is the echo > ip_info.output doing?
    – terdon♦
    37 mins ago










  • The echo > ip_info.output is writing nothing to the output file so that if I have to run the script again for whatever reason I dont end up with duplicate information.
    – user315468
    28 mins ago










  • Yes, but you don't seem to be using the output file anywhere, so I assume you're running the script as script.sh > ip_info.output, right?
    – terdon♦
    24 mins ago











  • no Im running it as ./script.sh | tee -a ip_info.output. echo > ip_info.output clear the file for when ./script.sh | tee -a ip_info.output is run.
    – user315468
    17 mins ago










  • But if you want to clear the file, why would you use tee -a? There's no reason to use tee at all and certainly no reason to use tee -a. Just run ./script.sh > ip_info.output and that will always overwrite whatever is in the file.
    – terdon♦
    16 mins ago














up vote
1
down vote

favorite












I am trying to write a scrip that will collect the hostname, IP and total memory installed on remote hosts from a list I feed into the script. The script will collect information from Redhat and Solaris machines.Below is what my script looks like:



#!/bin/bash
echo > ip_info.output
echo -e "n"
for host in `cat ip_adds`
do
echo "Hostname:" $host
sudo ssh -o BatchMode=yes -o ConnectTimeout=5 $host "echo IP Address:; ip route get 1 | awk 'print $NF;exit'; free -m | grep Mem | awk 'print $1,$2'"
echo -e "n"
done


When I run the script I get error below:



awk: print ,
awk: ^ syntax error
awk: print ,
awk: ^ syntax error
awk: cmd. line:1: print ,
awk: cmd. line:1: ^ unexpected newline or end of string


I think the problem is with free -m | grep Mem | awk 'print $1,$2 but not sure how to rectify it. If I run free -m | grep Mem | awk 'print $1,$2 directly into the shell I have no issues. Just inside the script.










share|improve this question









New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • What is the echo > ip_info.output doing?
    – terdon♦
    37 mins ago










  • The echo > ip_info.output is writing nothing to the output file so that if I have to run the script again for whatever reason I dont end up with duplicate information.
    – user315468
    28 mins ago










  • Yes, but you don't seem to be using the output file anywhere, so I assume you're running the script as script.sh > ip_info.output, right?
    – terdon♦
    24 mins ago











  • no Im running it as ./script.sh | tee -a ip_info.output. echo > ip_info.output clear the file for when ./script.sh | tee -a ip_info.output is run.
    – user315468
    17 mins ago










  • But if you want to clear the file, why would you use tee -a? There's no reason to use tee at all and certainly no reason to use tee -a. Just run ./script.sh > ip_info.output and that will always overwrite whatever is in the file.
    – terdon♦
    16 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to write a scrip that will collect the hostname, IP and total memory installed on remote hosts from a list I feed into the script. The script will collect information from Redhat and Solaris machines.Below is what my script looks like:



#!/bin/bash
echo > ip_info.output
echo -e "n"
for host in `cat ip_adds`
do
echo "Hostname:" $host
sudo ssh -o BatchMode=yes -o ConnectTimeout=5 $host "echo IP Address:; ip route get 1 | awk 'print $NF;exit'; free -m | grep Mem | awk 'print $1,$2'"
echo -e "n"
done


When I run the script I get error below:



awk: print ,
awk: ^ syntax error
awk: print ,
awk: ^ syntax error
awk: cmd. line:1: print ,
awk: cmd. line:1: ^ unexpected newline or end of string


I think the problem is with free -m | grep Mem | awk 'print $1,$2 but not sure how to rectify it. If I run free -m | grep Mem | awk 'print $1,$2 directly into the shell I have no issues. Just inside the script.










share|improve this question









New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying to write a scrip that will collect the hostname, IP and total memory installed on remote hosts from a list I feed into the script. The script will collect information from Redhat and Solaris machines.Below is what my script looks like:



#!/bin/bash
echo > ip_info.output
echo -e "n"
for host in `cat ip_adds`
do
echo "Hostname:" $host
sudo ssh -o BatchMode=yes -o ConnectTimeout=5 $host "echo IP Address:; ip route get 1 | awk 'print $NF;exit'; free -m | grep Mem | awk 'print $1,$2'"
echo -e "n"
done


When I run the script I get error below:



awk: print ,
awk: ^ syntax error
awk: print ,
awk: ^ syntax error
awk: cmd. line:1: print ,
awk: cmd. line:1: ^ unexpected newline or end of string


I think the problem is with free -m | grep Mem | awk 'print $1,$2 but not sure how to rectify it. If I run free -m | grep Mem | awk 'print $1,$2 directly into the shell I have no issues. Just inside the script.







linux bash centos scripting rhel






share|improve this question









New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 21 mins ago









Rui F Ribeiro

37.1k1273118




37.1k1273118






New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 59 mins ago









user315468

162




162




New contributor




user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • What is the echo > ip_info.output doing?
    – terdon♦
    37 mins ago










  • The echo > ip_info.output is writing nothing to the output file so that if I have to run the script again for whatever reason I dont end up with duplicate information.
    – user315468
    28 mins ago










  • Yes, but you don't seem to be using the output file anywhere, so I assume you're running the script as script.sh > ip_info.output, right?
    – terdon♦
    24 mins ago











  • no Im running it as ./script.sh | tee -a ip_info.output. echo > ip_info.output clear the file for when ./script.sh | tee -a ip_info.output is run.
    – user315468
    17 mins ago










  • But if you want to clear the file, why would you use tee -a? There's no reason to use tee at all and certainly no reason to use tee -a. Just run ./script.sh > ip_info.output and that will always overwrite whatever is in the file.
    – terdon♦
    16 mins ago
















  • What is the echo > ip_info.output doing?
    – terdon♦
    37 mins ago










  • The echo > ip_info.output is writing nothing to the output file so that if I have to run the script again for whatever reason I dont end up with duplicate information.
    – user315468
    28 mins ago










  • Yes, but you don't seem to be using the output file anywhere, so I assume you're running the script as script.sh > ip_info.output, right?
    – terdon♦
    24 mins ago











  • no Im running it as ./script.sh | tee -a ip_info.output. echo > ip_info.output clear the file for when ./script.sh | tee -a ip_info.output is run.
    – user315468
    17 mins ago










  • But if you want to clear the file, why would you use tee -a? There's no reason to use tee at all and certainly no reason to use tee -a. Just run ./script.sh > ip_info.output and that will always overwrite whatever is in the file.
    – terdon♦
    16 mins ago















What is the echo > ip_info.output doing?
– terdon♦
37 mins ago




What is the echo > ip_info.output doing?
– terdon♦
37 mins ago












The echo > ip_info.output is writing nothing to the output file so that if I have to run the script again for whatever reason I dont end up with duplicate information.
– user315468
28 mins ago




The echo > ip_info.output is writing nothing to the output file so that if I have to run the script again for whatever reason I dont end up with duplicate information.
– user315468
28 mins ago












Yes, but you don't seem to be using the output file anywhere, so I assume you're running the script as script.sh > ip_info.output, right?
– terdon♦
24 mins ago





Yes, but you don't seem to be using the output file anywhere, so I assume you're running the script as script.sh > ip_info.output, right?
– terdon♦
24 mins ago













no Im running it as ./script.sh | tee -a ip_info.output. echo > ip_info.output clear the file for when ./script.sh | tee -a ip_info.output is run.
– user315468
17 mins ago




no Im running it as ./script.sh | tee -a ip_info.output. echo > ip_info.output clear the file for when ./script.sh | tee -a ip_info.output is run.
– user315468
17 mins ago












But if you want to clear the file, why would you use tee -a? There's no reason to use tee at all and certainly no reason to use tee -a. Just run ./script.sh > ip_info.output and that will always overwrite whatever is in the file.
– terdon♦
16 mins ago




But if you want to clear the file, why would you use tee -a? There's no reason to use tee at all and certainly no reason to use tee -a. Just run ./script.sh > ip_info.output and that will always overwrite whatever is in the file.
– terdon♦
16 mins ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote













This is considerably more complicated than it needs to be. Also, why would you run ssh with sudo? If you need to log into the remote as root, then you can do that (ssh root@$host) but it's very unlikely you would need to run ssh with sudo unless your ssh keys all belonged to root. Which is a pretty bad idea.



Also, on my system, the command you seem to be using to get the IP returns the UID of my user:



$ ip route get 1
1.0.0.0 via 192.168.1.1 dev enp0s31f6 src 192.168.1.111 uid 1000
cache
$ ip route get 1 | awk 'print $NF;exit';
1000


Don't you mean this?



$ ip route get 1 | awk 'print $(NF-2);exit'; 
192.168.1.111


The errors you saw were indeed because of quoting. Since you were running ssh $host "command", the double quotes around command cause the shell to expand any variables found inside the command (so things like awk's $2 etc). To avoid this and pass the symbols unexpanded to awk, you need to escape the $.



A simpler version of your script:



#!/bin/bash
sshOpts="BatchMode=yes -o ConnectTimeout=5"
echo > ip_info.output
echo ""
while read host; do
printf "Hostname:%s" $host
ssh -o $sshOpts $host "printf 'Hostname: %snIP: %snMem: %sn' $host "$(ip route get 1 | awk 'print $(NF-2);exit')" "$(free -m | awk '/Mem/print $2')""
done < ip_adds





share|improve this answer



























    up vote
    1
    down vote













    I have found an answer for this. It was to do with escaping so I added before $1 and $2 as per below:



    free -m | grep Mem | awk 'print $1,$2





    share|improve this answer








    New contributor




    user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      user315468 is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474996%2fbash-script-to-remotely-collect-hostname-ip-and-host-total-memory%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      This is considerably more complicated than it needs to be. Also, why would you run ssh with sudo? If you need to log into the remote as root, then you can do that (ssh root@$host) but it's very unlikely you would need to run ssh with sudo unless your ssh keys all belonged to root. Which is a pretty bad idea.



      Also, on my system, the command you seem to be using to get the IP returns the UID of my user:



      $ ip route get 1
      1.0.0.0 via 192.168.1.1 dev enp0s31f6 src 192.168.1.111 uid 1000
      cache
      $ ip route get 1 | awk 'print $NF;exit';
      1000


      Don't you mean this?



      $ ip route get 1 | awk 'print $(NF-2);exit'; 
      192.168.1.111


      The errors you saw were indeed because of quoting. Since you were running ssh $host "command", the double quotes around command cause the shell to expand any variables found inside the command (so things like awk's $2 etc). To avoid this and pass the symbols unexpanded to awk, you need to escape the $.



      A simpler version of your script:



      #!/bin/bash
      sshOpts="BatchMode=yes -o ConnectTimeout=5"
      echo > ip_info.output
      echo ""
      while read host; do
      printf "Hostname:%s" $host
      ssh -o $sshOpts $host "printf 'Hostname: %snIP: %snMem: %sn' $host "$(ip route get 1 | awk 'print $(NF-2);exit')" "$(free -m | awk '/Mem/print $2')""
      done < ip_adds





      share|improve this answer
























        up vote
        2
        down vote













        This is considerably more complicated than it needs to be. Also, why would you run ssh with sudo? If you need to log into the remote as root, then you can do that (ssh root@$host) but it's very unlikely you would need to run ssh with sudo unless your ssh keys all belonged to root. Which is a pretty bad idea.



        Also, on my system, the command you seem to be using to get the IP returns the UID of my user:



        $ ip route get 1
        1.0.0.0 via 192.168.1.1 dev enp0s31f6 src 192.168.1.111 uid 1000
        cache
        $ ip route get 1 | awk 'print $NF;exit';
        1000


        Don't you mean this?



        $ ip route get 1 | awk 'print $(NF-2);exit'; 
        192.168.1.111


        The errors you saw were indeed because of quoting. Since you were running ssh $host "command", the double quotes around command cause the shell to expand any variables found inside the command (so things like awk's $2 etc). To avoid this and pass the symbols unexpanded to awk, you need to escape the $.



        A simpler version of your script:



        #!/bin/bash
        sshOpts="BatchMode=yes -o ConnectTimeout=5"
        echo > ip_info.output
        echo ""
        while read host; do
        printf "Hostname:%s" $host
        ssh -o $sshOpts $host "printf 'Hostname: %snIP: %snMem: %sn' $host "$(ip route get 1 | awk 'print $(NF-2);exit')" "$(free -m | awk '/Mem/print $2')""
        done < ip_adds





        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          This is considerably more complicated than it needs to be. Also, why would you run ssh with sudo? If you need to log into the remote as root, then you can do that (ssh root@$host) but it's very unlikely you would need to run ssh with sudo unless your ssh keys all belonged to root. Which is a pretty bad idea.



          Also, on my system, the command you seem to be using to get the IP returns the UID of my user:



          $ ip route get 1
          1.0.0.0 via 192.168.1.1 dev enp0s31f6 src 192.168.1.111 uid 1000
          cache
          $ ip route get 1 | awk 'print $NF;exit';
          1000


          Don't you mean this?



          $ ip route get 1 | awk 'print $(NF-2);exit'; 
          192.168.1.111


          The errors you saw were indeed because of quoting. Since you were running ssh $host "command", the double quotes around command cause the shell to expand any variables found inside the command (so things like awk's $2 etc). To avoid this and pass the symbols unexpanded to awk, you need to escape the $.



          A simpler version of your script:



          #!/bin/bash
          sshOpts="BatchMode=yes -o ConnectTimeout=5"
          echo > ip_info.output
          echo ""
          while read host; do
          printf "Hostname:%s" $host
          ssh -o $sshOpts $host "printf 'Hostname: %snIP: %snMem: %sn' $host "$(ip route get 1 | awk 'print $(NF-2);exit')" "$(free -m | awk '/Mem/print $2')""
          done < ip_adds





          share|improve this answer












          This is considerably more complicated than it needs to be. Also, why would you run ssh with sudo? If you need to log into the remote as root, then you can do that (ssh root@$host) but it's very unlikely you would need to run ssh with sudo unless your ssh keys all belonged to root. Which is a pretty bad idea.



          Also, on my system, the command you seem to be using to get the IP returns the UID of my user:



          $ ip route get 1
          1.0.0.0 via 192.168.1.1 dev enp0s31f6 src 192.168.1.111 uid 1000
          cache
          $ ip route get 1 | awk 'print $NF;exit';
          1000


          Don't you mean this?



          $ ip route get 1 | awk 'print $(NF-2);exit'; 
          192.168.1.111


          The errors you saw were indeed because of quoting. Since you were running ssh $host "command", the double quotes around command cause the shell to expand any variables found inside the command (so things like awk's $2 etc). To avoid this and pass the symbols unexpanded to awk, you need to escape the $.



          A simpler version of your script:



          #!/bin/bash
          sshOpts="BatchMode=yes -o ConnectTimeout=5"
          echo > ip_info.output
          echo ""
          while read host; do
          printf "Hostname:%s" $host
          ssh -o $sshOpts $host "printf 'Hostname: %snIP: %snMem: %sn' $host "$(ip route get 1 | awk 'print $(NF-2);exit')" "$(free -m | awk '/Mem/print $2')""
          done < ip_adds






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 22 mins ago









          terdon♦

          124k29234412




          124k29234412






















              up vote
              1
              down vote













              I have found an answer for this. It was to do with escaping so I added before $1 and $2 as per below:



              free -m | grep Mem | awk 'print $1,$2





              share|improve this answer








              New contributor




              user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                up vote
                1
                down vote













                I have found an answer for this. It was to do with escaping so I added before $1 and $2 as per below:



                free -m | grep Mem | awk 'print $1,$2





                share|improve this answer








                New contributor




                user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  I have found an answer for this. It was to do with escaping so I added before $1 and $2 as per below:



                  free -m | grep Mem | awk 'print $1,$2





                  share|improve this answer








                  New contributor




                  user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  I have found an answer for this. It was to do with escaping so I added before $1 and $2 as per below:



                  free -m | grep Mem | awk 'print $1,$2






                  share|improve this answer








                  New contributor




                  user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 42 mins ago









                  user315468

                  162




                  162




                  New contributor




                  user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  user315468 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




















                      user315468 is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      user315468 is a new contributor. Be nice, and check out our Code of Conduct.












                      user315468 is a new contributor. Be nice, and check out our Code of Conduct.











                      user315468 is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474996%2fbash-script-to-remotely-collect-hostname-ip-and-host-total-memory%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery