Know disk space using ssh remotely using shell script
All of the text in the "box" is generated by your login scripts, and not by the df
command. Often, a server set up with login messages will skip showing you those messages if a file called .hushlogin
exists in your home directory. So first off, try creating that file:
ssh rajesh-server "touch .hushlogin"
Next, you don't need the | cat
in your command line, OR the stderr redirection most likely. Try this instead:
ssh rajesh-server 'df -h /db*'
If you still get the messages with the .hushlogin
file in place, then you will need to parse your output. This would do it:
ssh rajesh-server 'df -h /db*' | sed '/^|/d'
This sed
command takes in the output of your ssh command, deletes any lines starting with a pipe character, and prints everything else.
Note that you probably want to consider using a monitoring tool that is build specifically for this kind of task. Many exist, some as RRDTool front-ends. My favourites are Cacti and Munin. These will graph your disk (or network or other) usage so you can see how things change over time.
If you'd like a tool that sends you a warning if you get close to limits, have a look at Nagios, Icinga or Zabbix.
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
I am using following commands to extract the disk space using ssh remotely. but wants to iust store the output instead of all other message as below;
Any help? or any alternative solution?
ssh rajesh-server 'df -h /db* | cat' 2>&1 |-----------------------------------------------------------------| | This system is for the use of authorized users only. | | Individuals using this computer system without authority, or in | | excess of their authority, are subject to having all of their | | activities on this system monitored and recorded by system | | personnel. | | | | In the course of monitoring individuals improperly using this | | system, or in the course of system maintenance, the activities | | of authorized users may also be monitored. | | | | Anyone using this system expressly consents to such monitoring | | and is advised that if such monitoring reveals possible | | evidence of criminal activity, system personnel may provide the | | evidence of such monitoring to law enforcement officials. | |-----------------------------------------------------------------| Filesystem Size Used Avail Use% Mounted on /dev/vx/dsk/xcv01_day/db01_day 200G 154G 44G 79% /db01_day /dev/vx/dsk/xcvg01_day/db01_day 200G 154G 44G 79% /db01_day /dev/vx/dsk/xcvg01_day/db01_day 200G 154G 44G 79% /db01_day
-
Admin about 12 yearsssh -q rajesh-server 'df -h /db* | cat' 2>&1 solved my problem
-
-
Admin about 12 yearsI am using some automation. Is it possible to display on command prompt only?
-
Admin about 12 yearsHi Graham, Thank very much for your useful info. I am trying other tools as you mentioned. But could not get working any of the ssh commands tips. ;(
-
Admin about 12 yearsNo problem, happy to help. What does "could not get working" mean, exactly? Do you get errors? I have a keyboard at home into which I spilled coffee once, so now when I press "s" I get "z". If you have a similarly damaged keyboard, it would be difficult to run either the "zzh" or "zed" commands...
-
Admin about 12 yearsI used all adove command and output is coming "|---" etc...no change in output.
-
Admin about 12 yearsI mean output of all adove commands are same..
-
Admin about 12 yearslet me add more point is that i am using Linux RHEL
-
Admin about 12 yearsThen either the comment lines are being sent to stderr, or they are slightly different from what you posted in your question. Try
ssh rajesh-server 'df -h /db*' 2>/dev/null
instead of the line withsed
.