如何使用grep查找命令的输出

时间:2016-10-06 14:40:43

标签: bash shell grep

我正在尝试循环访问某些端口以通过ssh进行连接。 shell / bash的新手不太确定如何实现这一目标。

startingPort=xxxx
endingPort=yyyy

ssh(){
ssh admin@localhost -p $startingPort
}

无效端口的输出是ssh:connect to host localhost port 8801:Connection refused 我需要捕获它,然后尝试范围

中的下一个端口

我正在尝试迭代端口号,直到我看到     密码验证     密码:

3 个答案:

答案 0 :(得分:1)

要获取ssh命令的输出,可以使用" $?"进行检查。 例如,如果要记录错误:

//inside the loop (of muzido answer)
ssh admin@localhost -p $port
if [ "$?" -ne 0 ] ; then
    //write into logfile
fi

答案 1 :(得分:1)

谢谢muzido和Theo Pnv;这两个答案的结合帮助我完成了这项任务。

log_file=/directory/log_file.txt
startingPort=8801 
endingPort=8899
port=''

sshToStuff(){
for (( port=$startingPort; port<=$endingPort; port++ ))
do
if [ "$?" -ne 0 ] ; then
    echo "`date -u`" " ssh: connect to host localhost port $port: Connection refused" >> $log_file2;
fi


ssh admin@localhost -p $port
done
}
sshToKaraf

答案 2 :(得分:0)

尝试这样的事情;

  async geolocate() {
    try {
      let result = await navigator.geolocation.getCurrentPosition(
        this.geolocationSuccess.bind(this),
        this.geolocationError.bind(this),
        {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
      );
      if (result != null) return result;
    } catch(err) {
      // This doesn't get called
      Alert.alert(
        "Location unknown",
        "Turn localization services on.",
        [
          {text: 'OK', onPress: () => console.log('OK')},
        ]
      );
    };
  }

  geolocationError(err) {
    console.log(err);
    throw err; // Stops here -> Promise.reject() does better job here, but still results in "Unhandled promise rejection"
  }