- Code: Select all
##### put me into an action action window/server/execute script
import subprocess
userID = "the userid on the unixbox"
password = "the password on the unix box"
ipNumber = "192.168.1.xxx"
promptOnServer = "the prompt on the unix box eg @"
cmd = "ls -l" ## the command you like to send
file = "/users/your-mac-id-here/documents/ssh.cmd"
f=open(file,"w")
f.write('spawn ssh '+userID+'@'+ipNumber+'\n'
+'set timeout 10\n'
+' expect {\n'
+' "(yes/no)?" {\n'
+' send "yes\\n"\n'
+' sleep 0.1\n'
+' expect "assword" { send "'+password+'\\n"}\n'
+' }\n'
+' "assword: " {\n'
+' send "'+password+'\\n"\n'
+' }\n'
+'}\n'
+'expect '+ promptOnServer+'\n'
+'sleep 0.1\n'
+'send "'+cmd+' \\n"\n'
+'sleep 0.1\n'
+'expect '+ promptOnServer+'\n'
+'sleep 0.1\n'
+'send "exit\\n"\n'
+'expect eof\n'
)
f.close()
###### either
## if you want to wait for the result:
ret = subprocess.Popen("/usr/bin/expect "+ file, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
indigo.server.log(unicode(ret))
###### or
## if it takes to long ( > 10 secs) just add & to the end
## subprocess.Popen("/usr/bin/expect "+ file+" &", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
hope some might find this useful
Karl