i'm no shell god but this seems suspect:
your sourcing from and redirecting to the same file.

> cat $net_file | sed "s/address 192.168.1.*/address 192.168.1.$1/" > $net_file
> cat $net_file | sed "s/gateway 192.168.1.*/gateway 192.168.1.$gw/" > $net_file

try redirecting to a tmpfile instead and then moving the tmpfile over $net_file

$tmp_file=/tmp/interfaces.$$

cat $net_file | ... > $tmp_file
...

mv $tmp_file $net_file

hope that does the trick.

db

Jimmy Pan wrote:

the following is the content of the shell script, /bin/setip written by me.


#!/bin/bash

PATH="$PATH:/bin:/sbin";
export PATH;
let gw=255-$1

net_file=/etc/network/interfaces
host_file=/etc/hostname

# change ip address and gateway of the UML
cat $net_file | sed "s/address 192.168.1.*/address 192.168.1.$1/" > $net_file
cat $net_file | sed "s/gateway 192.168.1.*/gateway 192.168.1.$gw/" > $net_file

# change hostname of the UML
echo uml_woody_$1 > $host_file

#reboot the UML
reboot

exit 0;

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

and I'm using the following Perl script to remotely execute the shell script i

mentioned above. Here's the content of the perl script, config_net.pl:

#!/usr/bin/perl

$| = 1;

use Net::SSH::Perl;
use Net::SCP::Expect;

die "Usage: $0 LAST_OCTECT\n" if (!$ARGV[0]);

$oldip = '192.168.1.127';
print "Running /bin/setip $ARGV[0]\n";

while ($error = Rpc($oldip, "/bin/setip $ARGV[0]")) {
print "UML not up yet. $error\n";
sleep 2;
}

###############################################################################


sub Rpc { my ($host,$cmd) = @_; my $ssh; print "Starting SSH\n"; eval { $ssh = Net::SSH::Perl->new($host, ['debug' => 'yes',]); }; if ($@) { return "Failed to connect\n$@"; } print "Logging into $host\n"; $ssh->login('root', 'root');

my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print "Output: $stdout\n";
print "Error: $stderr\n";
return $exit;
}

moreover, they way i use config_net.pl to remotely execute /bin/setip is like:

$config_net.pl uml_number

where uml_number is a number like 11


i found that SOMETIMES after i execute the script remotely, the file /etc/network/interfaces will be totally cleared, which is not my purpose. But, SOMETIMES it works very well.

I wonder where's the problem? is it on the schell script, or the perl script??






-- db


Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to