Test::More failing test with equal strings - unit-testing

Using perl 5.16 with the latest Test::More module.
I got a unit test being called like
is($ret, "ssh: connect to host 1.2.3.187 port 22: Network is unreachable\nCouldn't read packet: Connection reset by peer\n", "putFileWithSFTP bad server ret test");
Running the test fails with the following result:
# Failed test 'putFileWithSFTP bad server ret test'
# at t/Backup.t line 891.
# got: 'ssh: connect to host 1.2.3.187 port 22: Network is unreachable
# Couldn't read packet: Connection reset by peer
# '
# expected: 'ssh: connect to host 1.2.3.187 port 22: Network is unreachable
# Couldn't read packet: Connection reset by peer
# '
The strings should be equal and they also look like they are equal. What could be the cause of this?

I figured it out:
When comparing the two strings with a hexdump, I noticed that, for whatever reason, the ssh ouput in $ret contains MSWin Line Endings, which I didn't include in my comparison.
More can be read here

Related

CSocket does not work on LAN

I'm learning network-programming and try to develop a simple socket application that used CSocket class from MFC. And found myself hitting a wall that need some help.
I want the server side listens on a certain port, example 1001. Then from other computer, on the same sub-net, it should successfully telnet to that port. My program works correctly on localhost, but fail on the LAN, although I have opened that port on firewall of listener.
Here is my code from listener:
//CListenSocket is derived from CSocket
CListenSocket myListener;
myListener.Create(1001);
myListener.Listen();
//OnAccept()
//CConnectSocket is also derived from CSocket
CConnectSocket myConnect;
myListener.Accept(myConnect);
I built the release version using VS2008, here is the screenshot of the configuration properties:
So at this stage, when I run the program, netstat -an show this line:
TCP 0.0.0.0:1001 0.0.0.0:0 LISTENING
Then on that machine telnet 127.0.0.1 1001, this line appears.
TCP 127.0.0.1:1001 127.0.0.1:2681 ESTABLISHED
So I think my code is correct. After that I tried from other machine with the same sub-net, and the telnet fail:
Connecting To 192.168.2.199...Could not open connection to the host, on port 1001: Connect failed
Note: that my listener is on 192.168.2.199, and the connector is on 192.168.2.3. Both nodes can successfully ping and sharing file with each other. I also add both Inbound Rule and Outbound Rule for the program on my firewall, here is the properties of the rule:
For more information: On my listener node, there is Apache HTTP server installed on it, so I have the other node telnet to port 80, and it works...
So where did I miss? please help, thank you in advance.
EDITION 1:
Attempt for troubleshooting
So after hitting my head to the table for a while, I quickly make a decision for not use telnet for client anymore. But make a small client program to catch errors:
//CClientSocket is derived from CSocket
CClientSocket clientSocket;
clientSocket.Create();
int iConnect = clientSocket.Connect(ipAddress,1001) //ipAddress is a variable of MFC's text box on GUI.
switch (iConnect)
{
case 0:
{
DWORD errorNumber = ::GetLastError(); //catch error code
CString s_errorNumber;
s_errorNumber.Format("%d",errorNumber); //format to CString for easy echo
MessageBox("Connection fail :"+s_errorNumber)
clientSocket.ShutDown(CAsyncSocket::both);
clientSocket.Close();
break;
case SOCKET_ERROR:
if (::GetLastError() != WSAEWOULDBLOCK)
clientSocket.ShutDown(CAsyncSocket::both);
else
clientSocket.AsyncSelect();
break;
default:
{
MessageBox("Connection Established.");
}
break;
}
And the error number is: 10061. I checked this code on MSDN an it is WSAECONNREFUSED-Connection refused.
Now we know the problem here must be somewhere in the listener's firewall... still hiting my head to the table.

Telnet client console: SEND invalid command - Only for Spanish version

I’ve developed an application in QT C++ (server with listening sockets) waiting for clients to connect and send commands.
It was developed and tested in Windows XP Professional Version 2002 SP3 English version and tested in Windows seven 64 bits using telnet client sending strings to server from command prompt using reserved word SEND without problems.
Actually I am testing this app from another Windows XP Professional Version 2002 SP3 Spanish version machine and I’m having problems with sending strings.
Telnet client connects correctly, receives data from server, but when I move to command prompt and try to send strings I receive and error.
Microsoft Telnet> send MYSTRING
Invalid command. type ?/help for help
Microsoft Telnet> ?
Commands may be abbreviated. Supported commands are:
c – close close current connection
d – display display operating parameters
o – open hostname [port] connect to hostname (default port 23).
q – quit exit telnet
set – set set options (type ‘set ?’ for a list)
sen – send send strings to server
st – status print status information
u – unset unset options (type ‘unset ?’ for a list)
?/h – help print help information
Microsoft Telnet> d
Escape Character is ‘CTRL+}’
Will auth(NTLM Authentication)
Local echo off
New line mode – Causes return key to send CR & LF
Current mode: Console
Will term type
Preferred term type is ANSI
Note: Above transcription is translated, I get this message in Spanish
I need to know if this problem, telnet not recognizing a reserved word is a configuration or security problem.
Windows Firewall is deactivated.
EDIT1:
To clear situation on problem with Telnet Client:
I develop an App in Windows XP 2002 SP3 English version. This app is a server in a network listening in port 6000. Waits for clients to connect (through Telnet client for example) over lan.
The pc running my app has IP1 and its working.
From PC2 with same os I connect using >telnet IP1 6000 and after I connect and receive data from my app (server). Then I press 'ctrl+}' and get to Telnet prompt Microsoft Telnet> send AnyString AnyString has been sent to server. My app receives data correctly, process it and works fine.
From PC3 using W7 and Ubuntu virtual machines, same procedure than PC2 and it works fine.
From PC4 os Windows XP SP3 2002 Spanish i connect using >telnet IP1 6000 and after I connect and receive data from my app (server). Then I press 'ctrl+}' and get to Telnet prompt Microsoft Telnet> send AnyString Invalid command. type ?/help for help
That's why I suppose Telnet client on XP can be "configured?"
To send a text message over TCP/IP using telnet client you need just to call telnet with address (and port if not default 23 should be used), i.e:
> telnet 192.168.1.1 13 // tell me your time
> string to be sent // type windows's enter
> // should be sent as a text to 192.168.1.1 on port 13
when i move to command prompt and try to send strings i receive and
error.
Microsoft Telnet> send MYSTRING
Invalid command. type ?/help for help
The command is named sen, not send. Maybe a send is also sen but in help only sen is showed so better stick with it.
You can move to telnet mode by typing escape character that might be i.e. '^]' then type sen:
> telnet 192.168.1.1 13
> ctrl+]
Microsoft Telnet> sen my string here
> // should be sent as a text to 192.168.1.1 on port 13
Microsoft telnet client sen command terminates strings with null: when you type 5 characters p i o t r it terminates these 5 bytes with 00 byte ( '\0') so there are 6 bytes sent:
p i o t r .
70 69 6f 74 72 00
In case of problems with Microsoft Telnet you can always install a putty or write a simple telnet client:
void
str_cli(FILE *fp, int sockfd) {
char buf[MAXLINE];
int n;
for ( ;;) {
if ((n = Read(fileno(fp), buf, MAXLINE)) < 0) {
err_quit("str_cli:");
}
Writen( sockfd, buf, n);
}
}
int main( int argc, char** argv) {
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2)
err_quit("usage: tcpcli <IPaddress>");
sockfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons( SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
str_cli( stdin, sockfd); /* do it all */
close( sockfd);
exit(0);
}

