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: 6584
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       = "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

Posted on
Mon Oct 15, 2018 9:23 am
mundmc offline
User avatar
Posts: 556
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

Posted on
Mon Feb 11, 2019 8:17 pm
mundmc offline
User avatar
Posts: 556
Joined: Sep 14, 2012

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

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".

Posted on
Tue Feb 12, 2019 1:15 am
kw123 offline
User avatar
Posts: 6584
Joined: May 12, 2013
Location: Dallas, TX

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

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

Posted on
Tue Feb 12, 2019 11:49 am
kw123 offline
User avatar
Posts: 6584
Joined: May 12, 2013
Location: Dallas, TX

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

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

Posted on
Wed Feb 13, 2019 7:34 am
mundmc offline
User avatar
Posts: 556
Joined: Sep 14, 2012

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

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.

Posted on
Wed Feb 13, 2019 7:36 am
mundmc offline
User avatar
Posts: 556
Joined: Sep 14, 2012

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

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.

Posted on
Wed Feb 13, 2019 8:27 am
kw123 offline
User avatar
Posts: 6584
Joined: May 12, 2013
Location: Dallas, TX

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

Open a terminal on your mac

man expect

q to exit

expect -d your script name here



Sent from my iPhone using Tapatalk

Posted on
Wed Feb 13, 2019 5:12 pm
kw123 offline
User avatar
Posts: 6584
Joined: May 12, 2013
Location: Dallas, TX

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

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 pi@192.168.xxx
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
pi@192.168.1.39's password:
expect: does "pi@192.168.1.39'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) "pi@192.168.1.xxx'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

Posted on
Wed Feb 13, 2019 8:54 pm
mundmc offline
User avatar
Posts: 556
Joined: Sep 14, 2012

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

Karl you are my Indigo spirit totem animal.


MunDMC
Fitter. Happier. More productive.

Posted on
Wed Feb 13, 2019 9:10 pm
kw123 offline
User avatar
Posts: 6584
Joined: May 12, 2013
Location: Dallas, TX

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

Is there an emoji for that?


Sent from my iPhone using Tapatalk

Posted on
Thu Feb 14, 2019 7:33 am
mundmc offline
User avatar
Posts: 556
Joined: Sep 14, 2012

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

kw123 wrote:
Is there an emoji for that?


Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests