exec() and arrays...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

exec() and arrays...

Post by tutigan »

Hi all! I'm relatively new to PHP, so please forgive me if this is actually a stupid question!
background:
I am trying to make a webpage (which I will integrate into my company's nagios website) to restart selected services of windows servers.
I have found how to restart a specific service, and how to list the services using the exec function, but this is where my problem comes in...
I want to be able to input an IP address, and have it list the services on that IP, each one being a link to restart the service.
To be able to list the services of a server, I put it into an array and then have it print out all the contents...
problem:
when I list the services of a server, it lists them all on one line... how do I seperate each entry? and how would I go about adding tags around each line?
This is my code so far:

Code: Select all

<?php
if($_POST['submit'] ) {
        $ip = "$_POST[ip]";
        $cmd = "net rpc service list -I " . $ip . " -U Administrator%Password";
        exec($cmd, $output);
        print_r($output);
} else {
        echo "<form method='post' action=''>\n";
        echo "<input type='text' name='ip' />\n";
        echo "<input type='submit' name='submit' value='submit' />\n";
        echo "</form>";
}
?>
the PHP version is PHP Version 5.3.2-1ubuntu4.5 (if that makes any kind of difference)
Any help would be greatly appreciated :D
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: exec() and arrays...

Post by klevis miho »

Replace print_r($output); with:

echo '<pre>'.print_r($output).'</pre>';
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

Re: exec() and arrays...

Post by tutigan »

Hi, thanks for the reply...
This is the output of the code:

Code: Select all

Array
(
    [0] => AeLookupSvc             "Application Experience Lookup Service"
    [1] => Alerter                 "Alerter"
    [2] => ALG                     "Application Layer Gateway Service"
    [3] => AppMgmt                 "Application Management"
    [4] => aspnet_state            "ASP.NET State Service"
    [5] => AudioSrv                "Windows Audio"
    [6] => BITS                    "Background Intelligent Transfer Service"
    [7] => Browser                 "Computer Browser"
    [8] => CiSvc                   "Indexing Service"
    [9] => ClipSrv                 "ClipBook"
    [10] => clr_optimization_v2.0.50727_32    ".NET Runtime Optimization Service v2.0.50727_X86"
    [11] => COMSysApp               "COM+ System Application"
    [12] => CryptSvc                "Cryptographic Services"
    [13] => DcomLaunch              "DCOM Server Process Launcher"
    [14] => Dfs                     "Distributed File System"
    [15] => Dhcp                    "DHCP Client"
    [16] => DHCPServer              "DHCP Server"
    [17] => dmadmin                 "Logical Disk Manager Administrative Service"
    [18] => dmserver                "Logical Disk Manager"
    [19] => DNS                     "DNS Server"
    [20] => Dnscache                "DNS Client"
    [21] => EarthAgent              "Trend ServerProtect Agent"
    [22] => ERSvc                   "Error Reporting Service"
    [23] => EUQ_Migrator            "ScanMail EUQ Migrator service"
    [24] => EUQ_Monitor             "ScanMail EUQ Monitor"
    [25] => Eventlog                "Event Log"
    [26] => EventSystem             "COM+ Event System"
    [27] => FontCache3.0.0.0        "Windows Presentation Foundation Font Cache 3.0.0.0"
    [28] => helpsvc                 "Help and Support"
    [29] => HidServ                 "HID Input Service"
    [30] => HTTPFilter              "HTTP SSL"
    [31] => IAS                     "Internet Authentication Service"
    [32] => IDriverT                "InstallDriver Table Manager"
    [33] => idsvc                   "Windows CardSpace"
    [34] => IISADMIN                "IIS Admin Service"
    [35] => IMAP4Svc                "Microsoft Exchange IMAP4"
    [36] => ImapiService            "IMAPI CD-Burning COM Service"
    [37] => IsmServ                 "Intersite Messaging"
    [38] => kdc                     "Kerberos Key Distribution Center"
    [39] => lanmanserver            "Server"
    [40] => lanmanworkstation       "Workstation"
    [41] => LicenseService          "License Logging"
    [42] => LmHosts                 "TCP/IP NetBIOS Helper"
    [43] => Messenger               "Messenger"
    [44] => mnmsrvc                 "NetMeeting Remote Desktop Sharing"
    [45] => MSDTC                   "Distributed Transaction Coordinator"
    [46] => MSExchangeES            "Microsoft Exchange Event"
    [47] => MSExchangeIS            "Microsoft Exchange Information Store"
    [48] => MSExchangeMGMT          "Microsoft Exchange Management"
    [49] => MSExchangeMTA           "Microsoft Exchange MTA Stacks"
    [50] => MSExchangeSA            "Microsoft Exchange System Attendant"
    [51] => MSExchangeSRS           "Microsoft Exchange Site Replication Service"
    [52] => MSFtpsvc                "FTP Publishing Service"
    [53] => MSIServer               "Windows Installer"
    [54] => MSSEARCH                "Microsoft Search"
    [55] => named                   "ISC BIND"
    [56] => NetDDE                  "Network DDE"
    [57] => NetDDEdsdm              "Network DDE DSDM"
    [58] => Netlogon                "Net Logon"
    [59] => Netman                  "Network Connections"
    [60] => NetTcpPortSharing       "Net.Tcp Port Sharing Service"
    [61] => Nla                     "Network Location Awareness (NLA)"
    [62] => NntpSvc                 "Network News Transfer Protocol (NNTP)"
    [63] => NSClient                "Nagios Agent"
    [64] => NSClientpp              "NSClient++ (Win32)"
    [65] => NtFrs                   "File Replication Service"
    [66] => NtLmSsp                 "NT LM Security Support Provider"
    [67] => NtmsSvc                 "Removable Storage"
    [68] => ofcservice              "OfficeScan Master Service"
    [69] => OfficeScanCMAgent       "OfficeScan Control Manager Agent"
    [70] => OSCEIntegrationService    "OfficeScan Active Directory Integration Service"
    [71] => PlugPlay                "Plug and Play"
    [72] => PolicyAgent             "IPSEC Services"
    [73] => POP3Svc                 "Microsoft Exchange POP3"
    [74] => ProtectedStorage        "Protected Storage"
    [75] => RasAuto                 "Remote Access Auto Connection Manager"
    [76] => RasMan                  "Remote Access Connection Manager"
    [77] => RDSessMgr               "Remote Desktop Help Session Manager"
    [78] => RemoteAccess            "Routing and Remote Access"
    [79] => RemoteRegistry          "Remote Registry"
    [80] => RESvc                   "Microsoft Exchange Routing Engine"
    [81] => RpcLocator              "Remote Procedure Call (RPC) Locator"
    [82] => RpcSs                   "Remote Procedure Call (RPC)"
    [83] => RSoPProv                "Resultant Set of Policy Provider"
    [84] => sacsvr                  "Special Administration Console Helper"
    [85] => SamSs                   "Security Accounts Manager"
    [86] => ScanMail_Master         "ScanMail for Microsoft Exchange Master Service"
    [87] => ScanMail_RemoteConfig    "ScanMail for Microsoft Exchange Remote Configuration Server"
    [88] => ScanMail_SystemWatcher    "ScanMail for Microsoft Exchange System Watcher"
    [89] => SCardSvr                "Smart Card"
    [90] => Schedule                "Task Scheduler"
    [91] => seclogon                "Secondary Logon"
    [92] => SENS                    "System Event Notification"
    [93] => ShadowProtect ImageManager    "ShadowProtect ImageManager"
    [94] => ShadowProtectSvc        "ShadowProtect Service"
    [95] => SharedAccess            "Windows Firewall/Internet Connection Sharing (ICS)"
    [96] => ShellHWDetection        "Shell Hardware Detection"
    [97] => SMTPSVC                 "Simple Mail Transfer Protocol (SMTP)"
    [98] => Spooler                 "Print Spooler"
    [99] => Spyser                  "Spyser"
    [100] => stisvc                  "Windows Image Acquisition (WIA)"
    [101] => swprv                   "Microsoft Software Shadow Copy Provider"
    [102] => SysAidAgent             "SysAid Agent"
    [103] => SysmonLog               "Performance Logs and Alerts"
    [104] => TapiSrv                 "Telephony"
    [105] => TermService             "Terminal Services"
    [106] => Themes                  "Themes"
    [107] => TlntSvr                 "Telnet"
    [108] => TmImssAdminUI           "Trend Micro IMSS Web Console"
    [109] => TmImssCMAgent           "Trend Micro IMSS CMAgent Service"
    [110] => TmImssIpprofiler        "Trend Micro IMSS IPProfiler"
    [111] => TmImssManager           "Trend Micro IMSS Manager"
    [112] => TmImssMTA               "Trend Micro IMSS SMTP Service"
    [113] => TmImssPolicy            "Trend Micro IMSS Policy Service"
    [114] => TmImssScan              "Trend Micro IMSS Scan Service"
    [115] => TmImssTasks             "Trend Micro IMSS Task Services"
    [116] => tmlisten                "OfficeScan NT Listener"
    [117] => TmProxy                 "OfficeScan NT Proxy Service"
    [118] => TrkSvr                  "Distributed Link Tracking Server"
    [119] => TrkWks                  "Distributed Link Tracking Client"
    [120] => Tssdis                  "Terminal Services Session Directory"
    [121] => UMWdf                   "Windows User Mode Driver Framework"
    [122] => UPS                     "Uninterruptible Power Supply"
    [123] => vds                     "Virtual Disk Service"
    [124] => VSNAPVSS                "StorageCraft Shadow Copy Provider"
    [125] => VSS                     "Volume Shadow Copy"
    [126] => W32Time                 "Windows Time"
    [127] => W3SVC                   "World Wide Web Publishing Service"
    [128] => WebClient               "WebClient"
    [129] => WG Security Event Processor    "WG Security Event Processor"
    [130] => WinHttpAutoProxySvc     "WinHTTP Web Proxy Auto-Discovery Service"
    [131] => winmgmt                 "Windows Management Instrumentation"
    [132] => WINS                    "Windows Internet Name Service (WINS)"
    [133] => WinVNC4                 "VNC Server Version 4"
    [134] => WmdmPmSN                "Portable Media Serial Number Service"
    [135] => Wmi                     "Windows Management Instrumentation Driver Extensions"
    [136] => WmiApSrv                "WMI Performance Adapter"
    [137] => wuauserv                "Automatic Updates"
    [138] => WZCSVC                  "Wireless Configuration"
    [139] => xmlprov                 "Network Provisioning Service"
)
and when I change the line to be as you suggested, the following line is added at the end of the page:

Code: Select all

<pre>1</pre>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: exec() and arrays...

Post by s.dot »

You will want to loop through your output:

Code: Select all

foreach ($output AS $line)
{
    echo $line . '<br />';
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

Re: exec() and arrays...

Post by tutigan »

thanks s.dot! that works brilliantly! :D
Post Reply