在使用Expect从一台计算机登录到另一台计算机时运行命令,可以按照以下步骤进行操作:
#!/usr/bin/expect
set timeout 10
set username "your_username"
set password "your_password"
set ip_address "target_ip_address"
spawn ssh $username@$ip_address
expect {
"yes/no" {
send "yes\r"
exp_continue
}
"password:" {
send "$password\r"
}
}
expect "$ "
send "your_command\r"
expect "$ "
send "exit\r"
expect eof
在上面的脚本中,你需要将"your_username"替换为目标计算机的用户名,"your_password"替换为目标计算机的密码,"target_ip_address"替换为目标计算机的IP地址,"your_command"替换为你要在目标计算机上运行的命令。
expect script.exp
脚本将自动登录到目标计算机,并在目标计算机上运行指定的命令。
需要注意的是,使用Expect进行自动化操作时,需要确保目标计算机上已经安装了SSH服务,并且允许从远程计算机登录。此外,为了安全起见,建议在脚本中使用加密的SSH密钥进行身份验证。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云