I am trying to use a perl one liner as
my $stat = $ssh->exec("perl -e /.firstboot | echo $?");
and what i want is to have $stat = 0 , if the file does not exist, and 1 f it does but i am getting an error: Search pattern not terminated at -e line 1. Need some help please Thanks, -Kamal. here is the whole script:
#!/usr/bin/perl use Net::SSH::Expect; use YAML; use Data::Dumper; use warnings; use strict; use diagnostics; ##################### # process the input ##################### my $in = YAML::LoadFile('test_input.yml'); my $version = $in->{test_instance}{version}; my $build = $in->{test_instance}{build}; my $host_data = $in->{host}[0]; my $modelnum = $host_data->{model_number}; my $company = $host_data->{company}; my $address = $host_data->{address}; #print Dumper($address), "\n"; my $ssh = Net::SSH::Expect->new( ssh_option => "-v -v -v", #ssh_option => "-v", #host => $address, host => "baseline-dc1", #host => "upgrade-dc1", #host => "upgrade-3d1", #host => "baseline-3d1", #host => "batterup-dc2", user => 'admin', #password => 'password', raw_pty => 1, #timeout => 2, log_stdout => 1, ssh_option => '-o StrictHostKeyChecking=no ' . '-o UserKnownHostsFile=/dev/null ' . '-o LogLevel=ERROR' ); $ssh->run_ssh(); $ssh->exec("stty raw -echo"); #my $ls = $ssh->exec("ls -l /"); # print($ls); # my $who = $ssh->exec("who"); # print ($who); $ssh->send("sudo -i"); $ssh->waitfor('Password:', 3); $ssh->send("Sourcefire"); print "logged in?\n"; my $catschedular = $ssh->exec("cat /sys/block/sda/queue/scheduler" +); my $echoconf = $ssh->exec("echo '/usr/lib/bigloo/3.1a' >> /etc/ld. +so.conf"); my $catconf = $ssh->exec("cat /etc/ld.so.conf"); my $ldconfig = $ssh->exec("ldconfig"); print "the part we care about:\n\n\n"; my $stat = $ssh->exec("if [ -e /.firstboot ]; then echo 1; else echo 0 +; fi"); #my $stat = $ssh->exec("[ -f /.firstboot ] && echo "1" || echo "0""); open OUT, "> /tmp/STAT.out"; print OUT $stat; close OUT; print "stat:|".$stat."|"; chomp $stat; my $newstat; $newstat = substr($stat, 0, 1); $stat = $newstat; print "new stat: |" . $newstat . "|\n"; print "old stat: |" . $stat . "|\n"; # testport returns 0 if the port accepts tcp connections my $https=system("testport $address 443"); print "before..."."|".$https."|"; chomp $https; print "after..."."|".$https."|";
The problem, is that /tmp/STAT.out contains the "prompt" in addition to "0" and i just want "0" which is the value of $stat

In reply to One liner to test existance of a file by kamal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.