0

I have php program which I run via CLI. The aim of program is to connect some address http and get some data. Its works OK. But I want to use other IP address of my webserver instead of primary. Is there any solution for this ?

EDIT Webserver has multiple ip address. I want to use any of them I choose at any request. If not possible at least 1 specific one. I have tried CURL_INTERFACE, and its not sending data while i give other ip than primary

HOSTNAME=server.xxxx.net
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=<my ip> 23929 22
SSH_TTY=/dev/pts/1
USER=root
LS_COLORS=<blah blah>
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PWD=/root
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HTTP_PROXY_REQUEST_FULLURI=0
SHLVL=1
HOME=/root
LOGNAME=root
CVS_RSH=ssh
SSH_CONNECTION=<my ip> 23929 <server ip> 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/usr/bin/printenv

IPTable rules

xx.xx.xx.xx is servers primary ip address

*filter
:INPUT ACCEPT [85405:31617594]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [92024:84346360]
:acctboth - [0:0]
-A INPUT -j acctboth
-A OUTPUT -j acctboth
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo -p tcp -m tcp --dport 80
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo -p tcp -m tcp --sport 80
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo -p tcp -m tcp --dport 25
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo -p tcp -m tcp --sport 25
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo -p tcp -m tcp --dport 110
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo -p tcp -m tcp --sport 110
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo -p icmp
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo -p icmp
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo -p tcp
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo -p tcp
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo -p udp
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo -p udp
-A acctboth -s xx.xxx.xx.xx/32 ! -i lo
-A acctboth -d xx.xxx.xx.xx/32 ! -i lo
-A acctboth ! -i lo
COMMIT
safarov
  • 7,793
  • 2
  • 36
  • 52
  • Depends on which web server you are using. For IIS, yes, but you're probably not running IIS for PHP. – Eric J. Mar 12 '14 at 21:48
  • Do you mean your webserver has multiple IPs, and you want to use a specific one? – Marc B Mar 12 '14 at 21:49
  • @EricJ. The question states that this is a CLI script, so there's no web server involved, IIS or otherwise. – IMSoP Mar 12 '14 at 23:25
  • Do you mean you want to select which IP address the request appears to come *from*, as seen by the remote host? – IMSoP Mar 12 '14 at 23:26
  • possible duplicate of [select outgoing ip for curl request](http://stackoverflow.com/questions/2425651/select-outgoing-ip-for-curl-request) – Marc B Mar 13 '14 at 14:46
  • @MarcB I have tried that version too. CURL_INTERFACE option only changes http header, which is not for real use. It still using primary ip for outgoing – safarov Mar 13 '14 at 20:26
  • Post the output of: netstat -rn – Brad Lanam Mar 14 '14 at 22:22
  • So you've got a php program that connects out to a remote host and you want to control what ip it connects out with from your box? Am i reading this right? If so, what does the program use to connect out? – Rhys Mar 14 '14 at 22:31
  • @Ryven thats right. I am using CURL – safarov Mar 15 '14 at 08:20
  • Environment variables suggest proxy must be transparent. firewall rules are unhelpful since you pasted the default table. Try `sudo iptables-save` which prints all rules, in a compact form. Also do a `wget -S -O /dev/null` and look at the response headers. – Pritam Baral Mar 16 '14 at 00:11

2 Answers2

1

If you can use curl for outgoing http requests, you can set an option in curl: CURLOPT_INTERFACE

curl_setopt($curlh, CURLOPT_INTERFACE, "xxx.xxx.xxx.xxx");

**CURLOPT_INTERFACE**: The name of the outgoing network interface to use. This can be an interface name, an IP address or a host name.

PHP Manual: curl_setopt

safarov
  • 7,793
  • 2
  • 36
  • 52
Pritam Baral
  • 475
  • 2
  • 7
  • Check comments first. I have tried this. Not working – safarov Mar 15 '14 at 08:18
  • In my defense, that comment was buried deep. Perhaps you're invoking it wrong, or something's wrong with your php/curl installation, since that option has nothing to do with HTTP, rather it touches IP two layers below. – Pritam Baral Mar 15 '14 at 08:39
  • i have tried it 3 different machine, on my local and other 2 server. While I am getting ip from $_SERVER['REMOTE_ADDR'] variable , its always servers primary address. WHile its only affecting $_SERVER['HTTP_X_FORWARDED_FOR'] – safarov Mar 15 '14 at 10:58
  • 1
    Then you have a proxy server running somewhere along the path, most likely on the server matching REMOTE_ADDR. Those headers are added on by a proxy. Try looking at the response headers. CURLOPT_INTERFACE itself is working just fine. **This is a good example why you should supply more details.** Edit the question with your server stack details: firewall forwarding rules, environment variables like http_proxy etc. And please take back your downvote, since for the question in its current form, my answer isn't wrong. – Pritam Baral Mar 15 '14 at 12:01
1

Depends on http server you are using , you can bind your "virtual host" configurations to specific address and port. in Apache2 for example :

<VirtualHost **127.0.0.10:8080**>
     ServerAdmin myemail@mywebsite.com

     ServerName mywebsite.com
     ServerAlias www.mywebsite.com
     DirectoryIndex index.php index.htm

     DocumentRoot /var/websites/mywebsite.com/www
     <Directory "/var/websites/mywebsite.com/www">
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          Allow from all
     </Directory>

     ErrorLog logs/mywebsite.com-error_log
</VirtualHost>

With nginx , something like :

server
{
     server_name example.com www.example.com;
     **listen 66.113.100.140:80;**
     access_log /var/log/ngnix/example.log;
     error_log /var/log/nginx/example.error.log;

location /site {
     alias /data/www/content/site/example;
}
location / {
     proxy_pass_header Server;
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Scheme $scheme;
     proxy_connect_timeout 10;
     proxy_read_timeout 10;
     proxy_pass http://10.15.20.10:8107/;
}
}

Also starting from PHP 5.4 there is build in web server that could be used for testing purposes. Generally you can bind to any local address as following :

$ cd ~/public_html
$ php -S 127.0.1.1:8000
micrub
  • 740
  • 8
  • 17
  • Thanks for information, It was Nginx that forwarding ip. Its my weak side, i dont know nginx. Thats why i was stuck – safarov Mar 16 '14 at 07:42