An-Najah Blogs :: Razi Alsayyed Blog http://blogs.najah.edu/author/razi-sayed An-Najah Blogs :: Razi Alsayyed Blog en-us Sun, 24 Sep 2017 17:48:06 IST Sun, 24 Sep 2017 17:48:06 IST webmaster@najah.edu webmaster@najah.edu Make a Post Request through PHP Scripthttp://blogs.najah.edu/staff/razi-sayed/article/Make-a-Post-Request-through-PHP-ScriptGeneral PostsMaking a GET Request through PHP is very easy you can treat the URL of the target page as a file open it using fopen while include the parameters in the URL String: result = file_get_contentswwwexamplecom?param1=value1param2=value2; But some times GET does not work specially when you need to communicate with other platforms other servers which already written and expect a POST request Making a Post Request is Easy to but it is a little tricky because in POST requests you can not add the paramters to the end of the URL as in GET instead you need to send them in the HTTP headers and in order to do that you need to open a socket using fsocketopen and send all the HTTP headers that the target server expect: function PostRequesturl referer _data { convert variables array to string: data = array; whilelistnv = each_data{ data[] = urlencoden=urlencodev; } data = implode data; format -- test1=atest2=b etc parse the given URL url = parse_urlurl; if url[scheme] = http { dieOnly HTTP request are supported ; } extract host and path: host = url[host]; path = url[path]; open a socket connection on port 80 fp = fsockopenhost 80; send the request headers: fputsfp POST path HTTP11\r\n; fputsfp Host: host\r\n; fputsfp Referer: referer\r\n; fputsfp Content-type: applicationx-www-form-urlencoded\r\n; fputsfp Content-length: strlendata \r\n; fputsfp Connection: close\r\n\r\n; fputsfp data; result = ; whilefeoffp { receive the results of the request result = fgetsfp 128; } close the socket connection: fclosefp; split the result header from the content result = explode\r\n\r\n result 2; header = issetresult[0] ? result[0] : ; content = issetresult[1] ? result[1] : ; return as array: return arrayheader content; } using the above function is very easy you just need to put all your parameters in an associative array and pass it to the function: data = array test = foobar\ntest okay = yes number = 2 ; listheader content = PostRequest http:wwwexamplecompost_tophp the url to post to http:wwwexamplecompostphp the url of the post script this file data ; echo content; PHP script to find a PC location in a network based on its MAC addresshttp://blogs.najah.edu/staff/razi-sayed/article/PHP-script-to-find-a-PC-location-in-a-network-based-on-its-MAC-addressGeneral PostsFollowing is a PHP script I wrote to find a PC location in a farm of CISCO switches based on its MAC address the script connects to a central switch trys to find where the PC is connected on which port if it does not find it then the script connects to the next switch based on CDP protocol NOTE: Some regular expressions may be not accurate in all circumstances take it as is at your risk or feel free to change it ?php function logintelnetusernamepassword { response = readBuffertelnet; ifusername = null fputstelnet{username}\r\n; fputstelnet{password}\r\n; response = readBuffertelnet; if the switch in user mode then go to exec mode ifpreg_match[a-z0-9_\\\-]iresponse { response = sendCommandtelneten; response = sendCommandtelnetpassword; } preg_match[a-z0-9_\\\-][]iresponseout; return str_replaceout[0]; return hostname } function sendCommandtelnetc { fputstelnet{c}\r\n; return readBuffertelnet; } function readBuffertelnet { str = ; whiletrue { result = fgetstelnet2; ifordresult = 8 backspace str = result; if substrstr-8 == --More-- { str = preg_replace[\-]{2}More[\-]{2}i str; fputstelnet ; } if result == || result == || result == : || preg_match[a-z0-9_\\\-][]istr preg_matchPassword:\sistr break; } return str; } the following function are executed through regular expression to replace short interface names with full names you can add other short names here function fullnameshortname { switchshortname { case fa: return FastEthernet; case gi: return GigabitEthernet; } return shortname; } function findMACswitchmac { telnet = fsockopenswitch23errnoerrstr6030; hostname = logintelnetnullPASSWORD; execute show mac-address-table on the switch result = sendCommandtelnet sh mac-address-table | include mac; get the interface where the PC connects to it may be the next switch based on cdp output below preg_match\s[A-Z][0-9]\[0-9]\siresultout; interface = out[1]; if the interface is in short name format convert it to full name interface = preg_replace^[a-z]{2}[0-9]ie fullnamestrtolower\\1\2 interface; execute show cdp neighbors detail on the switch result = sendCommandtelnetsh cdp ne de; get all neighbors deviceid = \sDevice\sID:\s?[a-z0-9_\\\-]; ipaddress = IP\saddress:\s?[0-9]\[0-9]\[0-9]\[0-9]; port = Interface:\s?[A-Z][0-9]\[0-9]; neighbors_count = preg_match_all{deviceid}\s? {ipaddress}\s? {port}i result out; if the interface in the cdp neighbors output then the PC is not connected to this switch repeat the process on the neighbor switch fori=0;ineighbors_count;i { ifstrcasecmp out[5][i]interface == 0 { echo Device ID: hostnamebr ; echo IP Address: switchbr ; echo Interface: interfacebr ; echo Next Device: out[1][i]hr ; findMACout[3][i]mac; return; } } if we reach here then we find the interface print it out echo Device ID: hostnamebr ; echo IP Address: switchbr ; echo Interface: interfacehr ; } how to use mainswitch = 19216811; macaddress = xxxxxxxxxxxx; findMACmainswitchmacaddress; ?