在 Mac 上自动执行 telnet 登录和命令

在 Mac 上自动执行 telnet 登录和命令

目前我做的事情是这样的:

telnet 192.168.0.3 23

username

password

cd /dir/

programname -s -g dosomething

我想通过一个步骤来完成此操作,最酷的方法是使用 Automator 为此编译一个程序,但我不知道该怎么做。Automator
中的“shell 脚本”当然无法telnet 192.168.0.3 23显示“连接被拒绝”:我没有机会输入密码等等。

有任何想法吗?

答案1

当 ssh 不可用时,expect通常用于使用 telnet 自动访问

来自链接的文章:

#!/usr/bin/expect #Where the script should be run from.
set timeout 20 #If it all goes pear shaped the script will timeout after 20 seconds.
set name [lindex $argv 0] #First argument is assigned to the variable name
set user [lindex $argv 1] #Second argument is assigned to the variable user
set password [lindex $argv 2] #Third argument is assigned to the variable password
spawn telnet $name #This spawns the telnet program and connects it to the variable name
expect "login:" #The script expects login
send "$user " #The script sends the user variable
expect "Password:" #The script expects Password
send "$password " #The script sends the password variable
interact #This hands control of the keyboard over two you (Nice expect feature!)

答案2

然后,如果没有安装 sshd,您可以使用 expect 自动化您的 telnet,您可能必须在您的 mac 上安装它。

Expect 是一种主要用于自动化交互式应用程序(如 telnet、ftp、passwd、fsck、rlogin、tip 等)的工具。Expect 确实使这些事情变得非常简单。Expect 还可用于测试这些应用程序。它在许多书籍、文章、论文和常见问题解答中都有介绍。O'Reilly 有一整本关于它的书。

http://www.nist.gov/el/msid/expect.cfm

答案3

如果在 192.168.0.3 上运行的系统正在运行 sshd 服务器,那么您只需执行

ssh [email protected] 'cd /dir/ ; programmname -s -g dosomething'

要检查 192.168.0.3 上是否运行了 sshd,您可以尝试一下,或者像以前一样使用 telnet 登录,如果是 unix/linux/maxosx,请尝试执行

pgrep -fl sshd

或者

ps -e | grep sshd

或者

lsof -i :22

以上所有这些要么找不到任何内容,要么给你一行显示 sshd 正在运行。

相关内容