Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending file from command prompt output

Status
Not open for further replies.

Sunnmann

Technical User
Oct 11, 2003
10
US
Hello all,

I beginning in perl and am having an issue here at work trying to get something to work properly.

I use a program called srvinfo.exe to get information about the PC's I am admin on without having to go pc by pc down on the production floor, and server rooms. What i am trying to do is write a script that will run this in command prompt using an input file that has the names of each PC on them, then it outputs to another file the information it ussually prints out to the command prompt window.

Before I go on, here is my code:

#!/usr/local/bin/perl

open (PCINFO, "pcinfo.txt") or die "I could not get at pcinput.txt";

while (<PCINFO>)
{
$srv=$_;
open (SRVINFO, &quot;>>srvinfo.txt&quot;) or die &quot;I could not open

srvinfo.txt&quot;;
print SRVINFO &quot;srvinfo -ns \\\\$srv&quot;;
close SRVINFO;

system &quot;srvinfo -ns \\\\$srv >> srvinfo.txt&quot;;


open (SRVINFO, &quot;>>srvinfo.txt&quot;) or die &quot;I could not open

srvinfo.txt&quot;;
print SRVINFO &quot;\n===========================\n\n&quot;;
close SRVINFO;
}
close PCINFO;

So, my input file would look tlike this:
rrc7202-desk
rrc7852
snmhp101
(so on and so forth)

ok, if I only have 1 PC in teh list, it works fine and my output file will look something like this:

srvinfo -ns \\rrc7202-desk
Server Name: rrc7202-desk
Security: Users
Registered Owner: Intel Corporation
Registered Organization: Windows 2000 Single User Network-Centric Bullpen

(A BUNCH MORE INFO WAS HERE, BUT UNFORTUNATELY IT IS INTEL CONFIDENTIAL)

System Up Time: 0 Days, 15 Hr, 25 Min, 41 Sec

===========================

So, that works and is what i am looking for.

Now, if I have more than 1 PC in the list, (lets say 4 C's) It will only append the last PC in the list to teh output file, and the others are output to the command prompt window. The output file looks like this:

srvinfo -ns \\rrc7852

===========================

srvinfo -ns \\snmhp101

===========================

srvinfo -ns \\rrc7202-desk
Server Name: rrc7202-desk
Security: Users
Registered Owner: Intel Corporation
Registered Organization: Windows 2000 Single User Network-Centric Bullpen

(A BUNCH MORE INFO WAS HERE, BUT UNFORTUNATELY IT IS INTEL CONFIDENTIAL)

System Up Time: 0 Days, 15 Hr, 25 Min, 41 Sec

===========================

So, I am thinking that my problemis with the line:

system &quot;srvinfo -ns \\\\$srv >> srvinfo.txt&quot;;


Can anyone help me to get it to print EVERYTHING into teh text file and not just the last PC in the list?

Let me know if you need anymore information.
 
Yes, your problem is with the line you think it is. In that line, you are using a windows method to output to a file. Everywhere else you are using a perl method to output to a file. It is bad to mix the two.

Execute your servinfo command using backticks instead of the system call. Assign the commamd to a variable. Using backticks causes the result of the command, which normally goes to STDOUT, to be saved to the variable instead. Then use a print SRVINFO statement to put the data into a file.

#!/usr/local/bin/perl

open (PCINFO, &quot;pcinfo.txt&quot;) or die &quot;I could not get at pcinput.txt&quot;;
open (SRVINFO, &quot;>>srvinfo.txt&quot;) or die &quot;I could not open srvinfo.txt&quot;;

while (<PCINFO>){
chomp;
$srv=$_;
$result = `srvinfo -ns \\\\$srv`;
print SRVINFO &quot;$result\n&quot;;
print SRVINFO &quot;\n===========================\n\n&quot;;
}
close PCINFO;


Notice that the backticks are ` ` and not ' '. The backtick symbol is on the same key as the tilde ~.

I moved your open SRVINFO statement outside of the while loop. It is better to open your file for writing and leave it open until you are done, rather than constantly opening and closing it.

I added a &quot;chomp&quot; statement to your while loop. This will get rid of the newline character at the end of each PC listed in PCINFO. The pesky newline causes problems with reading in computer names - believe me this is something I do on a regular basis with Windows machines.
 
Thank you very much. That totally worked. I at first had the OPEN SRVINFO statement on top, but when the system command runs, if the file is open it errors out.

This has helped me out alot here and i once again thank you very much. I did add another rpint line to add the name of the PC at the top. This is incase I get a network error cause the PC is down or the T.O. port is not working. If I get that error, it does nto tell em what machine, but with the print statement I will know now.

Even once again i thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top