GDB Server and GDB Client who sends the "+" first?

GDB Client:
NetworkClientConnect 503: Attempting host: 10.23.37.155 (addr: 02CE4B50)
NetworkClientConnect 518: Connected to host: 10.23.37.155
NetworkClientRecv 576: Recv Packet: +
NetworkClientSend 550: Sent Packet: +
GDB Server:
Debug: 243 275 pld.c:207 handle_pld_init_command(): Initializing PLDs...
Info : 244 22937 server.c:83 add_connection(): accepting 'gdb' connection from 3333
Debug: 247 22954 gdb_server.c:260 gdb_get_char_inner(): received '+'
Debug: 248 22954 gdb_server.c:272 gdb_get_char_inner(): returned char '+' (0x2b)
Initially the connections are made then they acknowledge that they got the packet by sending "+". In my case the client says it is receiving a '+' and so does the server as the very first info exchange. That does not make sense. One has to send and the other receive what I see is both receiving and sending in parallel. But it is working. So where is my thinking wrong? Also if you can point me to a URL which shows exactly the GDB Server and Client protocol exchange that would be awesome.
In your GDB client printout, it looks to me, messages are not printed in order (see that Recv packet has number 576, and sent 550).
Use wireshark or similar tool to debug an issue like this.
I tried connecting to gdbserver via loopback and according to wireshark the dialogue looks like this:
client sends "+"
client sends "$qSupported:multiprocess+;xmlRegisters=i386;qRelocInsn+#b5"
server sends "+"
server sends "$PacketSize=3fff;QPassSignals+;..."
and so on.
Gdb does help an option selectable at runtime that can help debug such things. Start it, then issue "set debug remote 1". Same on remote side. Start gdbserver by "gdbserver --remote-debug ...". This will print remote gdb protocol dialogue on both sides.
Another, possibly best if most time consuming options is to check the gdb&gdbserver source.
I got into WireShark Help Forum (http://ask.wireshark.org/) and posed the question there. "How to capture packets between 2 IP's". There a person called Quadratic gave a brilliant answer. You can refer the WireShark site or here it is. It works like a charm!!
Do this:
• When you first start Wireshark, click on the button in the far upper-left that says "List the available capture interfaces" when you scroll over it.
• In the new "Capture Interfaces" window that opens, select the interface you want to capture packets (with the check box on the left-hand side) and click"Options".
• In the Capture Options window, on the lower-left corner there should be a "Stop Capture Automatically After..." seciton. Check the "packets" option and put in a value of 50
• In the same Capture Options window, in the text box to the right of "Capture Filter", type the statement (without quotes) "ip host 10.xx.xx.xx and ip host 10.yy.yy.yy".
• Hit the Start button :)
One small thing to note - if the interface you're capturing is doing vlan tagging, replace the capture filter statement to "vlan and ip host 10.xx.xx.xx and ip host 10.yy.yy.yy" without quotes.
Edit:
An even simpler solution is to just use one command line statement:
C:\Program Files\Wireshark\dumpcap.exe -c 50 -i {interface name or number} -w {wherever you want to save the packet capture file}

