ChrisMartino Posted September 26, 2009 Share Posted September 26, 2009 Hey there, I'm totally new to php, I know a little bit (I'm talking really basic stuff), And i would like to make a script that connects to my dedicated server via SSH then creates a user on the box, Could someone please show me, Or code for me so i could look at it please?, Thanks for taking the time to read this, Chris (PHP newbie) Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/ Share on other sites More sharing options...
trq Posted September 26, 2009 Share Posted September 26, 2009 Should be examples in the ssh section of the manual, though seriously, bash is probably much better suited to the task. Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925132 Share on other sites More sharing options...
ChrisMartino Posted September 26, 2009 Author Share Posted September 26, 2009 Would this work, I just want it to echo if it connects. <php? if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); if(!($con = ssh2_connect("94.23.204.219", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "somepassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925136 Share on other sites More sharing options...
trq Posted September 26, 2009 Share Posted September 26, 2009 Why don't you try it and see? Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925138 Share on other sites More sharing options...
ChrisMartino Posted September 26, 2009 Author Share Posted September 26, 2009 It didn't work, Heres what it dose: http://dino-host.net/paneltest/ It just gives a blank screen. Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925141 Share on other sites More sharing options...
trq Posted September 26, 2009 Share Posted September 26, 2009 You really ought to set yourself up a local development server, must be a hassle to upload everytime you want to test some code. Anyway, your using <php? where you should be using <?php Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925147 Share on other sites More sharing options...
ChrisMartino Posted September 26, 2009 Author Share Posted September 26, 2009 Thanks, I keep doing that hehe, Now i seem to get a echoed message saying "function ssh2_connect doesn't exist" Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925150 Share on other sites More sharing options...
redarrow Posted September 26, 2009 Share Posted September 26, 2009 Have you correctly installed the ssh2 correctly, cheek first! http://uk2.php.net/manual/en/ssh2.installation.php Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925157 Share on other sites More sharing options...
ChrisMartino Posted September 26, 2009 Author Share Posted September 26, 2009 Its a pre-installed web-host, It has got "ssh2" thats the only ssh i think it has, Dose it require just ssh or something?. Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925161 Share on other sites More sharing options...
redarrow Posted September 26, 2009 Share Posted September 26, 2009 Here a interesting read, apparently ssh and ssh2 are one off the same, and run from the same dll file so there no,reason your code shouldn't work. Here have a look http://felipecruz.com/blog_using-ssh2-and-php.php execute way ...... <?php if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); // log in at server1.example.com on port 22 if(!($con = ssh2_connect("server1.example.com", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "secretpassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // execute a command if(!($stream = ssh2_exec($con, "ls -al" )) ){ echo "fail: unable to execute command\n"; } else{ // collect returning data from command stream_set_blocking( $stream, true ); $data = ""; while( $buf = fread($stream,4096) ){ $data .= $buf; } fclose($stream); } } } ?> shell way <?php if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); // log in at server1.example.com on port 22 if(!($con = ssh2_connect("server1.example.com", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "secretpassword")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // create a shell if(!($shell = ssh2_shell($con, 'vt102', null, 80, 40, SSH2_TERM_UNIT_CHARS))){ echo "fail: unable to establish shell\n"; } else{ stream_set_blocking( $shell, true ); // send a command fwrite($shell,"ls -al\n"); sleep(1); // & collect returning data $data = ""; while( $buf = fread($shell,4096) ){ $data .= $buf; } fclose($shell); } } } ?> Please also read this link it way interesting, the bottom of the page might help you. Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925178 Share on other sites More sharing options...
redarrow Posted September 26, 2009 Share Posted September 26, 2009 Sorry here the link.... http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925183 Share on other sites More sharing options...
ChrisMartino Posted September 27, 2009 Author Share Posted September 27, 2009 Still no luck, Any help?, Thanks. Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925714 Share on other sites More sharing options...
redarrow Posted September 27, 2009 Share Posted September 27, 2009 big boy read this bro Check the following: - Did you follow every step of the prerequisites & installation how to in this article? - On the serverside, 'PasswordAuthentication yes' must be enabled in the sshd_config. Default is yes on most servers, but in some cases you will have to turn this on yourself by making sure the following lines are in place in the file: /etc/ssh/sshd_config: # Change to yes to enable tunnelled clear text passwords PasswordAuthentication yes If you've made any changes, ssh needs a restart /etc/init.d/ssh restart Link to comment https://forums.phpfreaks.com/topic/175569-beginner-how-would-i-do-a-simple-ssh-connection/#findComment-925754 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.