I need to write a script to monitor wlan0 interface of my remote machine. I can do this by executing iwconfig wlan0
repeatedly on remote machine. But how to login to that machine and execute this command via telnet. I can do this via ssh, but ssh takes time to login and it hangs if connection to remote machine drops in the middle of execution.
1 Answer
Instead of repeatedly invoking the ssh command, you could run ssh to repeatedly execute the command on the remote system and then parse and use the output locally. Then you wouldn't have to keep running the ssh connection and putting up with the initial lag.
For example:
ssh remotehost "while [ "1" ] ; do iwconfig wlan0 ; sleep 1 ; done"
Or, you could use netcat to send the output on the remote system back to your machine. For example, run this on the local machine:
nc -l 9999
Then on the remote machine:
while [ "1" ] ; do iwconfig wlan0 ; sleep 1 | nc localmachine 9999
Replace localmachine here with the host or ip of the machine that will be doing the "monitoring".
You could even pipe the output from nc reading that port into another command.
Closer to your actual question, you could do this on the remote system to act as a telnet "server":
while [ "1" ] ; do iwconfig wlan0 | nc -l 9999 ; done
Then you could do this to get the outupt:
telnet remotehost 9999
-
Doing ssh as you told creates a process on remote machine. I couldn't able to kill that from my local machine. Is there any way to kill that?– KumarMay 29, 2015 at 7:40
-
-
nope i tried it. The remote process is not terminating even though ssh is killed.– KumarMay 29, 2015 at 11:49
-
Create a script with a unique name, put your command in that, invoke that from the ssh command, and use a second ssh command to kill that script, but then you're back to waiting around for the ssh handshake to finish in order to do the kill. The second command would look like this:
ssh remotehost killall uniquely-named-iwconfig-calling-script.sh
– StephenMay 29, 2015 at 12:04 -
The very last entry of my "answer" is probably the closest to what you want. While it does provide unauthenticated access to the output of the
iwconfig
command, that's all it provides. And it's not using much in he way of resources while it's waiting for new connections. Did you try that one, yet?– StephenMay 29, 2015 at 12:11