-1

I am not a php expert at all.

I am working on a VOIP project and have to open a port range (10000 - 20000) in my virtual box. The issue is that there is no such option to do it all in once. It has to be done one by one using a command line:

VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10001,,10001"
VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10002,,10002"
VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10003,,10003"

and so on till I reach

VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,20000,,20000"

Can anybody help me with a php script that will generate a TXT file which will contain

VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10001,,10001"
to
VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,20000,,20000"

Thanks

4

4 回答 4

1

Stackoverflow 旨在帮助您解决问题,而不是为您编写代码。

反正,

$output = '';
for ($i = 10001; $i <= 20000; ++$i) {
    $output .= 'VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,' . $i . ',,' . $i . '"' . PHP_EOL;
}

$f = fopen('commands.txt', 'w');
fwrite($f, $output);
fclose($f);
于 2013-06-05T20:11:19.003 回答
1
 <?php
  $fp = fopen("outfile.txt", "w");
  for($x = 10000; $x<=20000; $x++){
      fprintf($fp , 'VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh%d,udp,,%d,,%d"'."\n", $x - 9999, $x, $x);
  }
  fclose($fp);
于 2013-06-05T20:11:34.843 回答
0

这是一个简单的解决方案,file_put_contents()用于将您写入的文本打印到名为“this_is_my_txt_file.txt”的文件中。

file_put_contents('this_is_my_txt_file.txt', "VBoxManage modifyvm \"Elastix 4.0\" --natpf1 \"guestssh,udp,,10001,,10001\" to VBoxManage modifyvm \"Elastix 4.0\" --natpf1 \"guestssh,udp,,20000,,20000\"\n\r");

编辑 哦,我现在看到你已经改变了问题。现在,恐怕我无法理解。您希望它查看每一行并在到达“guestssh,udp,,20000,,20000”时停止并将其打印到文件中?

请详细说明您要做什么。

于 2013-06-05T20:09:37.787 回答
0
$content = "";
for($i=1;$i<=10000;$i++){
$content.="VBoxManage modifyvm "Elastix 4.0" --natpf1 \"guestssh{$i},udp,,{$i+10000},,{$i+10000}\"\n";
}
file_put_contents('file.txt', $content);
于 2013-06-05T20:14:32.713 回答