Web Hosting Talk







View Full Version : PHP Script to edit SSH files?


kohkindachi
06-19-2009, 10:06 PM
Hi is there a way to edit files in ssh? There's a variable from a new module that I need to edit regularly and logging in and out through editor is troublesome :/

mwatkins
06-20-2009, 12:59 AM
You've asked if there is a way "to edit files in ssh", quoted verbatim.

Yes, you use an editor.

Then you complain that using an editor is troublesome.

?

I suppose we can just guess that what you really meant is if there is a way to edit a file WITHOUT logging in through a secure shell. Is this correct?

If so, the answer is yes. Read the file; display it in a text box; save the text box contents back to the file if the values have changed. Hopefully the file doesn't require root or escalated permissions in order to read and write.

Or, avail yourself of an editor that can edit remote files, easily. Even vim can open and save a file over ssh; if its in your recent edits list it will seem just like working on a local file. Many text editors offer this capability. Fast and simple.

dTuesday
06-23-2009, 04:24 PM
Depending on what what editors you have installed on your server it should be straight forward. Some editors that are often available are:

Pico
Nano
Joe
Vi / Vim <-- I do not love these, some ppl swear by them.

There are a number of other ones. You can check to see if the editor is installed by calling up the help manual for it, once you have SSH-ed to the server, e.g.

# man nano

If the editor is installed you will get a screen full of help text, to exit the help text press the "q" key. If it is not installed you will get a message "No manual entry for <name of application>"

Here is the basic practice for using an editor via SSH.

1. SSH into your server using PuTTY

2. navigate to the directory with the file, e.g.:
# cd /path/to/file

3. run your favorite editor and pass it the file name of the file you want to edit, e.g.:

# nano file.txt

4. Make your changes and save the file. Depending on which editor you are using there will be different methods for saving and exiting. For example in nano you use ctrl+d to save and ctrl+x to exit. Once launched there should be on screen cmd instructions for most of the editors.

Hope this helps!

ramnet
07-03-2009, 07:59 PM
Since we're going verbatim, I'll answer the thread title since the OP's content makes no sense.

To use PHP to connect to SSH you will need something like this:
http://pecl.php.net/package/ssh2

Read more here:
http://www.php.net/manual/en/book.ssh2.php

you can connect to ssh from a php script and use "ed" or something to edit your remote files.

aniketh
07-04-2009, 07:24 PM
the fastest way imho would be to setup password-less login to your server (using authorized_keys2) for the machine and write a php file to just modify and upload that one file, maybe with system($scp_string).

youropensource
07-07-2009, 05:48 AM
exec() function to edit those file if you have permission

mwatkins
07-07-2009, 11:20 AM
This is an OPDAPVQ thread.

(Original Poster Disappears After Posting Vague Question)

aniketh
07-07-2009, 12:06 PM
even if that is the case, someone later searching may find a working solution here :)

mwatkins
07-07-2009, 12:10 PM
A solution for a problem that hasn't been defined?

mwatkins
07-07-2009, 12:31 PM
This thread has been bugging me for a while, because it is emblematic of a general problem: lack of clear communication. We could post solutions till the cows come home and still miss the target.

This situation is all too common; a poster, often a new member of the community, throws up some ambiguous and poorly worded question which can be interpreted in many ways and then simply disappears. Responders left and right push out "solutions" to a problem which isn't well defined. Original poster frequently never reappears.

If we responders held question askers to a higher standard of communication, there'd be a lot more learning and a lot less guessing as to meaning.

Maybe we could start by looking at the title of this useless thread:

PHP Script to edit SSH files?

PHP script: is self-explanatory.
edit: ditto
SSH files: here's where confusion sets in.

Does the poster mean "files that ssh / sshd require for normal operation"? Probably not. The poster probably means "files on the remote host's file system" but instead of saying "files" uses "ssh" as an adjective, as too many newbies do. SSH is a protocol and a tool to access a remote host system. The files on that system are not broadly known as "ssh files".

One sentence, two possible meanings.

Hi is there a way to edit files in ssh? There's a variable from a new module that I need to edit regularly and logging in and out through editor is troublesome :/

Here the OP perpetuates the improper use of "ssh". It's a tool. When you use the tool you are given (under typical remote use) a command line prompt.

Adding to the confusion the user says logging in and out through the editor is troublesome.

Perhaps this means the user is *already* making use of an editor at their workstation that allows for remote editing of files. Many do. If this is the case, on a *nix desktop you'd simply ensure your *local* public key was added to your account (on the remote server) ~/.ssh/authorized_keys -- then when opening up files on the remote system from the local editor, via ssh, you would not be prompted for a password.

Or maybe the user really wants to know what editing tools are available on a typical *nix host. I'd suggest the OP learn vi or vim, because vi is available on virtually all *nix hosts. But that might be asking too much of someone who thinks of the command prompt as "ssh". nano or pico or "edit" might be better choices.

Getting back to the PHP script component of the OP's question, if all that the OP needs to do is change a couple of configuration values in a remote file, sure, one could create a PHP script that performs that task, perhaps offering up a text box to do the "editing" via a web page.

But I would not recommend that, because:

a) then the OP would have to learn about providing a secure web form, authentication
b) if ssh is still a somewhat foreign concept, imagine setting up ssl and https?
c) a poorly designed editing form could easily open up the system for remote exploitation by nefarious people
d) this question is so basic it suggests that "if you have to ask, you shouldn't attempt it"

Learning how to efficiently manipulate files on a remote host from the command prompt, or from a local editor, is such a key skill that the OP is best off exploring and learning those tasks rather than writing some custom piece of software that is highly unlikely to be as secure as a ssh session.

So there you have it - one vague question: multiple plausible "solutions".

aniketh
07-07-2009, 07:23 PM
That was quite an amazing analysis :)

Off-topic, but reminds me of http://thedailywtf.com/Comments/plz-email-me-teh-codez.aspx . Its a shame the original forum topic is no longer present.