|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C terminal commands
Hi
I am trying to modify a C program to work for my needs. Problem is I don’t know any real programming. I would really appreciate it if someone could help me! Basically it is to get bandwidth speeds from a remote box. I have two terminal commands that get me the up and down speeds. So how do I in c run the command and set the output to a variable to call later? Yeah it’s cpu but same idea, best example I can give. usage=command command=” cat /proc/stat | grep '^cpu ' ” Printf (CPU speed is %i , usage) |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
You open the file with fopen("filename", "r"), and read lines with fgets(buf, sizeof(buf), fp); and scan until you get the lines you want, and print them.
What have you tried? |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
In the file I have it does it about the same way as you said. Code:
void getValues() {
char line[100];
remove("/tmp/value.txt");
system(leftCmd);
system(rightCmd);
system(ledCmd);
lv1=lv2;
rv1=rv2;
lev1=lev2;
fr = fopen ("/tmp/value.txt", "r");
fgets(line, 80, fr) ;
sscanf (line, "%llu", &lv2);
fgets(line, 80, fr) ;
sscanf (line, "%llu", &rv2);
fgets(line, 80, fr) ;
sscanf (line, "%llu", &lev2);
fclose(fr);
}
void getConfig ()
{
FILE *fc;
char key[100];
char value[200];
char row[1024];
char *p;
fc=fopen("current.cfg","r");
while (!feof(fc) ){
fgets(row,200,fc);
if (strlen(row)<4) continue;
if( sscanf(row,"%[^=]=%[^=\n]",key,value)==2 ) {
if (strlen(key)<4) continue;
if (strcmp (key,"sleepTime")==0) sleepTime=strtol(value,&p, 10);
if (strcmp(key,"leftComunity")==0) strcpy(leftComunity,value);
if (strcmp(key,"leftIP")==0) strcpy(leftIP,value);
if (strcmp(key,"leftMIB")==0) strcpy(leftMIB,value);
if (strcmp(key,"rightComunity")==0) strcpy(rightComunity,value);
if (strcmp(key,"rightIP")==0) strcpy(rightIP,value);
if (strcmp(key,"rightMIB")==0) strcpy(rightMIB,value);
if (strcmp(key,"ledComunity")==0) strcpy(ledComunity,value);
if (strcmp(key,"ledIP")==0) strcpy(ledIP,value);
if (strcmp(key,"ledMIB")==0) strcpy(ledMIB,value);
if (strcmp(key,"leftMax")==0) leftMax=strtol (value, &p, 10);
if (strcmp(key,"rightMax")==0) rightMax=strtol (value, &p, 10);
if (strcmp(key,"ledMax")==0) ledMax=strtol (value, &p, 10);
}
}
fclose(fc);
sprintf(leftCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", leftComunity,leftIP,leftMIB);
sprintf(rightCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", rightComunity,rightIP,rightMIB);
sprintf(ledCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", ledComunity,ledIP,ledMIB);
}
int main(int argc, char ** argv)
{
int difference;
//struct ntptimeval start, elapsed;
struct usb_dev_handle * perf_fs_usb = usb_perf_fs_usb_open();
getConfig();
printf("Starting monitoring:\n");
ntp_gettime(&start);
getValues();
while(1) {
usleep(sleepTime);
getValues();
ntp_gettime(&elapsed);
difference=difference_micro(&start, &elapsed) ;
ntp_gettime(&start);
left=umin((lv2-lv1)*800/difference/leftMax ,100);
right=umin((rv2-rv1)*800/difference/rightMax ,100);
led=umin((lev2-lev1)*800/difference/ledMax ,100);
printf ("sleep %i ",difference);
printf ("display: %i %i %i\n",right,left, led);
writeUSB(perf_fs_usb,right,left,led);
}So If I understand it right sprintf sets “leftcmd” which runs the “snmpget” command and writes the output to values.txt system(leftcmd) is to run the snmpget again to update numbers fopen value.txt , sscanf (line, "%llu", &lv2) sets it to lv2 left=lv2 The issue is the command I need to run and how to handle it is different. The original way gets total packets and compares them to get a speed then divides for percentage. The command I have already gives me “4.5” Mbps. Can I skip the writing it to values.txt and do ‘print in=$1’. Then to get percentage left=in/leftmax. Thanks. |
|
#4
|
|||
|
|||
|
Quote:
Show the input you have, and the output you want. |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Sorry, I mean by taking this Code:
sprintf(leftCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", leftComunity,leftIP,leftMIB);And changing | awk print in=$4 }. By doing that would I be able to call “in” and get the output? As you can tell this is a bit past my knowledge. Here is the run down. I have this command Code:
./check_snmp_int.pl -H 192.168.1.1 -C public -n eth0 -k --label -w 0,0 -c 0,0 -d 15 -MB | grep in | cut -d '=' -f 2| cut -d "/" -f 1| grep [0-9.] | tr -d '[:alpha:]' | awk '{print $1}'Which in a terminal outputs the current Mbps download “4.3”. I would like to be able to run the command and set the output to a variable let’s say “inspeed” Then change it to a percentage of bandwidth used. max down =30 Code:
percent=(inspeed/30)*100 Then echo the numbers Code:
printf (current download is %i, percent) |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Quote:
Code:
./check_snmp_int.pl -H 192.168.1.1 -C public -n eth0 -k --label -w 0,0 -c 0,0 -d 15 -MB | grep in | cut -d '=' -f 2| cut -d "/" -f 1| grep [0-9.] | tr -d '[:alpha:]' | awk '{print $1}'This command is extremely inefficient. Can you show complete output from ./check_snmp_int.pl -H 192.168.1.1 -C public -n eth0 -k --label -w 0,0 -c 0,0 -d 15 -MB and highlight the parts you want, please? You can probably do it all in one awk. You can open commands like Code:
FILE *p=popen("./check_snmp_int.pl ...", "r");
char buf[512];
while(fgets(buf, 512, p) != NULL)
{
printf("Read line %s", buf);
}
pclose(p);...but it would be far better to simplify the command you have than use it blindly. You may not need C at all. Last edited by Corona688; 01-22-2013 at 11:02 AM.. |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
I really appreciate you help me! I don’t have much real knowledge of this kind of stuff, nor know the technical terms. Most of what I know is just from messing around, one of these days I should actually read a book. By “call” I mean be able to input a function? into another line. So if these are the right terms. variable=function math= (2+1)/2 answer=math but be able to do that with the command I have, with the output being assigned a variable Yeah I realized that the command was pretty messy but it was the only way I could figure it out. The original output is Code:
eth0:UP (in=0.0Mbps/out=0.0Mbps):1 UP: OK the issue I had was that the numbers scale? Up . for example in=5.0, in=10.4, in=28.3. This was the only way I could figure out how to get the numbers regardless if it was three or two digits. The main purpose of the program is to write the percentage to an usb device. I figure if I could get as far as printf the percentage, the next step would be easy. Anyways the writing to usb command is just Code:
writeUSB(perf_fs_usb,downspeed,upspeed) |
| The Following User Says Thank You to milestails For This Useful Post: | ||
dhanda2601 (01-24-2013) | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Terminal autorunning commands at start | randomtypos | OS X (Apple) | 1 | 08-15-2011 11:11 AM |
| help with simple terminal commands | omega666 | UNIX for Dummies Questions & Answers | 2 | 01-17-2011 11:20 AM |
| commands in the terminal | Oregano | Shell Programming and Scripting | 3 | 03-29-2010 04:23 PM |
| How do i find all the commands entered by root on any terminal | vishnu787 | Security | 13 | 10-31-2008 11:33 AM |
| Terminal Commands | indigoecho | UNIX for Dummies Questions & Answers | 5 | 12-16-2003 12:41 AM |
|
|