0

I want to open a spawn SSH connection and then query a MySQL Server for new users (in a loop), and if a new user is found a command should be sent to this SSH Connection via Expect.

I don't know if this is possible, up until now i allways kill ssh.exe when i try the "send" command after the MySQL Query.

I want the SSH to be open because it takes 10 seconds to login with Expect (Host ist slow) and i don't want that pause everytime a create a user.

How can i do this?

What i am doing:

...
set db [::mysql::connect -host 127.0.0.1 -user root -password **** -db test]
spawn ssh admin@192.168.1.2
expect {
     timeout { send_user "\nFalscher SSH User admin!\n"; exit 1 }
     "User:"
}
send "admin\r"
expect {
     timeout { send_user "\nFalscher SSH User admin!\n"; exit 1 }
     "Password:"

}
send "******\r"

set x = 1
while {$x>0} {
set query [::mysql::query $db {SELECT username, passwort FROM users WHERE erstellt='0'}]
while {[set row [::mysql::fetch $query]]!=""} {
     set username [lindex $row 0]
     set passwort [lindex $row 1]
     send "create user...;\r"
}
::mysql::endquery $query
after 2000
}
Rory
  • 852
  • 7
  • 12
  • Just curious... why would a trigger on the DB level not be what you want? – mmmmmpie Dec 22 '14 at 15:32
  • well i have never worked with DB triggers.. so i didn't know that possibility thanks, but the idea is, that i have an ssh connection open all the time and then just send a command when a new user is found... – Rory Dec 23 '14 at 17:01
  • When the Expect script finishes, the SSH connection will be closed. In your Expect script you can simply use a `for` or `while` loop to send commands to the remote server whenever you want. – pynexj Dec 24 '14 at 03:08

1 Answers1

0

I solved this by saving the expect output into a file. After starting in the background, ssh wanted to verify my ssl fingerprint. After accepting that, it worked fine.

Rory
  • 852
  • 7
  • 12