我有一段时间的问题是否可能为Perl或Bash脚本:
我该怎么做?
感谢伟大的网站!
这是可能的,但正确的工具是期望的 。 大多数脚本语言也可以使用类似于期望的库。
你的问题是用Perl标记的,所以我假设你正在使用它。
其他假设:
我将在本地和远程机器之间创buildssh密钥对 *,这使您可以build立一个安全连接而不必input密码(对于自动会话非常有用)。 链接是谷歌,因为有这么多的教程,并且他们略有不同,所以你应该find一个为您的操作系统版本。
一旦你能够做到这一点,你可以远程执行命令
$local machine> ssh remoteserver.com /file/to/execute.sh
输出将显示在您的本地机器上。 然后你可以从perl中取出shell并执行这个命令,然后将输出保存到一个文件中,就像在本地机器上执行脚本一样。
这里是一个示例expect脚本,将自动化您的SSH(假设)login资料来源: http : //bash.cyberciti.biz/security/expect-ssh-login-script/
#!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # set Variables set password [lrange $argv 0 0] set ipaddr [lrange $argv 1 1] set scriptname [lrange $argv 2 2] set arg1 [lrange $argv 3 3] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh root@$ipaddr $scriptname $arg1 match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "$password\r" # send blank line (\r) to make sure we get back to gui send -- "\r" expect eof
我期待着这样做,但没有奏效:最后closures了连接。
我们可以通过ssh运行一个脚本,它将login到远程机器,运行一个命令,而不是断开 ?
所以在一台机器上ssh,cd到这样一个目录,然后运行一个命令,并保持login状态。
-Jonathan