Visit Our UNIX and Linux User Community


print ip to hostname with array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print ip to hostname with array
# 1  
Old 08-29-2009
print ip to hostname with array

Hi Anyone can help me??i writing a perl using array to translate ip address to the hostname my script are below.this line print "$myhost{$ipaddr}\n"; blank Smiliehostname dint come outSmilieSmilie

#!/usr/local/bin/perl
@ipaddr=("192.1.168.2","172.25.1.13","129.1.2.5");
%myhost = { 192.1.168.2 =>"Machine 1",172.25.1.13=>"Machine 2",129.1.2.5=>"Machine 3");
foreach $ipaddr(@ipaddr){
print "$ipaddr\n";
print "$myhost{$ipaddr}\n";
}

---------- Post updated at 11:22 AM ---------- Previous update was at 11:16 AM ----------

fyi i have try
print "$myhost{192.1.168.2}";
supposely come out Machine 1 but nothing comeout...blank!!SmilieSmilieSmilie.anyone pls help me..

---------- Post updated at 11:25 AM ---------- Previous update was at 11:22 AM ----------

this line print "$ipaddr\n"; no problem can print out the ip addreSmilie
192.1.168.2
172.25.1.13
129.1.2.5
# 2  
Old 08-29-2009
PHP

Here's what can be written without having touched perl for a long once time: a good bad example
Code:
#!/usr/bin/perl

@ipaddr = ("192.1.168.2","172.25.1.13","129.1.2.5");

foreach $ipaddr(@ipaddr){
   $i = $i + 1;
   %myhost->{"$ipaddr"} = "Machine $i";

   $val = %myhost->{$ipaddr};
   print "IP $ipaddr has name $val\n";
}

I wish I could strike that, just to make sense at tyler_durden's post, otherwise I'd be glad to erase it.

Last edited by daPeach; 08-29-2009 at 06:47 PM.. Reason: the given code is an horror of errors
# 3  
Old 08-29-2009
A couple of mistakes in your script.

- Your hash initialization is incorrect, it should be a hash-list equivalence.

- Your hash keys are not the same as array elements.

Since this is Perl 101, I will not (by spoon-feeding you the solution) steal from you the joy of discovering things on your own.

tyler_durden
# 4  
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.
# 5  
Old 08-30-2009
Hi daPeach
Thanks for your kind help.

---------- Post updated at 12:40 AM ---------- Previous update was at 12:38 AM ----------

Hi durden_tyler
Thanks for your point out...Thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #29
Difficulty: Medium
Computer hard disks were first introduced in 1956.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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

Featured Tech Videos