473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

how to expand a perl variable within a unix command

4
Hi, i have just started using Perl and I'm trying to expand a perl variable within a Unix command in the perl script, for example: -
Expand|Select|Wrap|Line Numbers
  1. foreach $host (@lines)
  2.  
  3. {
  4.  
  5.         print "$host\n";
  6.         $sshcmd = "ssh";
  7.         $get = `$sshcmd $host "uptime"`;
  8.         print "$get\n";
  9.  
  10. }
  11.  
I want to use the contents of $host for the hostname but when i run the script it just hangs. if anyone could let me know how to expand the variable within the Unix command that would be great.

Thanks
Feb 18 '10 #1
6 2494
RonB
589 Expert Mod 512MB
What makes you think it's not expanding/interpolating the variable?

It's most likely waiting for user input for the username/password.
Feb 18 '10 #2
mnew
4
Hi RonB

If i explicitly specify a hostname instead of $host it works, but nothing happens when I replace the real hostname with $host variable. Also, no username and password is required as I am using ssh keys to connect to all of the servers in my array.
Feb 18 '10 #3
RonB
589 Expert Mod 512MB
$host does not hold the value you think it holds.

Add this just before the foreach loop and post its output.
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. print Dumper \@lines;
Also, always post your code within the code tags, as I did. The code tag button is not available in the "Quick Reply" window. You need to click on the "Go Advanced" button to get it and the other features.
Feb 18 '10 #4
mnew
4
Sure, below are my hostnames: -

Expand|Select|Wrap|Line Numbers
  1. bash-3.00# ./check.pl
  2. $VAR1 = [
  3.           'lonprodd1
  4. ',
  5.           'cradevd1
  6. ',
  7.           'lonprodgz1
  8. '
  9.         ];
  10.  
Feb 18 '10 #5
RonB
589 Expert Mod 512MB
That shows that your hostnames need to have the "\n" stripped off.

Expand|Select|Wrap|Line Numbers
  1. foreach my $host (@lines)
  2. {
  3.     chomp $host;
  4.     print "$host\n";
  5.     $sshcmd = "ssh";
  6.     $get = `$sshcmd $host uptime`;
  7.     print "$get\n";
  8. }
  9.  
Feb 18 '10 #6
mnew
4
Oh man, can't believe I missed that. Thanks for your help Ron, it working fine now.
Feb 18 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hans Deragon | last post by:
Greetings. I am performing: commands.getstatusoutput("rsync <params>"); rsync takes a long time and prints out a steady stream of lines showing which file it is currently working on.
6
by: Joseph | last post by:
Hi, I am trying to develop a C# application that will run on Windows that will do the following * Select file name to process * FTP the file to a UNIX server * Process this file on UNIX using a...
5
by: Jonay Aloat | last post by:
I know that I can use the news group to post questions. But last time, one of my friend show me that you can do the same thing using a unix command. I don't remember the command, but I know it is...
1
by: amaltasb | last post by:
I have few modules which are in perl, and all other scripts are in python. Is it possible to execute the perl scripts within python or import them like: Thanks
0
by: truthbajaj | last post by:
Hi, I am using Oracle 9i and Unix on my system and trying to execute a UNIX shell command through external procedure in C. I created a shared lib (libextproc.so) for the following function. ...
3
by: Vik Rubenfeld | last post by:
I'm developing a web site in PHP for an Apache server. I have a particular stand-alone application that takes a text file as input, and produces a text-file as output. It's usually run from the...
0
by: teckguan | last post by:
Hi everyone, I would like to ask how am I going to execute multiple scripts using UNIX command? I have found a command to execute. However, I cannot execute it. The command is db2 -txf <<EOF...
3
by: sasimca007 | last post by:
Hello friends, IN modperl we write perl with html,javascript and etc. when we are writing perl code in the middle of javascript and if we want to assign a javascript variable...
1
by: KiSSFRo | last post by:
Hi all, I was wondering if someone could tell me which is the best command to use to run a unix command and to hide the error output from the shell. I'd also like to be able to tell if the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.