sanPerl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have created a perl script (say script #1) which can create a report from an individual machine and keep the report file at a specified location on the same PC.

In my organization around 100+ PCs are connected to a domain. I have written another script (say script #2) to copy the said report from PC and add the report' data to a database.
My probelm is I don't know that at a given moment, when I run script #2, how many PCs are connected to network. Is there any way where I can get IP addresses/Names of all the PCs connected to Network when I run script #2, so that I can scan those PCs and add their reports to database?

Regards,
Sandeep

Update:- Dear Monks,
I would like to thanks all of you for helping me, Meanwhile I got some other code, which has solved my problem, I would like to share that code with you.
use Socket; use Win32::Lanman; Domains_im_Netz(); foreach $domain (@Domains) {Win32::Lanman::NetServerEnum( '', $domain, SV_TYPE_ALL, \@W1); foreach (@W1) {$hostname = ${$_}{'name'}; my $ip = gethostbyname($hostname); if ( defined $ip ) {print "$domain\t", inet_ntoa($ip), "\t $hostname\n";} } } sub Domains_im_Netz {# zeigt die Domainen im Netzwerk an use Win32::OLE; @Domains = (); $dom = Win32::OLE->GetObject("WinNT:"); foreach $AG (in $dom) {if ($AG->{Name} eq 'TRUMPF' ) {next;} push @Domains, $AG->{Name}; } } ## Domains_im_Netz # end code
  • Comment on How can get all the terminal names/IP addresses connected to network
  • Download Code

Replies are listed 'Best First'.
Re: How can get all the terminal names/IP addresses connected to network
by marto (Cardinal) on Oct 18, 2005 at 09:47 UTC
    Hi sanPerl,

    A non Perl way to do this would be to use Nmap to quickly scan your network and then you could manipulate its output to fit your requirements.
    Have a look on Cpan for modules relating to Nmap, a couple of them look like they would be of some use to you.

    Hope this helps.

    Martin
Re: How can get all the terminal names/IP addresses connected to network
by g0n (Priest) on Oct 18, 2005 at 10:08 UTC
    I take it by 'domain' you mean windows domain rather than broadcast domain. As suggested by marto, nmap will allow you to check (among other things) certain address ranges, or the current IP broadcast domain. If you want to get the addresses of machines currently attached to your windows domain, something like nbtstat on the domain controller might get you further.

    Update: Theres a perl interface to NetBios name resolution here.

    --------------------------------------------------------------

    $perlquestion=~s/Can I/How do I/g;

Re: How can get all the terminal names/IP addresses connected to network
by tirwhan (Abbot) on Oct 18, 2005 at 10:33 UTC

    If the machines are really all on your network (i.e. no routers need to be traversed between the scanning machine and the others) you can use Net::ARP to find out whether they are up. Do a Net::ARP::arp_lookup on each IP address, the host is up if its MAC address is returned. This will work even if the hosts have a firewall which disallows incoming connections.

      I have an application where I want to get MAC addresses from a network. However, everything is Losedows. AS Perl's PPM can't find Net::ARP (or even anything just called Net). CPAN's POD for Net::ARP gives examples with eth0, which I've only ever seen on Linux. Am I right in thinking that this module is not available for Losedows?

      Regards,

      John Davies

        I don't have Windows so can't tell for sure (you could just download and try to install the package from CPAN). There does seem to be a dearth of arp-related modules for Windows, you may be better off using arping.


        There are ten types of people: those that understand binary and those that don't.
Re: How can get all the terminal names/IP addresses connected to network
by skx (Parson) on Oct 18, 2005 at 11:14 UTC

    I did something similar to this in a previous job. My solution was to approach the result fetching from the other direction.

    Instead of :

    • Script 1 collects information on local host and stores results locally.
    • Script 2 polls all "alive" hosts and fetches their most current results.

    Modify script #1 to save results locally and write them to \\server\temp.

    Then script #2 can copy them from the local share (I'm assuming script runs upon "server") into the final destination - after preforming sanity checks to make sure you don't have a malicious local user uploading garbage to that location.

    (The reason for the temporary share is because presumably you don't want random clients to a) see, or b) potentially modify other PC's results - so you have to have a staging area where the files can be tested for sanity before they are moved into the "real" location)

    Steve
    --
      I'd go with this suggestion - except make it a permanent share that only the account running the job has access to (and perhaps a backup person or two). The nice advantage to this is you have all your raw data in one location plus it is being backed up on the network (you *are* doing backups aren't you? ;-)