3

I would like to connect via ssh to certain equipment in a network.

The requisites are:

  1. It must run a command and capture the output of the ssh session in R (or in bash, or any other programming language, but I would prefer it in R language)

  2. It must enter a plain-text password (as this equipment hasn't been accessed before, and can't be changed with a rsa keypair), so the ssh.utils package doesn't meet this requirement

  3. sshpass can't be used, as I have noticed that it doesn't work for some devices I tested.

I've read all this posts but I can't find an effective way to perform it: link 1, link 2, link 3, link 4

I know the requirements are hard to accomplish, but thank you for your effort!

EDIT:

Sorry if I didn't make myself understandable. I mean I work locally in R and I want to connect to +3000 devices in all of my network via ssh. It is Ubiquiti equipment, and the only open ports are 80 and 22.

If ssh doesn't work, I will use the RSelenium package for R and extract info from port 80. But first I will try with ssh pory 22 as it is a lot more efficient than opening an emulated browser.

The big problem in all these Ubiquiti equipment is that they have a password to log in. That's why requisite No.2 is needed. When I must enter a server that I know, I spend time setting up the rsa keypair so that I don't have to enter a password everytime I connect to a specific server, but it's impossible (or at least, for me it's impossible) to configure all +3000 Ubiquiti equipment with these keypairs.

That's why I don't use snmp, for example, as this equipment maybe they have it activated or not, or the snmp configuration is mistaken. I mean, I have to use something that's activated by default, and in a way, ordered. And only port 80 and port 22 are activated and I know all the user's and password's equipment.

And sshpass is an utility in UNIX/Linux like this link explains that works for servers but doesn't work for Ubiquiti equipment, as long as I've tested it. So I can't use it.

The command I need to extract the output from is mca-status. Simply by entering that into the console makes it print some stats I will like to get from the Ubiquiti equipment.

Correct me, please, if I am wrong in something I've posted. Thanks.

Community
  • 1
  • 1
Geiser
  • 1,054
  • 1
  • 12
  • 28

2 Answers2

1

I think you have this wrong. I also have no idea what you are trying to say in point 2, and I have not idea what point 3 is supposed to say.

Now: ssh is a authentication mechanism allowing you (trusted) access to another machine and the ability to run a command. This can be as simple as

edd@max:~$ ssh bud Rscript -e '2+2'
[1] 4
edd@max:~$ 

where I invoke R (or rather, Rscript) on the machine 'bud' (my desktop) from a session on the machine 'max' (my server). That command could be anything including something which writes to temporary or permanent files. You can then retrieve those files via scp.

Authentication is handled independently -- on Unix we often use ssh-agent which run in the background and against you authenticate on login.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you for your answer. Please see my edited question, if that makes you understand the problem better. – Geiser Jan 06 '16 at 12:04
  • 1
    Hm. You may need to write soemthing yourself, maybe calling `mca-status` and ingesting its output. I am not aware of anything existing. – Dirk Eddelbuettel Jan 06 '16 at 12:07
1

Finally I solved it using the rPython package and the python's paramiko module, as there was no way to do it purely via R.

library(rPython)
python.exec(python.code = c("import paramiko", 
                            "ssh = paramiko.SSHClient()",
                            "ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())",
                            sprintf('ssh.connect("%s", username="USER", password="PASSWORD") ', IP),
                            'stdin, stdout, stderr = ssh.exec_command("mca-status")',
                            'stats = stdout.readlines()'))
Geiser
  • 1,054
  • 1
  • 12
  • 28