how to use SSH to send any command to a unix box

Posted on
Sun Oct 07, 2018 3:02 pm
kw123 offline
User avatar
Posts: 6133
Joined: May 12, 2013
Location: Dallas, TX

how to use SSH to send any command to a unix box

put this into action/ server script
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       = ""
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.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'
   +'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'

###### 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()

###### 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


Posted on
Mon Oct 15, 2018 9:23 am
mundmc offline
Posts: 382
Joined: Sep 14, 2012

Re: how to use SSH to send any command to a unix box

Thanks Karl, This looks incredibly powerful.

Sent from my iPhone using Tapatalk

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests