I have to write a PHP script that will be executed on my client's network when finalized. My computer (A) cannot connect to that network, but I have SSH access to a single server (B) on it.
My script has to do a cURL request (with certificate and private key) to a web server (C) on a specific port on that network. Another difficulty is that I do not have the IP of the C server, only a URL resolvable only when within the network. But server B and C can communicate between each other
Basically I see 3 steps (but there may be more) :
Open SSH connection from computer A to server B
Send cURL request to server C (https://my.remote.server.domain.com:8444) and store response
Close SSH connection
The thing is, I have no idea how to do that (I'm basically ignorant in all things network related). Anyone has a clue ?
Using Bash:
$ ssh user#ssh_server << EOM
curl http://remote.server/ > /home/user/file
EOM
$ scp user#ssh_server:/home/user/file local_file
This first part connects to your ssh server (ssh_server), executes cURL and saves the file locally (on the ssh server). Then, scp is used to download the file on your local machine.
Creating a temporary file is probably the easiest way of doing this. You could create it in /tmp (and, if you really can't stand having that file there, delete it afterwards using ssh + rm: )
$ ssh user#ssh_server 'rm /tmp/file'
Finally, a dirty (and not recommended) way for not creating files is the following:
$ ssh user#ssh_server << EOM
curl http://remote/server | nc -l 1234 &
exit
$ nc ssh_server 1234 > file
I should probably mention once again that this technique should be avoided at all costs, since it transfers unencrypted data and requires no authentication whatsoever. Also, keep in mind that someone else could connect to the server using that same port (1234) before your command executes, thus retrieving the result for themselves, and leaving your script hanging.
So, one last time, don't use that.
Related
One may start a local PHP server, e.g for testing:
php -S localhost:8080
One can also execute a PHP statement, e.g.
php -r "echo 'Hello';"
We initially hoped we could use this to tell when the server was started, i.e. using systemd-notify or some other process readiness protocol. However, using -r and -S together seems to ignore -r.
My question is thus, when starting a local server using php -S, is it possible to execute some code after the server is ready to receive incoming connections? This would allow us to execute something like systemd-notify --ready and enable the parent process to know when to proceed with testing.
I need to connect to a particular API but that API only accept request from my mate server. Then include thoses informations back in our website.
So basically I need to connect to the server make the request receiving the answer transfering it to my host so that I can play around with them and integrating them to my website through a php file.
I've already exchange ssh keys and I can connect to my server easily, I know I probably need to use ssh -L (not -R or -D) for the tunneling, though I don't know what to do with my php files to make that request etc etc or what are the other steps once I've entered that command.
If anyone can help that would be lovely :) !
You can create an SSH tunnel using the -L command line switch:
$ ssh -L [port on local]:[apiserver hostname]:[port on apiserver]
[user]#[your friend's server hostname]
E.g.
$ ssh -L 8080:apiserver.com:80 bob#friend.server.com
After the above command successfully connects to friend.server.com, any requests sent to your localhost:8080 will be tunneled through friend.server.com host and arrive at apiserver.com:80. From apiserver's perspective, the request's origin is friend's server.
(This will actually open an SSH session in the terminal window where executed, i.e. you get the prompt of the remote server, which is not required and you can ignore that prompt. It is possible ro run this in the background w/o console login with other switches)
I am working on a webapp made by someone else which uses Bottle routing. I want to create a simple login page which requires some PHP. If I return the PHP page as a static_file, any HTML will be executed but PHP won't, for obvious reasons. How should I serve the PHP file so that it is dynamic?
Not working:
#route('/login')
def serve():
return static_file('login.php', root='.')
In order to server PHP files, you need to have PHP installed on the web server. Additionally, the webserver needs to be configured to detect PHP files and execute them.
Serving PHP files from Python is kinda useless and not recommended.
I'd recommend you to take the time to translate this script from PHP to Python.
I wanted to do the same thing yesterday, but the answers I got to my question made it clear it was either impossible or extremely difficult. I came up with writing a small python program to run the PHP built in server. NOTE: PHP needs to be able to run from the command line for this to work.
#Import the os package so that this code can run commands
import os
#Get the port that the user wants to host on
port = str(input("What port would you like to host on?"))
#Add wanted port to the command that hosts the php server
cmd = "php -S localhost:" + port
#Actually run the command to host php server
os.system(cmd)
#Now the PHP server will take over until you
#use ctrl + C to quit hosting
Just remember that the port needs to be 4 numbers. When you host this, you can return any file from the folder you ran this code in by simply typing it in the browser. Example:
localhost:8080/login.php
Returns login.php (if it is there) on the localhost port that you asked for.
So I want to make a daemon.php file which can open a port on a give ip address and request data from client.php and with the browser to listen/read to earlier pipe (Would that be a problem for the security or not?).
I fount this funphp tutorial which seems to be good but is more like a cronjob, I found also function stream-socket-server but can handle only one request and then shuts down, and I really don't know where exactly to put listener.
You can use the PHP Built-In Server
Add PHP to your PATH environment variable
Start your server on a given port, ex: php -S localhost:8000 OR
Start with a router script, ex: php -S localhost:8000 rt.php
Now, just open your browser and type; localhost:8000
Here's my goal :
I have a Windows XP PC with all the source code in it and a development database.
Let's call it "pc.dev.XP".
I have a destination computer that runs Linux.
Let's call it "pc.demo.Linux".
Here's what I've done on "pc.dev.XP" (just so you get the context) :
installed all cygwin stuff
created a valid rsa key and put it on the dest
backup computer so that ssh doesn't
ask for a password
rsync works pretty well this way
If i try to do this on "pc.dev.XP" via a command line :
cd \cygwin\bin
ssh Fred#pc.demo.Linux "cd /var/www && ls -al"
this works perfectly without asking a password
Now here's what I want to do on the "pc.dev.XP":
launch a php script that extract the dev. database into a sql file
zip this file
transfer it via ftp to the "pc.demo.Linux"
log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"
if I run on "pc.dev.XP" manually :
putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql
this works perfectly.
Same for :
cd \cygwin\bin
ssh Fred#dest "cd /var/www && ls -al"
If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.
Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")
Any idea what I could do / check / correct ?
I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:
php extractFromDb.php | ssh user#remote.com "mysql remoteDatabaseName"
This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.
If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:
php extractFromDb.php | gzip -9 | ssh user#remote.com "gunzip | mysql remoteDatabaseName"
You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?
If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.
"if I run on the development PC manually this works perfectly.".
Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.
Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.
Here's what I did :
a batch file that :
Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
Calls rsync to synchronize the source folder
Calls putty -l user -pw password -m file_with_ssh_commands_to_execute
It works like a charm.