Sponsored Content
Top Forums Shell Programming and Scripting print ip to hostname with array Post 302348790 by daPeach on Saturday 29th of August 2009 05:49:53 PM
Old 08-29-2009
After a few readings:
Code:
#!/usr/bin/perl -w

 use strict;
my @ipaddr = ("192.1.168.2","172.25.1.13","129.1.2.5");
my $i = undef;
my %hosts = ();
my $val = undef;

foreach my $ip (@ipaddr){
   $i++;
   $hosts{$ip} = ("Machine $i" ) ;
   print "IP: $ip has name: $hosts{$ip}\n";
}

doesn't print errors any more

I hope this is going to be more helpfull.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies

2. Shell Programming and Scripting

Print the first four characters of hostname

Hey, I'm trying to print the first four characters of the hostname of a computer. I can get it from using: hostname -s | sed 's/...........$//'" but this is when I know how many characters are in the computer name. I dont understand why some like: hostname -s | sed '/..../p' wont... (7 Replies)
Discussion started by: yxian
7 Replies

3. Shell Programming and Scripting

Print Array function

The code below prints outs each array element, but it always says the array length is 1 even though its not. What am i doing wrong? function printArray(){ #here should check if it is an array and that an arg exits echo "Priting array" myarr="${@}" echo... (2 Replies)
Discussion started by: chrisjones
2 Replies

4. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

5. Shell Programming and Scripting

Print @array content to a file

Hi, as the title, I have an array @f_lines with gene information in it. How can I put the content of @f_lines into a file so that I can read it? I tried this: open(OUTPUT, "file"); # put gene information in this file; @f_lines = ("gene1", "gene2", "gene3"...); # gene information; print... (3 Replies)
Discussion started by: lyni2ULF
3 Replies

6. UNIX for Dummies Questions & Answers

How To Print Array in awk?

Hello, May i please know how do i print the array using awk script. I am using below shell script to start with but not working. #!/bin/bash LOADSTATUS="Line 0" LOADSTATUS="Line 1" LOADSTATUS="Line 2" LOADSTATUS="Line 3" LOADSTATUS="Line 4" awk ' BEGIN { Your File Load Status }... (1 Reply)
Discussion started by: Ariean
1 Replies

7. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

8. Shell Programming and Scripting

Not getting array in .awk file and print it

I have test.sh file as below : set -A IDARR $ID echo | awk -f test.awk -v TempArr="${IDARR }" I have test.awk file as below : BEGIN { Flag = 1; } { print "Hello"; for(i in TempArr) { print i; } } (9 Replies)
Discussion started by: nes
9 Replies

9. Shell Programming and Scripting

How to print last character of hostname and assign to a variable ?

Hi How to pass echo output to a variable ? Does below awk command will get the last character of hostname and assign to a variable - "svr" ? svr=$( echo `hostname` | awk '{print substr($0,length,1)}' ) Thanks. Please use CODE tags when dsplaying code segments, sample input, and sample... (7 Replies)
Discussion started by: Lim
7 Replies

10. Shell Programming and Scripting

awk remote multiple hosts print remote hostname and output

Hi all, i'm trying to gether multiple pattern on remote hosts, and trying to print hostname and the pattern, ssh remoteserver1 -C 'hostname 2>&1;cat /var/log/server1.log | awk -F ";" '"'"'{ print " "$2" "$5}'"'"'| sort | uniq -c | sort -g -r ' The output is the following, remoteserver1 ... (8 Replies)
Discussion started by: charli1
8 Replies
Net::NBName(3pm)					User Contributed Perl Documentation					  Net::NBName(3pm)

NAME
Net::NBName - NetBIOS Name Service Requests SYNOPSIS
use Net::NBName; my $nb = Net::NBName->new; # a unicast node status request my $ns = $nb->node_status("10.0.0.1"); if ($ns) { print $ns->as_string; } # a unicast name query request my $nq = $nb->name_query("10.0.1.80", "SPARK", 0x00); if ($nq) { print $nq->as_string; } # a broadcast name query request my $nq = $nb->name_query(undef, "SPARK", 0x00); if ($nq) { print $nq->as_string; } DESCRIPTION
Net::NBName is a class that allows you to perform simple NetBIOS Name Service Requests in your Perl code. It performs these NetBIOS opera- tions over TCP/IP using Perl's built-in socket support. I've currently implemented two NBNS requests: the node status request and the name query request. NetBIOS Node Status Request This allows you to determine the registered NetBIOS names for a specified remote host. The decoded response is returned as a "Net::NBName::NodeStatus" object. querying 192.168.0.10 for node status... SPARK <20> UNIQUE M-node Registered Active SPARK <00> UNIQUE M-node Registered Active PLAYGROUND <00> GROUP M-node Registered Active PLAYGROUND <1C> GROUP M-node Registered Active PLAYGROUND <1B> UNIQUE M-node Registered Active PLAYGROUND <1E> GROUP M-node Registered Active SPARK <03> UNIQUE M-node Registered Active PLAYGROUND <1D> UNIQUE M-node Registered Active ..__MSBROWSE__.<01> GROUP M-node Registered Active MAC Address = 00-1C-2B-3A-49-58 NetBIOS Name Query Request This allows you to resolve a name to an IP address using NetBIOS Name Resolution. These requests can either be unicast (e.g. if you are querying an NBNS server) or broadcast on the local subnet. In either case, the decoded response is returned as an "Net::NBName::NameQuery" object. querying 192.168.0.10 for playground<00>... 255.255.255.255 GROUP B-node ttl = 0 (default is 300000) RA set, this was an NBNS server broadcasting for playground<1C>... 192.168.0.10 GROUP B-node ttl = 0 (default is 300000) RA set, this was an NBNS server broadcasting for spark<20>... 192.168.0.10 UNIQUE H-node ttl = 0 (default is 300000) RA set, this was an NBNS server CONSTRUCTOR
$nb = Net::NBName->new Creates a new "Net::NBName" object. This can be used to perform NetBIOS Name Service requests. METHODS
$ns = $nb->node_status( $host [, $timeout] ) This will query the host for its node status. The response will be returned as a "Net::NBName::NodeStatus" object. If no response is received from the host, the method will return undef. You can also optionally specify the timeout in seconds for the node status request. The timeout defaults to .25 seconds. $nq = $nb->name_query( $host, $name, $suffix [, $flags [, $timeout] ] ) This will query the host for the specified name. The response will be returned as a "Net::NBName::NameQuery" object. If $host is undef, then a broadcast name query will be performed; otherwise, a unicast name query will be performed. Broadcast name queries can sometimes receive multiple responses. Only the first positive response will be decoded and returned as a "Net::NBName::NameQuery" object. If no response is received or a negative name query response is received, the method will return undef. You can override the flags in the NetBIOS name request, if you *really* want to. See the notes on Hacking Name Query Flags. You can also optionally specify the timeout in seconds for the name query request. It defaults to .25 seconds for unicast name queries and 1 second for broadcast name queries. EXAMPLES
Querying NetBIOS Names You can use this example to query for a NetBIOS name. If you specify a host, it will perform a unicast query; if you don't specify a host, it will perform a broadcast query. I've used the shorthand of specifying the name as <name>#<suffix> where the suffix should be in hex. "namequery.pl spark#0" "namequery.pl spark#20 192.168.0.10" use strict; use Net::NBName; my $nb = Net::NBName->new; my $param = shift; my $host = shift; if ($param =~ /^([w-]+)#(w{1,2})$/) { my $name = $1; my $suffix = hex $2; my $nq; if (defined($host) && $host =~ /d+.d+.d+.d+/) { printf "querying %s for %s<%02X>... ", $host, $name, $suffix; $nq = $nb->name_query($host, $name, $suffix); } else { printf "broadcasting for %s<%02X>... ", $name, $suffix; $nq = $nb->name_query(undef, $name, $suffix); } if ($nq) { print $nq->as_string; } } else { die "expected: <name>#<suffix> [<host>] "; } Querying Remote Name Table This example emulates the windows nbtstat -A command. By specifying the ip address of the remote host, you can check its NetBIOS Name Ta- ble. "nodestat.pl 192.168.0.10" use Net::NBName; my $nb = Net::NBName->new; my $host = shift; if (defined($host) && $host =~ /d+.d+.d+.d+/) { my $ns = $nb->node_status($host); if ($ns) { print $ns->as_string; } else { print "no response "; } } else { die "expected: <host> "; } Scanning for NetBIOS hosts This example can be used to scan for NetBIOS hosts on a subnet. It uses Net::Netmask to parse the subnet parameter and enumerate the hosts in that subnet. "nodescan.pl 192.168.0.0/24" use Net::NBName; use Net::Netmask; $mask = shift or die "expected: <subnet> "; $nb = Net::NBName->new; $subnet = Net::Netmask->new2($mask); for $ip ($subnet->enumerate) { print "$ip "; $ns = $nb->node_status($ip); if ($ns) { for my $rr ($ns->names) { if ($rr->suffix == 0 && $rr->G eq "GROUP") { $domain = $rr->name; } if ($rr->suffix == 3 && $rr->G eq "UNIQUE") { $user = $rr->name; } if ($rr->suffix == 0 && $rr->G eq "UNIQUE") { $machine = $rr->name unless $rr->name =~ /^IS~/; } } $mac_address = $ns->mac_address; print "$mac_address $domain\$machine $user"; } print " "; } NOTES
Microsoft's WINS Server Implementation When performing name queries, you should note that when Microsoft implemented their NBNS Name Server (Microsoft WINS Server) they mapped group names to the single IP address 255.255.255.255 (the limited broadcast address). In order to support real group names, Microsoft modi- fied WINS to provide support for special groups. These groups appear differently in WINS. For example, the Domain Controllers(0x1C) group appears as "Domain Name" instead of "Group". The complete set of WINS mapping types is: Unique Group Domain Name Internet group Multihomed Unique and Group map to a single IP address. Domain Name, Internet group, and Multihomed are special groups that can include up to 25 IP addresses. Hacking Name Query Flags NetBIOS Name Service Requests have a number of flags associated with them. These are set to sensible defaults by the code when sending node status and name query requests. However, it is possible to override these settings by calling the name_query method of a "Net::NBName" object with a fourth parameter: $nb->name_query( $host, $name, $suffix, $flags ); For a unicast name query, the flags default to 0x0100 which sets the RD (recursion desired) flag. For a broadcast name query, the flags default to 0x0010 which sets the B (broadcast) flag. Experimentation gave the following results: o If B is set, the remote name table will be used. There will be no response if the queried name is not present. o If B is not set and the host is an NBNS server, the NBNS server will be used before the remote name table and you will get a negative response if the name is not present; if the host is not an NBNS server, you will get no response if the name is not present. COPYRIGHT
Copyright (c) 2002, 2003, 2004 James Macfarlane. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.8.8 2006-06-25 Net::NBName(3pm)
All times are GMT -4. The time now is 08:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy