I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2009-01-02 19:35:42-08 by radar103179
Expect with nested foreach loop.
Hey guys, what I'm trying to do is the following. 1.) Have a user input directories to be created, this I have completely. 2.) Based on the list of directories to create I want to then have perl/expect connect to the remote machine and create the directories. The thing I'm trying to avoid doing is starting a new ssh session per directory. What I would like to do is spawn the ssh connection to the remote machine then evaluate the variable ie, foreach $directory(@directory) do then the expect code here to create directories. I'm having a real problem getting my head wrapped around this. Any help would be great. This is what i have so far.
sub CreateAdditionalDirectories { foreach $line(@servers) { $command = "ssh $username\@$line"; my $exp1 = Expect->spawn($command) or die "Cannot spawn $command: #!\n"; @match_patterns = ("Password"); $patidx = $exp1->expect($timeout, @match_patterns); $exp1->send("$password\r"); @match_patterns = ("bash"); foreach $directory(@directory) { $patidx = $exp1->expect($timeout, @match_patterns); $exp1->send("/usr/local/bin/sudo mkdir $HOMEDIR/$createme/incoming/$directory\r"); @match_patterns = ("bash"); $patidx = $exp1->expect($timeout, @match_patterns); $exp1->send("/usr/local/bin/sudo mkdir $HOMEDIR/$createme/pub/$directory\r"); @match_patterns = ("bash"); $patidx = $exp1->expect($timeout, @match_patterns); }; }; };
But I'm not sure if they above will work or not. All of the perl is run on the main machine, and it connects to a remote machine to setup the ftp directories. Password is inputted from the command line when you invoke the script and hidden with Term:READKEY Thanks Dave
Direct Responses: Write a response