I am using phpseclib SSH2 to connect ec2 server. I want to get disk space details in json format. I run the command as below:
$command = "df -h";
echo $ssh->exec($command);
Output is:
Filesystem Size Used Avail Use% Mounted on devtmpfs 475M 0 475M 0% /dev tmpfs 483M 0 483M 0% /dev/shm tmpfs 483M 552K 483M 1% /run tmpfs 483M 0 483M 0% /sys/fs/cgroup /dev/xvda1 30G 2.5G 28G 9% / fs-035d9649ed9793406.efs.us-east-1.amazonaws.com:/ 8.0E 0 8.0E 0% /var/www/html/efsmount/Customer1 /dev/mapper/customervolumegroup-Cus2 5.0G 38M 5.0G 1% /var/www/html/efsmount/Cus2 /dev/mapper/customervolumegroup-Cus3 5.0G 38M 5.0G 1% /var/www/html/efsmount/Cus3 /dev/mapper/customervolumegroup-Cus4 5.0G 38M 5.0G 1% /var/www/html/efsmount/Cus4 tmpfs 97M 0 97M 0% /run/user/1000
The result is in string format. How to convert this to json format?
I convert the above output into array first.
$command = "df -h";
$s= $ssh->exec($command);
$data = trim($s);
$data = array_map(function($line){
$elements=preg_split('/\s+/',$line);
return(array(
'filesystem' => $elements[0],
'size' => $elements[1],
'used' => $elements[2],
'available' => $elements[3],
'use%' => $elements[4],
));
},explode("\n",$data));
Then encode this into json.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.