Re: [R] Executing a telnet session from R

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Mon, 07 Apr 2008 12:16:32 +0100 (BST)


On 06-Apr-08 23:14:18, Dennis Fisher wrote:
> Colleagues
> I am working in a Linux OS with R 2.6.2.
>
> I need to execute a telnet session to another Linux machine
> from R, perform some operations, then return to the original
> computer. When I am in an R session, this is easy to accomplish
> by typing:
> system("telnet -l username machinename")
> I am then asked for my password; once that is entered, I am
> connected to the remote machine. I can then enter commands on
> the remote machine, then logout.
>
> I would like to automate this process within a function.
> Obviously, I can embed the 'system' command in the function.
>
> Questions:
> 1. How do I enter the password into the command?
> 2. How do I execute the commands on the other machine?
> 3. Finally, how do I execute the 'logout' command (this should not
> differ from #2)?
>
> Thanks in advance for any help on this.
>
> Dennis

As well as Claus Wilke's 'ssh' suggestion, the following (as an example of how an automated telnet session can be done) may give you useful hints for what you want to do.

Explanation: There are two shell scripts here. The purpose is to monitor the Signal-to-Noise Ratio Margin (SNR) of my ADSL router (A BT Voyager 205), every 10 minutes. One can telnet to this device (it has an embedded Linux system). The "wrapper" script is 'getSNR', which establishes a telnet session every 10 minutes. Each telnet session is run by piping the second script '205log.sh' into the 'telnet' command. When it reaches the end of the '205log.sh' script, an implicit "EOF" (^D) is sent to the telnet session, which terminates it. ("myuserid" and "mypassword" are of course dummies for the real ones). The 'sleeps' are important: They give the remote device time to respond and await the next input.

The overall command is

  getSNR.sh >> logfile

getSNR.sh:



#! /bin/bash
while true ; do
echo -n `date | awk '{print $4}'` ": "
./205log.sh | telnet 192.168.1.1 2>/dev/null |   grep "SNR Margin" | awk '{print $5}'
sleep 594
done

205log.sh:



echo myuserid
sleep 1
echo mypassword
sleep 1
echo "get dsl params"
sleep 1
echo "get atm 1483 stats"
sleep 1
echo "get atm aal5 stats"
sleep 2

Hoping this may be useful,
Ted.



E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 07-Apr-08                                       Time: 12:16:27
------------------------------ XFMail ------------------------------

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 07 Apr 2008 - 11:24:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Apr 2008 - 11:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive