Results 1 to 9 of 9
  1. #1

    How to write this PHP code with escaping the various characters.

    I want to write this code in a php file.. that executes a shell command...

    PHP Code:
            if(!($stream ssh2_exec($con"(echo "g/${123.123.123.123}/d"; echo 'wq') | ex -s /sources/sample.txt" )) ){
                echo 
    "fail: unable to execute command\n"
    i tried using the below command

    PHP Code:
            if(!($stream ssh2_exec($con"(echo \"g/${123.123.123.123}/d\"; echo 'wq') | ex -s /sources/sample.txt" )) ){
                echo 
    "fail: unable to execute command\n"
    but i get error with this saying..

    Parse error: syntax error, unexpected T_DNUMBER in /path/to/this/file.php on line 64 // line 64 is nothing but the code written above


    Thank you

  2. #2
    Join Date
    May 2009
    Posts
    766
    My guess would be that php is trying to interpret the $ in the regex as a variable. I'd escape that, or more to the point of your post title, don't mix your quotation marks in the command...use double quotes inside of the command and wrap the whole thing in single quotes. That will take care of escaping...

    PHP Code:
    if(!($stream ssh2_exec($con'(echo "g/${123.123.123.123}/d"; echo "wq") | ex -s /sources/sample.txt' )) ){
                echo 
    "fail: unable to execute command\n"

  3. #3
    Join Date
    Aug 2009
    Location
    Helsinki
    Posts
    5
    Maybe you forget to close starting tag { with } take look below. And the I think you are missing one ) too. And "( should be (" after that $con

    PHP Code:
    <?
    if(!($stream ssh2_exec($con, ("echo \"g/${123.123.123.123}/d\"; echo 'wq') | ex -s /sources/sample.txt" )))){ 
        echo 
    "fail: unable to execute command\n";  
    // closing tag missed. 
    ?>
    Last edited by godza; 08-31-2009 at 06:28 PM.

  4. #4
    Join Date
    May 2009
    Posts
    766
    Quote Originally Posted by godza View Post
    Maybe you forget to close starting tag { with } take look below. And the I think you are missing one ) too. And "( should be (" after that $con
    PHP Code:
    $ echo "g/${123.123.123.123}/d"; echo 'wq')
    bashsyntax error near unexpected token `)' 
    Doubt it. Pretty sure he meant:

    PHP Code:
    $ (echo "g/\${123.123.123.123}/d"; echo 'wq')
    g/${123.123.123.123}/d
    wq 
    Which brings me to a correction in my post...you will still need to escape the '$' for the shell, but again, '...\$...' is much nicer than "...\\\$..."

    As to the closing brace, of course, it is needed, but it's unrelated to the original question. Probably just a case of overly-cropped code

  5. #5
    Join Date
    Aug 2009
    Location
    Helsinki
    Posts
    5
    Maybe better example would be:

    PHP Code:
    <?
    $con 
    ssh2_connect('codename.fi'22);

    $blaah='echo \"g/\${123.123.123.123}/d\"; echo \"wq\") | ex -s /sources/sample.txt\"';
    if(!(
    $stream ssh2_exec($con, ($blaah)))){
        echo 
    "fail: unable to execute command\n";
    // closing tag missed.
    ?>
    Last edited by godza; 08-31-2009 at 06:45 PM.

  6. #6
    thnx for the replies

    PHP Code:
    if(!($stream ssh2_exec($con"(echo \"g/$123.123.123.123/d\"; echo 'wq') | ex -s /sources/sample.txt" )) ){
                echo 
    "fail: unable to execute command\n"
    that also did the trick

  7. #7
    Join Date
    Aug 2009
    Location
    Helsinki
    Posts
    5
    No problem.

  8. #8
    I tried / backslashing to escape but didn't work right now i'm making seperate values for each part of my date, and concacting them it would be easier for me to use the date function like: date(m/d/y:h) ...etc.... but i would need to know how to properly escape those characters

    here's my code:

    $month = date(m);
    $day = date(d);
    $year = date(Y);
    $hour = date(h);
    $minute = date(i);

    echo $day.'/'.$month.'/'.$year.':'.$hour.':'.$minute;

  9. #9
    Join Date
    May 2009
    Posts
    766
    Maybe I'm missing something here, but what's wrong with:

    PHP Code:
    echo date("d/m/Y:h:m"); 
    Last edited by mattle; 09-04-2009 at 05:24 PM. Reason: syntax error

Similar Threads

  1. PHP: Secure ways to write code?
    By CraigML in forum Programming Discussion
    Replies: 24
    Last Post: 09-30-2008, 05:08 PM
  2. Ok im write in code for a nav link BUT
    By rumrunner439 in forum Web Design and Content
    Replies: 1
    Last Post: 09-28-2008, 11:32 AM
  3. How do you write CD or DVD in Japanese/Chinese characters?
    By vibesolutions in forum Web Hosting Lounge
    Replies: 8
    Last Post: 01-31-2006, 05:51 AM
  4. Javascript escaping characters in variable
    By ilyash in forum Programming Discussion
    Replies: 3
    Last Post: 05-01-2005, 06:46 PM
  5. How can I stop php escaping characters in mail()?
    By tutv in forum Programming Discussion
    Replies: 3
    Last Post: 03-23-2003, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •