0

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?

vishnu
  • 891
  • 2
  • 11
  • 20
  • 3
    What does "But that does not worked out." actually mean? What happens? _Be precise..._ – arkascha Nov 07 '16 at 10:06
  • Is there a reason why do you have to run the first script in a sub-shell? – Inian Nov 07 '16 at 10:06
  • 1
    AND using `(exec)` in shell scripting is usually not required. Are you doing this because of a special case, or because you're used to that cmd in `python` or other language? But, even if you remove that this example won't do much. Get things to work from the cmd-line. Maybe you need to run you server in the background with `/path/to/server -opts x y z file &`? Then see if it responds on the port to a telnet connection. Good luck. – shellter Nov 07 '16 at 10:18
  • @shellter. yeah manually i could connect to server. it works!! But after that i need to issue certain commands repeatedly. I want to automate. How to interact with this guile shell? – vishnu Nov 07 '16 at 10:39
  • @Inian: I can run manually. But after that i want to issue "parse" command to server repeatedly. How to interact with this guile shell? – vishnu Nov 07 '16 at 10:44
  • @arkascha: I have edited the question. – vishnu Nov 07 '16 at 10:51
  • 1
    telnet won't open another window, it's a console application. Take a look at "expect". – Stefan Hegny Nov 07 '16 at 11:22

3 Answers3

2

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
Aleksandar Pavić
  • 3,143
  • 1
  • 34
  • 36
0

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.

vishnu
  • 891
  • 2
  • 11
  • 20
  • 1
    This may not work depending on the server because the TELNET protocol is not simple plain text over TCP (client and server exchange some information during connection setup). I'm not aware of any `nc` flag to support this automatically. The `telnet` therefore works better than `nc`, but since it doesn't handle well its standard input like `nc` does, `expect` is a nice tool to run `telnet` with proper handling of input/output. – André Sassi Nov 07 '16 at 12:31
0

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.

Lyralover
  • 21
  • 4