Telnet SMTP with expect or shell script

Want to build up a Auth Smtp Connection with expect script... just to test I wanted to get ehlo parameters but expect is not working like this
#!/usr/bin/expect
set timeout -1
set smtp [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $smtp $port
expect "[2]{2,}[0]{1,}"
send "ehlo\n"
I expect the code 220 to come from mailserver to continue to send ehlo ... just like
..../...:telnet smtp.mail.yahoo.de 25
Trying 77.238.184.85...
Connected to smtp2-de.mail.vip.ukl.yahoo.com.
Escape character is '^]'.
220 smtp116.mail.ukl.yahoo.com ESMTP
ehlo
250-smtp116.mail.ukl.yahoo.com
250-AUTH LOGIN PLAIN XYMCOOKIE
250-PIPELINING
250-SIZE 41697280
250 8BITMIME
error saying:
spawn telnet smtp.mail.yahoo.de 25
invalid command name "2"
while executing
"2"
invoked from within
"expect "[2]{2,}[0]{1,}""
(file "./login.exp" line 6)
if I just write expect "220" instead of expect "[2]{2,}[0]{1,}" it works but ignors send "ehlo\n"
As above adviced I used exp_internal 1 to get sense of what expects really listen to...
Also I can recommend autoexpect which created the expect script not perfectly but after improving some codings it is a real help and at last it worked.
#!/usr/bin/expect
#exp_internal 1
set timeout -1
set smtp [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $smtp $port
expect -re {[2]{2,}[0]{1,}}
sleep 3;
send -- "Ehlo\r"
expect -re {[2]{1,}[5]{1,}[0]{1,}}
send -- "quit\r"
expect eof
You need to send a newline after sending "ehlo":
send "ehlo\n"
EDIT: Based on your latest edit, you also have to escape the leading bracket in your regex to prevent tcp from trying to interpret it as a command:
expect "\[2]{2,}\[0]{1,}"
EDIT: Also, your expect line isn't actually matching what you think it is. At this point, I'd suggest running through on of the many tutorials on expect, or simply use autoexpect to generate your script.

talking between python tcp server and a c++ client

I am having an issue trying to communicate between a python TCP server and a c++ TCP client.
After the first call, which works fine, the subsequent calls cause issues.
As far as WinSock is concerned, the send() function worked properly, it returns the proper length and WSAGetLastError() does not return anything of significance.
However, when watching the packets using wireshark, i notice that the first call sends two packets, a PSH,ACK with all of the data in it, and an ACK right after, but the subsequent calls, which don't work, only send the PSH,ACK packet, and not a subsequent ACK packet
the receiving computers wireshark corroborates this, and the python server does nothing, it doesnt have any data coming out of the socket, and i cannot debug deeper, since socket is a native class
when i run a c++ client and a c++ server (a hacked replica of what the python one would do), the client faithfully sends both the PSH,ACk and ACK packets the whole time, even after the first call.
Is the winsock send function supposed to always send a PSH,ACK and an ACK?
If so, why would it do so when connected to my C++ server and not the python server?
Has anyone had any issues similar to this?
client sends a PSH,ACK and then the
server sends a PSH,ACK and a
FIN,PSH,ACK
There is a FIN, so could it be that the Python version of your server is closing the connection immediately after the initial read?
If you are not explicitly closing the server's socket, it's probable that the server's remote socket variable is going out of scope, thus closing it (and that this bug is not present in your C++ version)?
Assuming that this is the case, I can cause a very similar TCP sequence with this code for the server:
# server.py
import socket
from time import sleep
def f(s):
r,a = s.accept()
print r.recv(100)
s = socket.socket()
s.bind(('localhost',1234))
s.listen(1)
f(s)
# wait around a bit for the client to send it's second packet
sleep(10)
and this for the client:
# client.py
import socket
from time import sleep
s = socket.socket()
s.connect(('localhost',1234))
s.send('hello 1')
# wait around for a while so that the socket in server.py goes out of scope
sleep(5)
s.send('hello 2')
Start your packet sniffer, then run server.py and then, client.py. Here is the outout of tcpdump -A -i lo, which matches your observations:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 96 bytes
12:42:37.683710 IP localhost:33491 > localhost.1234: S 1129726741:1129726741(0) win 32792 <mss 16396,sackOK,timestamp 640881101 0,nop,wscale 7>
E..<R.#.#...............CVC.........I|....#....
&3..........
12:42:37.684049 IP localhost.1234 > localhost:33491: S 1128039653:1128039653(0) ack 1129726742 win 32768 <mss 16396,sackOK,timestamp 640881101 640881101,nop,wscale 7>
E..<..#.#.<.............C<..CVC.....Ia....#....
&3..&3......
12:42:37.684087 IP localhost:33491 > localhost.1234: . ack 1 win 257 <nop,nop,timestamp 640881102 640881101>
E..4R.#.#...............CVC.C<......1......
&3..&3..
12:42:37.684220 IP localhost:33491 > localhost.1234: P 1:8(7) ack 1 win 257 <nop,nop,timestamp 640881102 640881101>
E..;R.#.#...............CVC.C<......./.....
&3..&3..hello 1
12:42:37.684271 IP localhost.1234 > localhost:33491: . ack 8 win 256 <nop,nop,timestamp 640881102 640881102>
E..4.(#.#...............C<..CVC.....1}.....
&3..&3..
12:42:37.684755 IP localhost.1234 > localhost:33491: F 1:1(0) ack 8 win 256 <nop,nop,timestamp 640881103 640881102>
E..4.)#.#...............C<..CVC.....1{.....
&3..&3..
12:42:37.685639 IP localhost:33491 > localhost.1234: . ack 2 win 257 <nop,nop,timestamp 640881104 640881103>
E..4R.#.#...............CVC.C<......1x.....
&3..&3..
12:42:42.683367 IP localhost:33491 > localhost.1234: P 8:15(7) ack 2 win 257 <nop,nop,timestamp 640886103 640881103>
E..;R.#.#...............CVC.C<......./.....
&3%W&3..hello 2
12:42:42.683401 IP localhost.1234 > localhost:33491: R 1128039655:1128039655(0) win 0
E..(..#.#.<.............C<......P...b...
9 packets captured
27 packets received by filter
0 packets dropped by kernel
What size of packets do you send?
If they are small - may be Nagle's Algorith & Delayed ACK Algorithm is your headache? From what you described think Delayed ACK is involved...