Однако я получаю следующую ошибку при попытке передать идентификатор появления второму скрипт:
не может найти канал с именем "exp5"
при выполнении
Ожидать, что скрипт установит ssh-соединение:
Код: Выделить всё
#!/usr/bin/expect -f
# ssh_persistent_control.exp
# Suppress the display of the process interaction to terminal
log_user 1
# Get arguments
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
#set pipe "/var/www/html/commands/ssh_command_pipe"
set output_pipe "/var/www/html/commands/ssh_command_output_pipe"
set spawnId_pipe "/var/www/html/commands/ssh_spawnId_pipe"
set timeout 5 ; # Adjust as needed
# Global variable to store the spawn_id
set ssh_spawn_id -1
proc open_connection {username password hostname} {
global ssh_spawn_id output_pipe spawnId_pipe
if {$ssh_spawn_id != -1} {
return
}
# Create the named pipe if it doesn't exist
if {![file exists $output_pipe]} {
exec mkfifo $output_pipe
exec chmod 666 $output_pipe
}
# Open the named pipe for writing
set pipe_file [open $output_pipe w]
# Create the named pipe if it doesn't exist
if {![file exists $spawnId_pipe]} {
exec mkfifo $spawnId_pipe
exec chmod 666 $spawnId_pipe
}
# Open the named pipe for writing
set spawnId_pipe_file [open $spawnId_pipe w]
puts $pipe_file "Attempting to connect...\n"
# Start the SSH connection
spawn sshpass -p $password ssh -o StrictHostKeyChecking=no -o ServerAliveInterval=30 $username@$hostname
set ssh_spawn_id $spawn_id
# Write the spawn ID to the named pipe
puts $spawnId_pipe_file $ssh_spawn_id
close $spawnId_pipe_file
expect {
"assword:" {
send "$password\r"
exp_continue
}
"$username@$hostname's password:" {
send "$password\r"
exp_continue
}
"yes/no" {
send "yes\r"
exp_continue
}
"Welcome to SSH Server..." {
puts $pipe_file "Connected successfully. Ready to send commands."
expect {
-re {.*?(\r\n|\n)} {
set response $expect_out(buffer)
puts $pipe_file $response
exp_continue
}
}
}
timeout {
send_user "Failed to connect: Timeout while waiting for welcome message.\n"
puts $pipe_file "Failed to connect: Timeout while waiting for welcome message."
close $pipe_file
set ssh_spawn_id -1
return 0
}
eof {
send_user "Failed to connect: Connection closed unexpectedly.\n"
puts $pipe_file "Failed to connect: Connection closed unexpectedly."
close $pipe_file
set ssh_spawn_id -1
return 0
}
}
}
# Open the connection
open_connection $username $password $hostname
Выдает ошибку
Невозможно найти канал с именем «exp5»
во время выполнения
Код: Выделить всё
#!/usr/bin/expect -f
# send_command.exp
set output_pipe "/var/www/html/commands/ssh_command_output_pipe"
set spawnId_pipe "/var/www/html/commands/ssh_spawnId_pipe"
# Open the spawnId pipe for reading
set spawnId_pipe_file [open $spawnId_pipe r]
# Read the spawn ID
#set spawn_id [gets $spawnId_pipe_file]
set spawn_id [exp_id $spawnId_pipe_file] ;
# Close the pipe file
close $spawnId_pipe_file
# Create the named pipe if it doesn't exist
if {![file exists $output_pipe]} {
exec mkfifo $output_pipe
exec chmod 666 $output_pipe
}
# Open the named pipe for writing
set pipe_file [open $output_pipe w]
if {$spawn_id == ""} {
puts $pipe_file "No active SSH session found."
exit 1
}
# Get the command from the arguments
set command [lindex $argv 0]
if {[string equal $command ""]} {
puts $pipe_file "No command provided."
exit 1
}
puts $pipe_file "spawn id is :$spawn_id"
# Send the command to the existing SSH session
send -- "$command\n"
# Read the response
expect {
-re {.*?(\r\n|\n)} {
set response $expect_out(buffer)
puts $pipe_file "Response: $response"
}
timeout {
puts $pipe_file "No response received."
}
}
Я ссылался на следующий похожий ответ на сообщение, но до сих пор не понимаю, что я необходимо изменить:
как запустить сценарий ожидания по заданному идентификатору появления
Подробнее здесь: https://stackoverflow.com/questions/790 ... -in-anothe