Legged robots have advanced potential in mobility compared with wheeled or tracked vehicles in harsh environment. In real applications, both environmental perception and terrain identification play an important role in ensuring the safety of the robot. However, few papers involve the obstacle avoidance and terrain identification methods for legged robots. An obstacle avoidance and terrain identification method is proposed in this paper. Firstly, a novel dynamic obstacle avoidance motion planning is proposed which takes full account of the motion ability of the robot. Secondly, a terrain identification method is proposed. The robot can distinguish typical terrains like flat floor, step-on, step-down, ditch and so on. Besides, the ground properties like stiffness are identified to improve the performance of the robot. Finally, the proposed method is integrated on hexapod robots and tested by real experiments which show the method effective. Our methods and experimental results can be a valuable reference for other legged robots.