2016-03-09 10 views
0

我需要幫助寫一個簡單的shell腳本,它會掃描協議的端口:FTP ,SSH,SMTP,HTTP爲網絡中的前10臺主機。寫一個簡單的shell腳本,它會掃描協議的端口:FTP,SSH,SMTP,HTTP的第10個網絡中的主機

#Select port range 
$portrange = 20,21,22,25,80 
#Open connection for each port from the range 
Foreach ($p in $portrange) 
{ 
$Socket = New-Object Net.Sockets.TcpClient  
$ErrorActionPreference = 'SilentlyContinue' 
#Connect on the given port 
$Socket.Connect("10.59.80.64/24") 
#Determine if the connection is established 
if ($Socket.Connected) { 
Write-Host "Outbound port $p is open." -ForegroundColor Yellow 
$Socket.Close() 
} 
else { 
Write-Host "Outbound port $p is closed or filtered."} 
} #end foreach 

感謝

回答

0

該腳本將掃描的端口,並保存在文件scanport.out掃描信息。 我使用FTP(21),SSH(22),STMP(25)和HTTP(80)的默認端口。

> scanport.out #creates blank file 

for i in {0..9} #scan 10 first hosts 
do 
    nc -v -z 192.168.1.$i 21 >> scanport.out 2>&1 #ftp 
    nc -v -z 192.168.1.$i 22 >> scanport.out 2>&1 #ssh 
    nc -v -z 192.168.1.$i 25 >> scanport.out 2>&1 #smtp 
    nc -v -z 192.168.1.$i 80 >> scanport.out 2>&1 #http 
done 

我希望它能幫助

+0

嗨路易斯,這是我的代碼,但我試圖增加更多的主機, #選擇端口範圍 $ portrange = 20,21,22,25,80 #Open連接從每個端口範圍 FOREACH(在$ $ portrange p) { $插座=新物體Net.Sockets.TcpClient $ ErrorActionPreference = 'SilentlyContinue' #Connect給定端口 $ Socket.Connect(「10.59.80.64/上24" ) #Determine如果連接是如果($ Socket.Connected){ 的Wri建立 te-Host「出站端口$ p已打開。」 -ForegroundColor黃 $ Socket.Close() } 其他{ 寫主機「出站口$ p被關閉或過濾。」}} 的foreach#結束 – John

+0

哦對不起我以爲你使用shell Linux,但我看到它的電源外殼。在這種情況下,我會回答。 –

0

這個腳本會掃描端口使用PowerShell。

$net="10.67.198" 
$range=210..220 
#Select port range 
$portrange = 20,21,22,25,80 

foreach ($r in $range) { 

    $ip="{0}.{1}" -F $net,$r 
    write-host $ip 

    #Open connection for each port from the range 
    Foreach ($p in $portrange) { 

     $Socket = New-Object System.Net.Sockets.TcpClient 

     $ErrorActionPreference = 'SilentlyContinue' 

     #Connect on the given port 
     $Socket.Connect($ip, $p) 

     $ErrorActionPreference = 'Continue' 

     #Determine if the connection is established 
     if ($Socket.Connected) 
     { 
      Write-Host "Outbound port $p is open." -ForegroundColor Yellow 
      $Socket.Close() 
     } 
     else { 
      Write-Host "Outbound port $p is closed or filtered." 
     } 

     $Socket = $null 

    } 
    #end foreach 

}