Linux 使用shell和传递命令打开telnet

Linux 使用shell和传递命令打开telnet,linux,bash,shell,guile,Linux,Bash,Shell,Guile,我不熟悉linux和shell脚本。我想连接到localhost并与之交互 #! /bin/bash (exec /opt/scripts/run_server.sh) 当我执行这个bash脚本时,它开始侦听端口 Listening on port xxxxx 现在我想发出这个命令“telnet localhost xxxxx” 我试过这样的方法: #! /bin/bash (exec /opt/opencog/scripts/run_server.sh)&& telne

我不熟悉linux和shell脚本。我想连接到localhost并与之交互

 #! /bin/bash
 (exec /opt/scripts/run_server.sh)
当我执行这个bash脚本时,它开始侦听端口

Listening on port xxxxx
现在我想发出这个命令“telnet localhost xxxxx” 我试过这样的方法:

#! /bin/bash
(exec /opt/opencog/scripts/run_server.sh)&&
telnet localhost xxxxx
/usr/bin/sshpass -p 'password' /usr/bin/ssh 
-o StrictHostKeyChecking=no  -q user@localhost 'any command'
它仍在监听端口。但我认为第二个命令没有运行。我希望另一个窗口显示它正在像这样连接

vishnu@xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server> 
我之所以以脚本的形式执行这些命令,是因为在服务器中,我需要自动执行一些过程,通过发出某些命令,如“scm”“parse”等

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")
我有很多短信要发。我无法手动为每个句子发出此解析命令。所以我想自动化。因此,我需要编写一个脚本来连接到服务器并进行交互


任何指导方针。最后,如何交互/发送命令到此guile shell?

以相同或不同的用户身份登录linux服务器并运行一些命令或.sh脚本(对于提交后挂钩或cron作业非常有用)的一种方法是使用名为sshpass的程序,例如cron作业命令或svn提交后挂钩如下所示:

#! /bin/bash
(exec /opt/opencog/scripts/run_server.sh)&&
telnet localhost xxxxx
/usr/bin/sshpass -p 'password' /usr/bin/ssh 
-o StrictHostKeyChecking=no  -q user@localhost 'any command'
只需将密码替换为您的密码,将用户替换为您的用户,并将您需要作为该特定用户运行的命令

要在ubuntu上安装sshpassit,只需键入

apt-get install sshpass
还是在CentOs

yum install sshpass

以相同或不同用户身份登录linux服务器并运行某些命令或.sh脚本(对于提交后挂钩或cron作业非常有用)的一种方法是使用名为sshpass的程序,例如,cron作业命令或svn提交后挂钩如下所示:

#! /bin/bash
(exec /opt/opencog/scripts/run_server.sh)&&
telnet localhost xxxxx
/usr/bin/sshpass -p 'password' /usr/bin/ssh 
-o StrictHostKeyChecking=no  -q user@localhost 'any command'
只需将密码替换为您的密码,将用户替换为您的用户,并将您需要作为该特定用户运行的命令

要在ubuntu上安装sshpassit,只需键入

apt-get install sshpass
还是在CentOs

yum install sshpass

我用netcat(nc)命令解决了这个问题

  $ echo "command1\ncommand2\n" | nc localhost xxxxx

我可以使用telnet localhost xxxx手动连接到localhost,然后我可以像这样将命令从shell传递到localhost。

我用netcat(nc)命令解决了这个问题

vishnu@xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server> 
  $ echo "command1\ncommand2\n" | nc localhost xxxxx

我可以使用telnet localhost xxxx手动连接到localhost,然后我可以像这样将命令从shell传递到localhost。

什么是“但这不起作用”。实际上是什么意思?会发生什么?准确地说……您为什么必须在子shell中运行第一个脚本?通常不需要在shell脚本中使用
(exec)
。您这样做是因为一个特殊情况,还是因为您已经习惯了python或其他语言中的cmd?但是,即使你删除了这个例子,它也不会有多大作用。从命令行开始工作。也许您需要在后台使用
/path/to/server-opts x y z文件运行服务器&
?然后查看它是否在端口上响应telnet连接。祝你好运。是的,我可以手动连接到服务器。它起作用了!!但在那之后,我需要反复发出某些命令。我想自动化。如何与这个guile shell交互?telnet不会打开另一个窗口,它是一个控制台应用程序。看看“期望”。什么是“但那不起作用。”实际上是什么意思?会发生什么?准确地说……您为什么必须在子shell中运行第一个脚本?通常不需要在shell脚本中使用
(exec)
。您这样做是因为一个特殊情况,还是因为您已经习惯了python或其他语言中的cmd?但是,即使你删除了这个例子,它也不会有多大作用。从命令行开始工作。也许您需要在后台使用
/path/to/server-opts x y z文件运行服务器&
?然后查看它是否在端口上响应telnet连接。祝你好运。是的,我可以手动连接到服务器。它起作用了!!但在那之后,我需要反复发出某些命令。我想自动化。如何与这个guile shell交互?telnet不会打开另一个窗口,它是一个控制台应用程序。看看“expect”。这可能不起作用,这取决于服务器,因为TELNET协议不是简单的TCP纯文本协议(客户端和服务器在连接设置期间交换一些信息)。我不知道有任何
nc
标志自动支持此操作。因此,
telnet
nc
工作得更好,但由于它不能像
nc
那样很好地处理其标准输入,
expect
是运行
telnet
并正确处理输入/输出的好工具。这可能不适用于服务器,因为telnet协议不是简单的TCP纯文本协议(客户端和服务器在连接设置期间交换一些信息)。我不知道有任何
nc
标志自动支持此操作。因此,
telnet
nc
工作得更好,但由于它不能像
nc
那样很好地处理其标准输入,
expect
是运行
telnet
并正确处理输入/输出的好工具。
vishnu@xps15:~$ telnet localhost xxxx
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
server>