How to use sockets in Lua to telnet? - sockets

In Lua, I downloaded the LuaSocket library and then am trying to:
Connect using telnet
Send some commands
receive output from the commands to parse later
close telnet
What am I doing wrong with this code that is not making that work?
local socket = require("socket")
conn = socket.tcp()
conn:connect("IP Address",23)
conn:send("password")
conn:send("enable")
conn:send("password2") ;; above was all login procedure
conn:send("sh run interfaces ethernet 16")
var1 = conn:receive('*l')
print (var1)
I expected this to give me the first line of code printed off by the sh run interfaces ethernet 16 command, but all I get is 2:) 2v sqrt:) sqrt<3... That is, four random symbols that I don't know what they mean.
Any help on what I did wrong here would be greatly appreciated, thanks.
here are those four symbols:

Update: For all who were curious I needed \r to make the commands go on separate lines, and I had to put a sleep time in between each of the commands so the system would have time to load in between them. It then worked perfectly.

Related

Writing to multiple lines in command prompt VBSCript

I am writing code in VB script.
The script will executing commands for programming, erasing, verfying hex code on microcontrollers in serial production.
The vbscript executes Commad line interface which is running similar like windows command prompt.
After Ppcli.exe is started from VBScript, some commands are sent to the com port. The commamds are shown below:
Get port, OpenPort , SetPowerVoltage 3.3, SetProtocol, EraseAll,Program, ...etc.
Everything works great, but here is the problem. I can send commands in sigle string (Open Port, Set Power, Erase... etc.) but this kind of sending has a big issue.
I can't read the response for the sigle command (OK or not OK) and I can't send the whole hex file at once, because programmer only supports programming, verifying line by line. This means I have to read line from file and send it to microcontroller 128 times.
I have to send 128 commands for write lines in sigle string which is really not the good solution.
I would like to write do while loop, but every time I close command prompt window, the ports is automaticly closed and the communication is lost :(
I would like to know if is it possible to write to command line in the way to send:
Open Port
Read if it is OK or not OK
Program line form hex file 1
Verify line 1
Program Line 2
Verify line 2
etc.
I really don't know what to do, since I am not able to communicate with the programmer in any other way than single string sending. But the string is then very long and there is no diagnostic if something goes wrong while programming:(
I was sure that CLI (command line interface) would be the best choice to program microcontrollers but now I am stuck, since I didn't know that it is only possible programming row by row and not the whole hex program at once :(
If you have any solution for me I would be glad.
Can I run Perl code from VBS? I also have working pearl example for programming devices?
Thank you,
Separate commands with & sign.

Execute Commands in the Linux Commandline [Lazarus / Free Pascal]

I have a problem. I want to execute some commands in the Commandline of linux. I tested TProcess (So i am using Lazarus) but now when i am starting the programm, there is nothing, wich the Program do.
Here is my Code:
uses [...], unix, process;
[...]
var LE_Path: TLabeledEdit;
[...]
Pro1:=TProcess.Create(nil);
Pro1.CommandLine:=(('sudo open'+LE_Path.Text));
Pro1.Options := Pro1.Options; //Here i used Options before
Pro1.Execute;
With this Program, i want to open Files with sudo (The Programm is running on the User Interface)
->Sorry for my Bad English; Sorry for fails in the Question: I am using StackOverflow the first time.
I guess the solution was a missing space char?
Change
Pro1.CommandLine:=(('sudo open'+LE_Path.Text));
to
Pro1.CommandLine:=(('sudo open '+LE_Path.Text));
# ----------------------------^--- added this space char.
But if you're a beginner programmer, my other comments are still worth considering:
trying to use sudo in your first bit of code may be adding a whole extra set of problems. SO... Get something easier to work first, maybe
/bin/ls -l /path/to/some/dir/that/has/only/a/few/files.
find out how to print a statement that will be executed. This is the most basic form of debugging and any language should support that.
Your english communicated your problem well enough, and by including sample code and reasonable (not perfect) problem description "we" were able to help you. In general, a good question contains the fewest number of steps to re-create the problem. OR, if you're trying to manipulate data,
a. small sample input,
b. sample output from that same input
c. your "best" code you have tried
d. your current output
e. your thoughts about why it is not working
AND comments to indicate generally other things you have tried.

have perl debugger not stop at first statement

I'm trying to find a way to start a perl process up under the debugger, but have in just start running automatically without stopping at the first statement and having me enter 'c' to start it. This module is run by a larger system and I need to be able to periodically (based on external conditions) interrupt the program via interrupt signal, examine some data structures and have it continue.
Obviously, I can have the supervising program start my process using "perl -d myProcess", but how to get it to just run without the initial break. Anybody know how to get this to happen?
Much Thanks.
Thanks. That was a big hint. I see several options including "NonStop".
It looks like using the line PERLDB_OPTS="NonStop" perl -d myprog.pl & does the trick. Then I just kill -INT <pid> and fg it to get it up in the debugger. After I 'c' to continue executing and bg it so it will continue.
You can also add this config option to the .perldb file. The format of the config file is a bit unusual and not so well documented, so here goes
DB::parse_options("NonStop=1");
Other useful options:
DB::parse_options("dumpDepth=3");
DB::parse_options("PrintRet=0");
$DB::deep = 1000;

How do I forward the current Asterisk call in AGI using Perl?

I have an asterisk-based PBX, and I have been able to successfully run an AGI script from the web control panel of the PBX.
Because I am calling AGI from Perl (neither of which I know, yet)...
asterisk commands look like this:
print "SET CALLERID $newcid \"\"\n";
So far, I don't expect to need to do any database lookups, etc. I simply want to set the caller ID (which I can do already) - then forward the call to a particular number.
Can anyone show me how to forward the current call using the AGI/perl style of executing asterisk commands shown above?
FYI: I've seen some examples using a format like:
Exten => blah blah....
But, I am not sure how to convert that to the AGI/perl format.
print "EXEC DIAL \"Zap/1/5551234567\"\n";
or, if you are using Asterisk::AGI,
$AGI->exec("DIAL", "Zap/1/5551234567");

"inappropriate ioctl for device"

I have a Perl script running in an AIX box.
The script tries to open a file from a certain directory and it fails to read the file because file has no read permission, but I get a different error saying inappropriate ioctl for device.
Shouldn't it say something like no read permissions for file or something similar?
What does this inappropriate ioctl for device message mean?
How can I fix it?
EDIT: This is what I found when I did strace.
open("/local/logs/xxx/xxxxServer.log", O_WRONLY|O_CREAT|O_APPEND|O_LARGEFILE,
0666) = 4 _llseek(4, 0, [77146], SEEK_END) = 0
ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, 0xbffc14f8) = -1 ENOTTY
(Inappropriate ioctl for device)
Most likely it means that the open didn't fail.
When Perl opens a file, it checks whether or not the file is a TTY (so that it can answer the -T $fh filetest operator) by issuing the TCGETS ioctl against it. If the file is a regular file and not a tty, the ioctl fails and sets errno to ENOTTY (string value: "Inappropriate ioctl for device"). As ysth says, the most common reason for seeing an unexpected value in $! is checking it when it's not valid -- that is, anywhere other than immediately after a syscall failed, so testing the result codes of your operations is critically important.
If open actually did return false for you, and you found ENOTTY in $! then I would consider this a small bug (giving a useless value of $!) but I would also be very curious as to how it happened. Code and/or truss output would be nifty.
Odd errors like "inappropriate ioctl for device" are usually a result of checking $! at some point other than just after a system call failed. If you'd show your code, I bet someone would rapidly point out your error.
"inappropriate ioctl for device" is the error string for the ENOTTY error. It used to be triggerred primarily by attempts to configure terminal properties (e.g. echo mode) on a file descriptor that was no terminal (but, say, a regular file), hence ENOTTY. More generally, it is triggered when doing an ioctl on a device that does not support that ioctl, hence the error string.
To find out what ioctl is being made that fails, and on what file descriptor, run the script under strace/truss. You'll recognize ENOTTY, followed by the actual printing of the error message. Then find out what file number was used, and what open() call returned that file number.
Since this is a fatal error and also quite difficult to debug, maybe the fix could be put somewhere (in the provided command line?):
export GPG_TTY=$(tty)
From: https://github.com/keybase/keybase-issues/issues/2798
"files" in *nix type systems are very much an abstract concept.
They can be areas on disk organized by a file system, but they could equally well be a network connection, a bit of shared memory, the buffer output from another process, a screen or a keyboard.
In order for perl to be really useful it mirrors this model very closely, and does not treat files by emulating a magnetic tape as many 4gls do.
So it tried an "IOCTL" operation 'open for write' on a file handle which does not allow write operations which is an inappropriate IOCTL operation for that device/file.
The easiest thing to do is stick an " or die 'Cannot open $myfile' statement at the end of you open and you can choose your own meaningful message.
I just fixed this perl bug.
See https://rt.perl.org/Ticket/Display.html?id=124232
When we push the buffer layer to PerlIO and do a failing isatty() check
which obviously fails on all normal files, ignore the wrong errno ENOTTY.
Eureka moment!
I have had this error before.
Did you invoke the perl debugger with something like :-
perl -d yourprog.pl > log.txt
If so whats going on is perl debug tries to query and perhaps reset the terminal width.
When stdout is not a terminal this fails with the IOCTL message.
The alternative would be for your debug session to hang forever because you did not see the prompt for instructions.
Ran into this error today while trying to use code to delete a folder/files that are living on a Windoze 7 box that's mounted as a share on a Centos server. Got the inappropriate icotl for device error and tried everything that came to mind. Read just about every post on the net related to this.
Obviously the problem was isolated to the mounted Windoze share on the Linux server. Looked
at the file permissions on the Windoze box and noted the files had their permissions set to read only.
Changed those, went back to the Linux server and all worked as expected. This may not be the solution for most but hopefully it saves someone some time.
I tried the following code that seemed to work:
if(open(my $FILE, "<File.txt")) {
while(<$FILE>){
print "$_";}
} else {
print "File could not be opened or did not exists\n";
}
I got the error Can't open file for reading. Inappropriate ioctl for device recently when I migrated an old UB2K forum with a DBM file-based database to a new host. Apparently there are multiple, incompatible implementations of DBM. I had a backup of the database, so I was able to load that, but it seems there are other options e.g. moving a perl script/dbm to a new server, and shifting out of dbm?.
I also get this error "inappropriate ioctl for device" when try to fetch file stat.
It was first time when I got a chance to work on perl script.
my $mtime = (stat("/home/ec2-user/sample/test/status.xml"))[9]
Above code snippet was throwing error. Perl script was written in version 5.12 on Windows, and I have to run it on amazon linux having perl 5.15.
In my case error was because of Array index out of bond ( In java language sense).
When I modified code my $var = (stat("/home/ec2-user/sample/test/status.xml"))[0][9]; error gone and I get correct value.
Of course, it is too late to answer, but I am posting my finding so that it can be helpful for developer community.
If some perl expert can verify this, it will be great..