在Ubuntu上使用SecureCRT执行自定义脚本,可参考以下方法:
首先安装Expect:sudo apt-get install expect。然后创建脚本文件,如securecrt_auto.sh,内容如下:
#!/usr/bin/expect
set timeout 20
set session [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn securecrt -ssh $username@$session -password $password
send "$command \r"
expect eof
给脚本添加可执行权限:chmod +x securecrt_auto.sh。运行脚本:./securecrt_auto.sh <session> <username> <password> "<command>"。
在SecureCRT中,选择“Options”>“Global Options”>“Startup”>“Scripts”,添加Python等脚本。例如编写一个Python脚本script.py:
import os
host = "your_remote_host"
username = "your_username"
password = "your_password"
command = "your_command"
os.system(f"ssh {username}@{host} << EOF
{command}
EOF")
将脚本保存到本地,在SecureCRT的“Scripts”设置中添加它,连接服务器时脚本会自动执行。
先在本地创建脚本并赋予执行权限,如chmod +x my_script.sh。然后用SecureCRT的SFTP功能将脚本上传到远程服务器。接着打开SecureCRT连接到远程服务器,依次点击“Options”>“Configure”>“Default Sessions”,选择会话,在“Connection”标签的“Logon Actions”中勾选“Automate Logon”,在“Send string at start”中输入脚本在服务器上的实际路径,之后连接该服务器就会自动执行脚本。