How to terminate old session in iTerm2 - php

Maybe a stupid question with an obvious answer, but I don't know the solution. I'm new with the command line and especially Apple and iTerm2.
In iTerm2 I open a new session for my PHP project with php -S localhost:port
Now I accidentally closed this tab once and now I don't know how to terminate that session.
When I want to open another project on that port (8000) it says, ofcourse, Failed to listen on 127.0.0.1:8000 (reason: Address already in use)
Can someone help me on how to terminate this session, so I can start another project on this port instead of using 8001, 8002, 8003 etc.
Thanks in advance

I fixed it myself, but since I can't be the only one wondering this, here is my solution:
Enter this into the terminal:
lsof -i TCP:8000
It will yield a result that might look something like this:
renaebair#siren ~/workspace/intridea/newsite (master)
→ lsof -i TCP:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 68780 renaebair 6u IPv4 0x10898278 0t0 TCP *:hbci (LISTEN)
Grab the process number (a.k.a. PID) (in this case it was 68780) and then type “kill #{that_pid}”:
kill 68780
Then try restarting your server and all should be well!

I think that this might work, at least in my unix machine it does.
sudo kill $(fuser -n tcp 8000 2> /dev/null)

Related

Runnig scilab with BackDoor from PHP

I want to run scilab with BackDoor module from PHP script. (https://atoms.scilab.org/toolboxes/BackDoor/0.2)
When I run command in terminal
sudo scilab-adv-cli
result is something like
opening BackDoor
BackDoor: listening to commands on TCP port 27020
and scilab is still runnig, and I can connect it from octave.
But I want to run this from API. When I run in PHP script
$result = shell_exec("sudo scilab-adv-cli 2>&1");
$result is
Start
... (loading things)
Opening back door
BackDoor: listening to connections on TCP port 27020
Killed
2>&1 in the command I need for showing last line from result. (http://php.net/manual/en/function.shell-exec.php#106250)
I dont know why is this happening. When the module BackDoor wasn't installed, result was "\n\n", not "Killed". When I run never ending while, the process will showing in the list of command "top" in terminal, until I kill it. With BackDoor module process will showing up only few seconds and then ends.
I tried:
chown www-data:www-data -R /(path to scilab folder also with backdoor files)
chmod 777 -R /(path to scilab folder also with backdoor files)
I also tried run never ending while
$result = shell_exec("sudo scilab-adv-cli -e "i = 1; while i < 10 disp("i"); end; " 2>&1");
but few seconds after scilab start, is killed. In the last case was showed many many times "1.\n\n" and then "BackDoor: listening to connections on TCP port 27020. Killed".
I'm the developer of this module. The problem is probably that the Backdoor opens a TCP port for receiving connections. Check your PHP module to see if it has permission to open such a port.

How to allow www-data to execute ping?

I try to execute a ping command with the user www-data
$command = 'ping -c 4 www.stackoverflow.com 2>&1';
$result = shell_exec($command);
But i always get ping: icmp open socket: Operation not permitted.
So i tried to allow the command by executing visudo and adding this line:
www-data ALL = NOPASSWD: /bin/ping
Then i restarted apache2 and tried it again, but i still get Operation not permitted.
How can i solve this?
The use of setuid, that provoques that ping is executed with the user of the ping itself (root) and not the user who launches the command (here www-data), is an old way to solve this problem, and not the best one today.
See this post. Recent linux distribution use kernel capabilities to solve that. Run getcap /bin/ping, it should return: /bin/ping = cap_net_raw+ep.
If not, you can manually set the capabilities. Run, as root:
# setcap cap_net_raw+ep /bin/ping
Or, more elegant, re-installe the appropriate package. In Debian distributions and derivatives, this is iputils-ping.

Artisan Error: Failed to listen on localhost:8000

I'm having a problem starting my Laravel installation. Whenever I type in the terminal php artisan serve, I get this error:
Failed to listen on localhost:8000 (reason:une tentative d'access un
α socket de maniere interdite par ses autorisation d'access a 0t0
tent0e)
What's the problem, and how can it be fixed?
img problem http://i.imgur.com/rOt3Lat.png
it's working now I just changed the listen port from 8000 to 8888 or any other port your services didn't use it
php artisan serve --port="8888"
Fixing Error: Failed to listen on localhost:8000 (reason: Address already in use)
List processes with php in it
ps -ef | grep php
Example output
501 **9347** 393 0 1:29PM ttys000 0:00.21 php artisan serve
501 9351 **9347** 0 1:29PM ttys000 0:02.01 /usr/local/php5-5.6.14-20151002-085853/bin/php -S localhost:8000 .../laravel/server.php
501 9781 393 0 1:56PM ttys000 0:00.00 grep php
Then kill the process
kill -9 9347
Are there any other services running on port 8000?
You can use this command on Windows:
netstat -aon | more
or on Linux / OSX
sudo netstat -plnt
to see what services are running. Then disable the service that is running on port 8000 or use another port.
List process using ps -ef | grep php
Then below only works for me
kill -9 9347
which is a force kill of the process
501 9347 393 0 1:29PM ttys000 0:00.21 php artisan serve
Option 2:
If above not works, Change the default laravel serve port number if you can, like
php artisan serve --port=8088
8000 is the default port. Use instead of :
php artisan serve --port=8005
It is because something already running on that port and you can change the port by command itself, by running following command
php artisan serve --port 8001
Use killall -9 php and if you want to close other services use killall -9 node or killall -9 mysql
When php artisan serve command given, below mentioned problem occured.
macridmi1109#Ridmis-MacBook-Pro kcnk % php artisan serve
Laravel development server started on http://localhost:8000/
[Thu Aug 6 11:31:10 2020] Failed to listen on localhost:8000 (reason: Address already in use)
Then try this line of code,
macridmi1109#Ridmis-MacBook-Pro project_laravel % ps -ef | grep php
Result will be,
501 66167 1 0 11:24am ttys002 0:00.77 /usr/bin/php -S localhost:8000
/Users/macridmi1109/Documents/Laravel/project_laravel/server.php
501 66268 64261 0 11:31am ttys002 0:00.00 grep php
Finally run the below code and, then again php artisan serve
macridmi1109#Ridmis-MacBook-Pro project_laravel % kill 66167
SOLUTION EXPLAINED BELOW
I use the command, ps -ef | grep php. After that, you will be able to find Process ID. After recognising the correct Process ID, use this command kill 66167 (kill "Process ID"). Then try php artisan serve. This worked for me.
Happy Coding😊
For me, it was silly mistake. I have installed php in new machine but php.ini was missing. So, I have created php.ini file from php.ini-production file and then php artisan serve command worked fine as expected.
for me php -S localhost:8080 from the terminal in vs code will sometimes crash, but still be running in the background.
-9 to force kill did it for me
thanks #hemss
I did
php -S localhost:8080
[Wed Dec 12 13:48:03 2018] Failed to listen on localhost:8080(reason: Address already in use)
then I..
sudo netstat -plnt
find the process running on port 8080
tcp 2 0 127.0.0.1:8080 0.0.0.0:* LISTEN 10312/php
then force kill it..
kill -9 10312
I get
[1] + 10312 killed php -S localhost:8080
then restart...
php -S localhost:8080
best way if you 8000 port is busy or you have more one project running is run your project in new port such as 8088 or another free port.
php artisan serve --port=8088
The solution I found a problem we face several times in Ubuntu.
*Failed to listen on 127.0.0.1:8000 (reason: Address already in use)*
What we do, we change the port, right?
This problem can be solved also in few seconds following below steps.
1. Open Terminal
2. **sudo su**
3. **netstat -plnt**
_find the process running on port 8080_
4. **kill -9 PROCESSNUMBER**
For more details, see my blog, click here
If you have all your configurations ok in the .env file then you should:
Use the answer from mayorsanmayor in this post to kill all php processes.
Then php artisan config:clear
Finally, php artisan serve
Good luck!

Problem with DKIM socket

I have a problem with dkim-milter.
My maillog file is filled with logs
Dec 5 23:59:59 NS1 dkim-filter[31424]: Sendmail DKIM Filter: Unable to bind to port inet:20209#localhost: Address already in use
Dec 5 23:59:59 NS1 dkim-filter[31424]: Sendmail DKIM Filter: Unable to create listening socket on conn inet:20209#localhost
Dec 5 23:59:59 NS1 dkim-filter[31424]: smfi_opensocket() failed
I suspect that the problem is related to Dkim-filter.conf and line:
Socket inet:20209#localhost
It looks like something is allready runing on that port so I can't bind it again.
How can I change this and make DKIM running without errors?
As a note, I found that when I manually stopped and restarted the dkim-milter service, my logs filled up with that message. This was due to the config option "AutoRestart" which defaults to Yes"
With that option on, when the service is stopped using "service dkim-milter stop", it automatically restarts, and when my shell script then ran "service dkim-milter start" a few seconds later, it was duplicating the process and was unable to connect to the port as it was already running.
I am not really experienced enough to know the best was to fix it, but the problem above was tested and repeatable.
In my case, I was running the wrong command. Instead of running
sudo opendkim -x /etc/opendkim.conf -p inet:8891#localhost
I was running:
sudo opendkim -x /etc/opendkim.conf -p init:8891#localhost
Not sure how common this is, but it sure took me a while to track down.
port 20209 is already in use, maybe an old zombie dkim-milter?
netstat -nlp | grep 20209 will help you find out whats blocking the port
I had the same problem so with the help of a friend I followed the steps below:
Show 25 more recent errors from logs (/path/to/file): tail -n 25 /var/log/maillog
Listen to specific port (20209) to see what's blocking it: netstat -nlp | grep 20209
Check status of a service (dkim-milter in this case): service dkim-milter status
Show all proccesses with "filter" in name, because dkim-milter is
running as dkim-filter: ps ax | grep filter
Kill process with pid f.e. 32731: kill 32731
Now the process which produced the errors isn't running.
This is how to restart to dkim-milter and postfix:
service dkim-milter stop
service dkim-milter start
service postfix restart
And everything seemed to work ok now

How can I use PHP to setup an interactive SSH session?

I'm trying to establish an interactive SSH connection to a remote server using PHP via the command line on Mac OS X 10.6. I'm currently using PHP's proc_open function to execute the following command:
ssh -t -t -p 22 user#server.com
This almost works. The -t -t options are supposed to force a pseudo terminal which they almost do. I am able to enter the SSH password and press enter. However, after pressing enter the terminal appears to simply hang. No output, no nothing - it's as if the SSH session has failed. I can't run commands or anything and have to kill the whole thing using Ctrl+C. I know the login is successful because I can execute a command like ssh -t -t -p 22 user#server.com "ls -la" and get the correct output.
I thought the problem must be related to the fact that I was using standard pipes in my proc_open call, so I replaced them with pty. I get the following error: "pty pseudo terminal not supported on this system..."
Does Mac OS X simply not support pty or pseudo terminals? (I'm pretty new at using all this shell terminology).
Here's the PHP code:
$descriptorspec = array(0 => array("pty"), 1 => array("pty"), 2 => array("pty"));
$cwd = getcwd();
$process = proc_open('ssh -t -t -p 22 user#server.com', $descriptorspec, $pipes, $cwd);
if (is_resource($process))
{
while (true)
{
echo(stream_get_contents($pipes[1]));
$status = proc_get_status($process);
if (! $status["running"])
break;
}
}
(Sorry - cannot for the life of me figure out SO's formatting instructions...)
What am I doing wrong? Why can't I use pty? Is this just impossible on Mac OS X? Thanks for your help!
You should use public key authentication rather than trying to programmatically bypass interactive password authentication.
The password prompt is supposed to be used from a tty and I believe it was made intentionally difficult to use otherwise. Also the -t -t argument only takes effect once you are connected to the remote host. And I don't believe the PHP function proc_open() can run a command inside a virtual terminal.
To setup public key authentication:
# Generate keypair
ssh-keygen -t rsa
# Copy public key to server
scp ~/.ssh/id_rsa.pub example.com:.ssh/authorized_keys
# Now you shouldn't be prompted for a password when connecting to example.com
# from this host and user account.
ssh example.com
# Since the web server (and thus PHP) probably has its own user account...
# Copy the ~/.ssh/id_rsa file somewhere else
cp ~/.ssh/id_rsa /some_path/id_rsa
# Change ownership of the file to the web server account
chown www-data:www-data /some_path/id_rsa
# Fix the file permissions (ssh ignore the keyfile if it is world readable)
chown 600 /some_path/id_rsa
# Try connecting to the server through the web server account
su -c "ssh -i /some_path/id_rsa -o UserKnownHostsFile=/some_path/known_hosts example.com" www-data
# Add the host to the known hosts file when prompted
Alternately, you could use plink (part of PuTTY for Linux) instead of OpenSSH as it can take the password on the command line plink -pw password example.com. But doing so presents a security risk as anyone who runs ps aux on the server can see the password in the process list.
There is also a program called sshpass that takes the password from an environment variable or command argument and passes it to ssh.
It looks like the problem is best solved using PHP's passthru() function. After alot more (rather painful) research I was able to issue a command through this function and could interact with the remote server through the terminal as if I had run ssh and svn export by hand (they both require passwords, therefore were good tests). What I'm going to have to do is construct a (potentially very long) string of commands separated by && and attach them to the end of the ssh command: ssh -t -t -p 22 hostname command1 && command2 ... The output will be sent to my terminal in Mac OS X even though the commands are being executed on the remote server. Looks like this is the solution I was looking for the whole time - pretty simple really! Thanks to everyone who helped me with this. I gave Alexandre the "green checkmark" because he was the only one who kept responding and was quite helpful in deducing the final answer to the problem. Thanks Alexandre!
This is old, but for any googlers out there, here is an actual solution using proc_open:
Pty descriptors are available in PHP, but have to be configured during compilation (see this 10yr old bug report https://bugs.php.net/bug.php?id=33147)
But in python however, we don't have that problem. So instead of running the ssh command directly, run this python script:
import sys
import pty
args = " ".join(sys.argv[1:])
pty.spawn(['/usr/bin/ssh', args])
About pty.spawn from python docs:
Spawn a process, and connect its controlling terminal with the current
process’s standard io. This is often used to baffle programs which
insist on reading from the controlling terminal.
Have you tried the PHP SSH2 extension?
Have you tried phpseclib, a pure PHP SSH implementation?:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->read('username#username:~$');
$ssh->write("ls -la\n");
echo $ssh->read('username#username:~$');
?>
I wrote a ssh client on php with ssh2 extension, you can take a look to the source code on the github page https://github.com/roke22/PHP-SSH2-Web-Client
Please send some feedback.

Categories