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

User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

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       = "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
User avatar
mundmc
Posts: 1068
Joined: Fri Sep 14, 2012 4:34 pm

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

Post by mundmc »

Thanks Karl, This looks incredibly powerful.


Sent from my iPhone using Tapatalk
User avatar
mundmc
Posts: 1068
Joined: Fri Sep 14, 2012 4:34 pm

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

Post by mundmc »

Karl,
I finally tried to play with this on a raspi on Debian linux. Will this strictly only work on Unix? I am seeing the login info from the raspi, but it is then timing out. I am trying to tell firefox on a remote pi to open a window using a command that works from ssh.

Edit: I changed "assword" to "password".
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

Leave assword. As sometimes it is Password and sometimes password

Which Unix. It depends on the prompts

It will not work on windows.


Sent from my iPhone using Tapatalk
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

try:
expect -D ...
or -d
'that gives you a trace


do
man expect
on the unix prompt

for everything you always wanted to know

and expect has to run on the host from where you launch it.- does not need to be available on the target .. = does not use it

Karl
User avatar
mundmc
Posts: 1068
Joined: Fri Sep 14, 2012 4:34 pm

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

Post by mundmc »

kw123 wrote:Leave assword. As sometimes it is Password and sometimes password

Which Unix. It depends on the prompts

It will not work on windows.


Sent from my iPhone using Tapatalk
Lol, assword it is!


MunDMC
Fitter. Happier. More productive.
User avatar
mundmc
Posts: 1068
Joined: Fri Sep 14, 2012 4:34 pm

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

Post by mundmc »

kw123 wrote:try:
expect -D ...
or -d
'that gives you a trace


do
man expect
on the unix prompt

for everything you always wanted to know

and expect has to run on the host from where you launch it.- does not need to be available on the target .. = does not use it

Karl
I’m sorry, Karl, but in reference to the lines above, am i putting them into the script (and, if so, where)?


MunDMC
Fitter. Happier. More productive.
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

Open a terminal on your mac

man expect

q to exit

expect -d your script name here



Sent from my iPhone using Tapatalk
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

to get the full debug into the logfile from within indigo action:

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


ret = subprocess.Popen("/usr/bin/expect -d "+ file, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
indigo.server.log(unicode(ret))

add >> -d  << with the spaces 

lower case d not D

you should then see:
[code]MacPro-2012:~ karlwachs$ /usr/bin/expect -d '....

argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ...

set argc 8
set argv0 "...
set argv ...
executing commands from command file ....
spawn sftp -o ConnectTimeout=15 [email protected]
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {35747}

expect: does "" (spawn_id exp6) match glob pattern "(yes/no)? "? no
"assword"? no
[email protected]'s password: 
expect: does "[email protected]'s password: " (spawn_id exp6) match glob pattern "(yes/no)? "? no
"assword"? yes
expect: set expect_out(0,string) "assword"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "[email protected]'s password"
send: sending "xxxxxx\r" to { exp6 }
....
... and many more lines
but w/o the line brakes instead you will see \n where the line breaks should be..

Karl
User avatar
mundmc
Posts: 1068
Joined: Fri Sep 14, 2012 4:34 pm

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

Post by mundmc »

Karl you are my Indigo spirit totem animal.


MunDMC
Fitter. Happier. More productive.
User avatar
kw123
Posts: 8705
Joined: Sun May 12, 2013 4:44 pm
Location: Dallas, TX
Contact:

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

Post by kw123 »

Is there an emoji for that?


Sent from my iPhone using Tapatalk
User avatar
mundmc
Posts: 1068
Joined: Fri Sep 14, 2012 4:34 pm

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

Post by mundmc »

kw123 wrote:Is there an emoji for that?
Image
Post Reply

Return to “Karl's Plugins and Scripts”