Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
Shekar writes: > Depending upon how vendor implemented SNMP part for their device, other > than standard OID's such as sysUptime , will have custom MIB files. > You should try visiting their website for downloading required MIB file for > your switch. > Then import them with -m or put it under snmp path to get its OID's working. Thanks! I couldn't find them. I always stayed away from SNMP, and now that I learned about it, it seems a very good idea to stay away even further. It's an awful mess. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
mailing lists writes: > are you sure? > > # emerge -s expect > > [ Results for search key : expect ] > Searching... > > [...] > * dev-perl/Expect > Latest version available: 1.320.0-r1 > Latest version installed: [ Not Installed ] > Size of files: 61 KiB > Homepage: http://search.cpan.org/dist/Expect/ > Description: Expect for Perl > License: || ( Artistic GPL-1+ ) It turned out to be something else, has nothing to do with ssh. I'm resorting to libssh and do it in C. For the record, see attachment --- it'll need some refinement, but it's working. #include #include #include #include #include int interactive_shell_session(ssh_session session) { ssh_channel channel; int rc; char readbuffer[1024]; char writebuffer[512]; int nbytes, nwritten; int tries = 0; memset(writebuffer, 0, sizeof(writebuffer)); strcpy(writebuffer, "display environment\n"); channel = ssh_channel_new(session); if (channel == NULL) { return SSH_ERROR; } rc = ssh_channel_open_session(channel); if (rc != SSH_OK) { ssh_channel_free(channel); return rc; } rc = ssh_channel_request_pty(channel); if (rc != SSH_OK) return rc; rc = ssh_channel_change_pty_size(channel, 16, 24); if (rc != SSH_OK) return rc; rc = ssh_channel_request_shell(channel); if (rc != SSH_OK) return rc; while ((tries < 4) && ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel)) { nbytes = sizeof(writebuffer); nwritten = ssh_channel_write(channel, writebuffer, nbytes); if (nwritten != nbytes) { return SSH_ERROR; } nbytes = ssh_channel_read_nonblocking(channel, readbuffer, sizeof(readbuffer), 0); while (nbytes > 0) { if (write(1, readbuffer, nbytes) != (unsigned int) nbytes) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } nbytes = ssh_channel_read_nonblocking(channel, readbuffer, sizeof(readbuffer), 0); } if (nbytes < 0) { ssh_channel_close(channel); ssh_channel_free(channel); return SSH_ERROR; } tries++; // printf("\nsleeping\n"); sleep(20); } return rc; } int main() { ssh_session my_ssh_session; int rc; char *password; int verbosity = SSH_LOG_PROTOCOL; // // Open session and set options // my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { fprintf(stderr, "Error getting new session\n"); exit(-1); } ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "switch.example.com"); ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "user"); // ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); // // Connect to server // rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to remote host: %s\n", ssh_get_error(my_ssh_session)); ssh_free(my_ssh_session); exit(-1); } // // Authenticate ourselves // password = "secret"; rc = ssh_userauth_password(my_ssh_session, NULL, password); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session)); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(-1); } interactive_shell_session(my_ssh_session); fprintf(stderr, "\nError: %s\n", ssh_get_error(my_ssh_session)); /* show_temperature(my_ssh_session); */ ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(0); } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
Depending upon how vendor implemented SNMP part for their device, other than standard OID's such as sysUptime , will have custom MIB files. You should try visiting their website for downloading required MIB file for your switch. Then import them with -m or put it under snmp path to get its OID's working. Cheers, Shekar On Thu, Apr 20, 2017 at 7:42 AM, lee wrote: > lee writes: > > > Shekar writes: > > > >> +1 for SNMP, or if Net::SSH::Perl didn't help, can you try expect > module? > > > > Thanks! 'Expect' isn't available as Gentoo package, so I skipped it. > > > > SNMP is probably better, so I need to learn about that first and see if > > I can use it. I guess it's time to learn about SNMP anyway :) > > Ok, I got this far: > > > snmpget -v 3 -u lee h3c .1.3.6.1.2.1.1.3.0 > DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1081149678) 125 days, > 3:11:36.78 > > > Any idea how to get the temperature reading? > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
lee writes: > Shekar writes: > >> +1 for SNMP, or if Net::SSH::Perl didn't help, can you try expect module? > > Thanks! 'Expect' isn't available as Gentoo package, so I skipped it. > > SNMP is probably better, so I need to learn about that first and see if > I can use it. I guess it's time to learn about SNMP anyway :) Ok, I got this far: snmpget -v 3 -u lee h3c .1.3.6.1.2.1.1.3.0 DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1081149678) 125 days, 3:11:36.78 Any idea how to get the temperature reading? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
Shekar writes: > +1 for SNMP, or if Net::SSH::Perl didn't help, can you try expect module? Thanks! 'Expect' isn't available as Gentoo package, so I skipped it. SNMP is probably better, so I need to learn about that first and see if I can use it. I guess it's time to learn about SNMP anyway :) (BTW, I seem to be subscribed again :) ) > > Cheers, > Shekar > > On Wed, Apr 19, 2017 at 1:38 PM, lee wrote: > >> Duncan Ferguson writes: >> >> > If the temperature is available on your switch, can you not enable SNMP >> on it and read the specific OID to get the info? Far far easier than >> trying to keep an ssh connection open, I think. >> > >> > I guess this does depend on the switch type and whether the info is >> available on the device via SNMP, though. >> >> Good idea, thanks! I need to check the documentation if there is a way >> to use SNMP for this. I guess chances are good because it's a pretty >> powerful switch :) >> >> >> (I seem to have become unsubscribed from this list and am trying to >> resubscribe.) >> >> -- >> To unsubscribe, e-mail: beginners-unsubscr...@perl.org >> For additional commands, e-mail: beginners-h...@perl.org >> http://learn.perl.org/ >> >> >> -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
+1 for SNMP, or if Net::SSH::Perl didn't help, can you try expect module? Cheers, Shekar On Wed, Apr 19, 2017 at 1:38 PM, lee wrote: > Duncan Ferguson writes: > > > If the temperature is available on your switch, can you not enable SNMP > on it and read the specific OID to get the info? Far far easier than > trying to keep an ssh connection open, I think. > > > > I guess this does depend on the switch type and whether the info is > available on the device via SNMP, though. > > Good idea, thanks! I need to check the documentation if there is a way > to use SNMP for this. I guess chances are good because it's a pretty > powerful switch :) > > > (I seem to have become unsubscribed from this list and am trying to > resubscribe.) > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
Duncan Ferguson writes: > If the temperature is available on your switch, can you not enable SNMP on it > and read the specific OID to get the info? Far far easier than trying to > keep an ssh connection open, I think. > > I guess this does depend on the switch type and whether the info is available > on the device via SNMP, though. Good idea, thanks! I need to check the documentation if there is a way to use SNMP for this. I guess chances are good because it's a pretty powerful switch :) (I seem to have become unsubscribed from this list and am trying to resubscribe.) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
SSC_perl writes: >> On Apr 18, 2017, at 6:19 PM, lee wrote: >> >> The purpose is to get room temperature readings > > Hey Lee, > > I don’t have a solution for you, but I have an idea that might > help. Have you tried the Misterhouse mailing list? It’s a Perl > script that handles sensors like that so someone there may have done > what you’re trying to do. > > https://sourceforge.net/p/misterhouse/mailman/misterhouse-users/ Thanks, I'll check that out. (I seem to have become unsubscribed from this list and am trying to resubscribe.) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
If the temperature is available on your switch, can you not enable SNMP on it and read the specific OID to get the info? Far far easier than trying to keep an ssh connection open, I think. I guess this does depend on the switch type and whether the info is available on the device via SNMP, though. Duncs -Original Message- From: lee [mailto:l...@yagibdah.de] Sent: 19 April 2017 02:20 To: beginners@perl.org Subject: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output? Hi, I'm trying to repeatedly execute a command on a remote machine and to capture the output with a perl program. I have tried Net::OpenSSH and Net::SSH::Perl. Both log in, execute the command, capture the output --- and then log out. According to the log file of the remote machine, Net::OpenSSH logs out by closing the connection after running the command once despite being designed otherwise. Net::OpenSSH is designed to log out after each command. Since I want to execute the command repeatedly (over long periods of time in intervals of maybe 300 seconds), I do not want to close the connection to the remote machine until my perl program is finished. How could this be achieved? The purpose is to get room temperature readings which are stored in a table in a mysql database. I haven't found any device at a reasonable price that would reliably provide such readings. Using lmsensors works, but the readings are too much influenced by the temperature of the server. I have a switch that provides pretty stable temperature readings which I might be able to use instead, and to get those, I have to log in to the switch and issue a command that shows its temperature. Hence I don't want to log in and out all the time just to get a temperature reading. Suggestions for reliably getting a room temperature reading are also welcome. I do not want to use the serial port because I sometimes need it to connect to a console port on a switch or router, and there's only one serial port. I could use the parallel port or usb, though. I'm not really inclined to solder some sort of diy thingy myself as I'd probably overheat the parts and do more damage than anything else; it's just not my thing doing that kind of stuff. If anything fails, I could get away by hooking up a simple thermostat which opens and closes a contact depending on temperature, as long as I can get a reading whether the contact is opened or closed. It won't be a good solution, yet better than nothing. I'm aware that there are USB sticks ("temper") that /might/ work, but from what I've been reading, they seem rather unreliable even if you can get one to work. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
Hm, IIUC can't the remote script sleep-loop and send output back asynchronously. (More reseach needed on async piece).. $ssh->system('/path/to/remote_script arg, arg,...'); #!/bin/... # remote_script for (...); do get_temp_probe(); . ; sleep 300; done On Tue, Apr 18, 2017 at 6:19 PM, lee wrote: > > Hi, > > I'm trying to repeatedly execute a command on a remote machine and > to capture the output with a perl program. > > I have tried Net::OpenSSH and Net::SSH::Perl. Both log in, execute the > command, capture the output --- and then log out. According to the log > file of the remote machine, Net::OpenSSH logs out by closing the > connection after running the command once despite being designed > otherwise. Net::OpenSSH is designed to log out after each command. > > > Since I want to execute the command repeatedly (over long periods of > time in intervals of maybe 300 seconds), I do not want to close the > connection to the remote machine until my perl program is finished. > > How could this be achieved? > > > The purpose is to get room temperature readings which are stored in a > table in a mysql database. I haven't found any device at a reasonable > price that would reliably provide such readings. > > Using lmsensors works, but the readings are too much influenced by the > temperature of the server. I have a switch that provides pretty stable > temperature readings which I might be able to use instead, and to get > those, I have to log in to the switch and issue a command that shows its > temperature. > > Hence I don't want to log in and out all the time just to get a > temperature reading. > > > Suggestions for reliably getting a room temperature reading are also > welcome. I do not want to use the serial port because I sometimes need > it to connect to a console port on a switch or router, and there's only > one serial port. I could use the parallel port or usb, though. > > I'm not really inclined to solder some sort of diy thingy myself as I'd > probably overheat the parts and do more damage than anything else; it's > just not my thing doing that kind of stuff. > > If anything fails, I could get away by hooking up a simple thermostat > which opens and closes a contact depending on temperature, as long as I > can get a reading whether the contact is opened or closed. It won't be > a good solution, yet better than nothing. > > I'm aware that there are USB sticks ("temper") that /might/ work, but > from what I've been reading, they seem rather unreliable even if you can > get one to work. > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
> On Apr 18, 2017, at 6:19 PM, lee wrote: > > The purpose is to get room temperature readings Hey Lee, I don’t have a solution for you, but I have an idea that might help. Have you tried the Misterhouse mailing list? It’s a Perl script that handles sensors like that so someone there may have done what you’re trying to do. https://sourceforge.net/p/misterhouse/mailman/misterhouse-users/ HTH, Frank -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
how to repeatedly execute a command on a remote machine via a shh login from within a perl program capturing the output?
Hi, I'm trying to repeatedly execute a command on a remote machine and to capture the output with a perl program. I have tried Net::OpenSSH and Net::SSH::Perl. Both log in, execute the command, capture the output --- and then log out. According to the log file of the remote machine, Net::OpenSSH logs out by closing the connection after running the command once despite being designed otherwise. Net::OpenSSH is designed to log out after each command. Since I want to execute the command repeatedly (over long periods of time in intervals of maybe 300 seconds), I do not want to close the connection to the remote machine until my perl program is finished. How could this be achieved? The purpose is to get room temperature readings which are stored in a table in a mysql database. I haven't found any device at a reasonable price that would reliably provide such readings. Using lmsensors works, but the readings are too much influenced by the temperature of the server. I have a switch that provides pretty stable temperature readings which I might be able to use instead, and to get those, I have to log in to the switch and issue a command that shows its temperature. Hence I don't want to log in and out all the time just to get a temperature reading. Suggestions for reliably getting a room temperature reading are also welcome. I do not want to use the serial port because I sometimes need it to connect to a console port on a switch or router, and there's only one serial port. I could use the parallel port or usb, though. I'm not really inclined to solder some sort of diy thingy myself as I'd probably overheat the parts and do more damage than anything else; it's just not my thing doing that kind of stuff. If anything fails, I could get away by hooking up a simple thermostat which opens and closes a contact depending on temperature, as long as I can get a reading whether the contact is opened or closed. It won't be a good solution, yet better than nothing. I'm aware that there are USB sticks ("temper") that /might/ work, but from what I've been reading, they seem rather unreliable even if you can get one to work. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/